diff options
author | Pavel Odvody <podvody@redhat.com> | 2015-05-19 16:29:42 +0200 |
---|---|---|
committer | Lennart Poettering <lennart@poettering.net> | 2015-05-19 18:21:48 +0200 |
commit | ed967b12be53889b522f02f915336d37d3ff1551 (patch) | |
tree | 3801c18827f4d572356b11c920e91b593aca33ba /src/shared/json.h | |
parent | 74eff91e5f118224c1e556d2716d5360dd97a87d (diff) |
shared/json: Added DOM-like JSON parser
This makes working with complexly structured documents easy
and more reliable as the parser is not susceptible to
element re-ordering.
Also fixes a bug when the tokenizer would choke after reading
a number.
Diffstat (limited to 'src/shared/json.h')
-rw-r--r-- | src/shared/json.h | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/src/shared/json.h b/src/shared/json.h index 55976d513b..2ce56420a3 100644 --- a/src/shared/json.h +++ b/src/shared/json.h @@ -22,6 +22,7 @@ ***/ #include <stdbool.h> +#include "util.h" enum { JSON_END, @@ -38,12 +39,48 @@ enum { JSON_NULL, }; +typedef enum { + JSON_VARIANT_CONTROL, + JSON_VARIANT_STRING, + JSON_VARIANT_INTEGER, + JSON_VARIANT_BOOLEAN, + JSON_VARIANT_REAL, + JSON_VARIANT_ARRAY, + JSON_VARIANT_OBJECT, + JSON_VARIANT_NULL +} JsonVariantType; + union json_value { bool boolean; double real; intmax_t integer; }; +typedef struct JsonVariant { + 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 *); +DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref); +#define _cleanup_jsonunref_ _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 *); + +JsonVariant *json_variant_element(JsonVariant *, unsigned index); +JsonVariant *json_variant_value(JsonVariant *, 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); |