diff options
author | Lennart Poettering <lennart@poettering.net> | 2020-08-11 23:16:28 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2020-08-12 22:39:43 +0200 |
commit | 0b9481cf2ec328015b535af79b80f61f14255dfc (patch) | |
tree | ab327c8880f304587b6cc5641de15982e9675ea2 /src/shared/json.c | |
parent | man: document systemd-dissect (diff) | |
download | systemd-0b9481cf2ec328015b535af79b80f61f14255dfc.tar.xz systemd-0b9481cf2ec328015b535af79b80f61f14255dfc.zip |
json: add helpers for dealing with id128 + strv
Diffstat (limited to 'src/shared/json.c')
-rw-r--r-- | src/shared/json.c | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/src/shared/json.c b/src/shared/json.c index 27a3a518fe..2e151578ff 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -430,6 +430,12 @@ int json_variant_new_base64(JsonVariant **ret, const void *p, size_t n) { return json_variant_new_stringn(ret, s, k); } +int json_variant_new_id128(JsonVariant **ret, sd_id128_t id) { + char s[SD_ID128_STRING_MAX]; + + return json_variant_new_string(ret, sd_id128_to_string(id, s)); +} + static void json_variant_set(JsonVariant *a, JsonVariant *b) { assert(a); @@ -1964,6 +1970,17 @@ int json_variant_set_field_boolean(JsonVariant **v, const char *field, bool b) { return json_variant_set_field(v, field, m); } +int json_variant_set_field_strv(JsonVariant **v, const char *field, char **l) { + _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; + int r; + + r = json_variant_new_array_strv(&m, l); + if (r < 0) + return r; + + return json_variant_set_field(v, field, m); +} + int json_variant_merge(JsonVariant **v, JsonVariant *m) { _cleanup_(json_variant_unrefp) JsonVariant *w = NULL; _cleanup_free_ JsonVariant **array = NULL; @@ -3579,6 +3596,34 @@ int json_buildv(JsonVariant **ret, va_list ap) { break; } + case _JSON_BUILD_ID128: { + sd_id128_t id; + + if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { + r = -EINVAL; + goto finish; + } + + id = va_arg(ap, sd_id128_t); + + if (current->n_suppress == 0) { + r = json_variant_new_id128(&add, id); + if (r < 0) + goto finish; + } + + n_subtract = 1; + + if (current->expect == EXPECT_TOPLEVEL) + current->expect = EXPECT_END; + else if (current->expect == EXPECT_OBJECT_VALUE) + current->expect = EXPECT_OBJECT_KEY; + else + assert(current->expect == EXPECT_ARRAY_ELEMENT); + + break; + } + case _JSON_BUILD_OBJECT_BEGIN: if (!IN_SET(current->expect, EXPECT_TOPLEVEL, EXPECT_OBJECT_VALUE, EXPECT_ARRAY_ELEMENT)) { |