From a832b08e6e1bb93829b488615006bba768c50121 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Thu, 4 Jul 2019 18:27:02 +0200 Subject: [PATCH] json: add json_variant_set_field_integer() and json_variant_set_field_boolean() helpers --- src/shared/json.c | 22 ++++++++++++++++++++++ src/shared/json.h | 2 ++ 2 files changed, 24 insertions(+) diff --git a/src/shared/json.c b/src/shared/json.c index d1a477c36d6..7c62136703e 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -1905,6 +1905,17 @@ int json_variant_set_field_string(JsonVariant **v, const char *field, const char return json_variant_set_field(v, field, m); } +int json_variant_set_field_integer(JsonVariant **v, const char *field, intmax_t i) { + _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; + int r; + + r = json_variant_new_integer(&m, i); + if (r < 0) + return r; + + return json_variant_set_field(v, field, m); +} + int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uintmax_t u) { _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; int r; @@ -1916,6 +1927,17 @@ int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uintmax_ return json_variant_set_field(v, field, m); } +int json_variant_set_field_boolean(JsonVariant **v, const char *field, bool b) { + _cleanup_(json_variant_unrefp) JsonVariant *m = NULL; + int r; + + r = json_variant_new_boolean(&m, b); + 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; diff --git a/src/shared/json.h b/src/shared/json.h index 393e35a6479..bb72b43015e 100644 --- a/src/shared/json.h +++ b/src/shared/json.h @@ -178,7 +178,9 @@ int json_variant_filter(JsonVariant **v, char **to_remove); int json_variant_set_field(JsonVariant **v, const char *field, JsonVariant *value); int json_variant_set_field_string(JsonVariant **v, const char *field, const char *value); +int json_variant_set_field_integer(JsonVariant **v, const char *field, intmax_t value); int json_variant_set_field_unsigned(JsonVariant **v, const char *field, uintmax_t value); +int json_variant_set_field_boolean(JsonVariant **v, const char *field, bool b); int json_variant_append_array(JsonVariant **v, JsonVariant *element);