json: introduce json_append()

This commit is contained in:
Yu Watanabe 2022-09-03 23:10:24 +09:00
parent d12c0f4cfe
commit 41dceb91dd
3 changed files with 42 additions and 0 deletions

View file

@ -4120,6 +4120,30 @@ int json_build(JsonVariant **ret, ...) {
return r;
}
int json_appendv(JsonVariant **v, va_list ap) {
_cleanup_(json_variant_unrefp) JsonVariant *w = NULL;
int r;
assert(v);
r = json_buildv(&w, ap);
if (r < 0)
return r;
return json_variant_merge(v, w);
}
int json_append(JsonVariant **v, ...) {
va_list ap;
int r;
va_start(ap, v);
r = json_appendv(v, ap);
va_end(ap);
return r;
}
int json_log_internal(
JsonVariant *variant,
int level,

View file

@ -327,6 +327,9 @@ enum {
int json_build(JsonVariant **ret, ...);
int json_buildv(JsonVariant **ret, va_list ap);
/* These two functions below are equivalent to json_build() (or json_buildv()) and json_variant_merge(). */
int json_append(JsonVariant **v, ...);
int json_appendv(JsonVariant **v, va_list ap);
/* A bitmask of flags used by the dispatch logic. Note that this is a combined bit mask, that is generated from the bit
* mask originally passed into json_dispatch(), the individual bitmask associated with the static JsonDispatch callout

View file

@ -631,4 +631,19 @@ TEST(variant) {
test_variant_one("[ 0, -0, 0.0, -0.0, 0.000, -0.000, 0e0, -0e0, 0e+0, -0e-0, 0e-0, -0e000, 0e+000 ]", test_zeroes);
}
TEST(json_append) {
_cleanup_(json_variant_unrefp) JsonVariant *v = NULL, *w = NULL;
assert_se(json_build(&v, JSON_BUILD_OBJECT(
JSON_BUILD_PAIR("b", JSON_BUILD_STRING("x")),
JSON_BUILD_PAIR("c", JSON_BUILD_CONST_STRING("y")),
JSON_BUILD_PAIR("a", JSON_BUILD_CONST_STRING("z")))) >= 0);
assert_se(json_append(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("b", JSON_BUILD_STRING("x")))) >= 0);
assert_se(json_append(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("c", JSON_BUILD_STRING("y")))) >= 0);
assert_se(json_append(&w, JSON_BUILD_OBJECT(JSON_BUILD_PAIR("a", JSON_BUILD_STRING("z")))) >= 0);
assert_se(json_variant_equal(v, w));
}
DEFINE_TEST_MAIN(LOG_DEBUG);