diff options
Diffstat (limited to 'src/shared')
-rw-r--r-- | src/shared/json.c | 33 | ||||
-rw-r--r-- | src/shared/json.h | 22 |
2 files changed, 33 insertions, 22 deletions
diff --git a/src/shared/json.c b/src/shared/json.c index 69514160c2..be40a0d203 100644 --- a/src/shared/json.c +++ b/src/shared/json.c @@ -27,6 +27,7 @@ int json_variant_new(JsonVariant **ret, JsonVariantType type) { JsonVariant *v; + v = new0(JsonVariant, 1); if (!v) return -ENOMEM; @@ -36,6 +37,8 @@ int json_variant_new(JsonVariant **ret, JsonVariantType type) { } static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) { + int r; + assert(ret); assert(variant); @@ -47,18 +50,18 @@ static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) { if (!ret->string) return -ENOMEM; } else if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT) { + size_t i; + ret->objects = new0(JsonVariant, variant->size); if (!ret->objects) return -ENOMEM; - for (unsigned i = 0; i < variant->size; ++i) { - int r; + for (i = 0; i < variant->size; ++i) { r = json_variant_deep_copy(&ret->objects[i], &variant->objects[i]); if (r < 0) return r; } - } - else + } else ret->value = variant->value; return 0; @@ -72,7 +75,6 @@ static JsonVariant *json_variant_unref_inner(JsonVariant *variant) { if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT) return json_object_unref(variant); - else if (variant->type == JSON_VARIANT_STRING) free(variant->string); @@ -91,11 +93,14 @@ static JsonVariant *json_raw_unref(JsonVariant *variant, size_t size) { } static JsonVariant *json_object_unref(JsonVariant *variant) { + size_t i; + assert(variant); + if (!variant->objects) return NULL; - for (unsigned i = 0; i < variant->size; ++i) + for (i = 0; i < variant->size; ++i) json_variant_unref_inner(&variant->objects[i]); free(variant->objects); @@ -119,6 +124,7 @@ static JsonVariant **json_variant_array_unref(JsonVariant **variant) { return NULL; } + DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant **, json_variant_array_unref); JsonVariant *json_variant_unref(JsonVariant *variant) { @@ -127,7 +133,6 @@ JsonVariant *json_variant_unref(JsonVariant *variant) { if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT) json_object_unref(variant); - else if (variant->type == JSON_VARIANT_STRING) free(variant->string); @@ -174,11 +179,13 @@ JsonVariant *json_variant_element(JsonVariant *variant, unsigned index) { } JsonVariant *json_variant_value(JsonVariant *variant, const char *key) { + size_t i; + assert(variant); assert(variant->type == JSON_VARIANT_OBJECT); assert(variant->objects); - for (unsigned i = 0; i < variant->size; i += 2) { + for (i = 0; i < variant->size; i += 2) { JsonVariant *p = &variant->objects[i]; if (p->type == JSON_VARIANT_STRING && streq(key, p->string)) return &variant->objects[i + 1]; @@ -624,9 +631,11 @@ static int json_scoped_parse(JsonVariant **tokens, size_t *i, size_t n, JsonVari assert(scope); while((var = *i < n ? tokens[(*i)++] : NULL) != NULL) { - bool stopper = !json_is_value(var) && var->value.integer == terminator; + bool stopper; int r; + stopper = !json_is_value(var) && var->value.integer == terminator; + if (stopper) { if (state != STATE_COMMA && size > 0) goto error; @@ -655,7 +664,7 @@ static int json_scoped_parse(JsonVariant **tokens, size_t *i, size_t n, JsonVari state = STATE_VALUE; } else if (state == STATE_VALUE) { - _cleanup_jsonunref_ JsonVariant *v = NULL; + _cleanup_json_variant_unref_ JsonVariant *v = NULL; size_t toadd = arr ? 1 : 2; if (!json_is_value(var)) { @@ -723,7 +732,7 @@ static int json_parse_tokens(JsonVariant **tokens, size_t ntokens, JsonVariant * size_t it = 0; int r; JsonVariant *e; - _cleanup_jsonunref_ JsonVariant *p = NULL; + _cleanup_json_variant_unref_ JsonVariant *p = NULL; assert(tokens); assert(ntokens); @@ -767,8 +776,8 @@ static int json_tokens(const char *string, size_t size, JsonVariant ***tokens, s p = buf; for (;;) { + _cleanup_json_variant_unref_ JsonVariant *var = NULL; _cleanup_free_ char *rstr = NULL; - _cleanup_jsonunref_ JsonVariant *var = NULL; t = json_tokenize(&p, &rstr, &v, &json_state, NULL); diff --git a/src/shared/json.h b/src/shared/json.h index 2ce56420a3..e0b4d810b5 100644 --- a/src/shared/json.h +++ b/src/shared/json.h @@ -57,30 +57,32 @@ union json_value { }; typedef struct JsonVariant { + JsonVariantType type; + size_t size; union { char *string; struct JsonVariant *objects; union json_value value; }; - JsonVariantType type; - unsigned size; } JsonVariant; int json_variant_new(JsonVariant **ret, JsonVariantType type); -JsonVariant *json_variant_unref(JsonVariant *); +JsonVariant *json_variant_unref(JsonVariant *v); + DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref); -#define _cleanup_jsonunref_ _cleanup_(json_variant_unrefp) +#define _cleanup_json_variant_unref_ _cleanup_(json_variant_unrefp) -char *json_variant_string(JsonVariant *); -bool json_variant_bool(JsonVariant *); -intmax_t json_variant_integer(JsonVariant *); -double json_variant_real(JsonVariant *); +char *json_variant_string(JsonVariant *v); +bool json_variant_bool(JsonVariant *v); +intmax_t json_variant_integer(JsonVariant *v); +double json_variant_real(JsonVariant *v); -JsonVariant *json_variant_element(JsonVariant *, unsigned index); -JsonVariant *json_variant_value(JsonVariant *, const char *key); +JsonVariant *json_variant_element(JsonVariant *v, unsigned index); +JsonVariant *json_variant_value(JsonVariant *v, const char *key); #define JSON_VALUE_NULL ((union json_value) {}) int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line); + int json_parse(const char *string, JsonVariant **rv); int json_parse_measure(const char *string, size_t *size); |