diff options
author | Lennart Poettering <lennart@poettering.net> | 2018-11-27 22:26:21 +0100 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2018-11-28 08:38:55 +0100 |
commit | 319a4f27c477cbc63decdc510b7ca698a7be44f4 (patch) | |
tree | 3b7810fb51304b549612d61a3e9f9929bd15b52f /src/test/test-json.c | |
parent | 2de622531400f45f60d8182d41fceb1d7335e5e8 (diff) | |
download | systemd-319a4f27c477cbc63decdc510b7ca698a7be44f4.tar.gz |
json: teach json builder "conditional" object fields
Quite often when we generate objects some fields should only be
generated in some conditions. Let's add high-level support for that.
Matching the existing JSON_BUILD_PAIR() this adds
JSON_BUILD_PAIR_CONDITIONAL() which is very similar, but takes an
additional parameter: a boolean condition. If "true" this acts like
JSON_BUILD_PAIR(), but if false then the whole pair is suppressed.
This sounds simply, but requires a tiny bit of complexity: when complex
sub-variants are used in fields, then we also need to suppress them.
Diffstat (limited to 'src/test/test-json.c')
-rw-r--r-- | src/test/test-json.c | 16 |
1 files changed, 16 insertions, 0 deletions
diff --git a/src/test/test-json.c b/src/test/test-json.c index 6a57f88882..d3ece0bc0b 100644 --- a/src/test/test-json.c +++ b/src/test/test-json.c @@ -316,6 +316,22 @@ static void test_build(void) { a = json_variant_unref(a); b = json_variant_unref(b); + + assert_se(json_build(&a, JSON_BUILD_OBJECT( + JSON_BUILD_PAIR("x", JSON_BUILD_STRING("y")), + JSON_BUILD_PAIR("z", JSON_BUILD_STRING("a")), + JSON_BUILD_PAIR("b", JSON_BUILD_STRING("c")) + )) >= 0); + + assert_se(json_build(&b, JSON_BUILD_OBJECT( + JSON_BUILD_PAIR("x", JSON_BUILD_STRING("y")), + JSON_BUILD_PAIR_CONDITION(false, "p", JSON_BUILD_STRING("q")), + JSON_BUILD_PAIR_CONDITION(true, "z", JSON_BUILD_STRING("a")), + JSON_BUILD_PAIR_CONDITION(false, "j", JSON_BUILD_ARRAY(JSON_BUILD_STRING("k"), JSON_BUILD_STRING("u"), JSON_BUILD_STRING("i"))), + JSON_BUILD_PAIR("b", JSON_BUILD_STRING("c")) + )) >= 0); + + assert_se(json_variant_equal(a, b)); } static void test_source(void) { |