diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-07-26 05:11:33 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2022-07-26 05:11:33 +0000 |
commit | a751023422eadf87cd8d6484878193a4914a9d85 (patch) | |
tree | 3fca0ae6325173fc2583e7d88abe4c38e6193277 /util/json.h | |
parent | Adding upstream version 2.0. (diff) | |
download | nvme-cli-a751023422eadf87cd8d6484878193a4914a9d85.tar.xz nvme-cli-a751023422eadf87cd8d6484878193a4914a9d85.zip |
Adding upstream version 2.1~rc0.upstream/2.1_rc0
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'util/json.h')
-rw-r--r-- | util/json.h | 123 |
1 files changed, 44 insertions, 79 deletions
diff --git a/util/json.h b/util/json.h index 2ef8632..2b8022a 100644 --- a/util/json.h +++ b/util/json.h @@ -1,84 +1,49 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ #ifndef __JSON__H #define __JSON__H -struct json_object; -struct json_pair; - -#define JSON_TYPE_STRING 0 -#define JSON_TYPE_INTEGER 1 -#define JSON_TYPE_FLOAT 2 -#define JSON_TYPE_OBJECT 3 -#define JSON_TYPE_ARRAY 4 -#define JSON_TYPE_UINT 5 -#define JSON_PARENT_TYPE_PAIR 0 -#define JSON_PARENT_TYPE_ARRAY 1 -struct json_value { - int type; - union { - long long integer_number; - unsigned long long uint_number; - long double float_number; - char *string; - struct json_object *object; - struct json_object *array; - }; - int parent_type; - union { - struct json_pair *parent_pair; - struct json_object *parent_array; - }; -}; - -struct json_object { - struct json_value **values; - int value_cnt; - struct json_pair **pairs; - int pair_cnt; - struct json_value *parent; -}; - -struct json_pair { - char *name; - struct json_value *value; - struct json_object *parent; -}; - -struct json_object *json_create_object(void); -struct json_object *json_create_array(void); - -void json_free_object(struct json_object *obj); -void json_free_array(struct json_object *array); - -int json_object_add_value_type(struct json_object *obj, const char *name, int type, ...); -#define json_object_add_value_int(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_INTEGER, (long long) (val)) -#define json_object_add_value_uint(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_UINT, (unsigned long long) (val)) -#define json_object_add_value_uint64 json_object_add_value_uint -#define json_object_add_value_float(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_FLOAT, (val)) -#define json_object_add_value_string(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_STRING, (val)) -#define json_object_add_value_object(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_OBJECT, (val)) -#define json_object_add_value_array(obj, name, val) \ - json_object_add_value_type((obj), name, JSON_TYPE_ARRAY, (val)) -int json_array_add_value_type(struct json_object *array, int type, ...); -#define json_array_add_value_int(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_INTEGER, (val)) -#define json_array_add_value_uint(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_UINT, (val)) -#define json_array_add_value_float(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_FLOAT, (val)) -#define json_array_add_value_string(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_STRING, (val)) -#define json_array_add_value_object(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_OBJECT, (val)) -#define json_array_add_value_array(obj, val) \ - json_array_add_value_type((obj), JSON_TYPE_ARRAY, (val)) - -#define json_array_last_value_object(obj) \ - (obj->values[obj->value_cnt - 1]->object) +#include <json.h> + +/* Wrappers around json-c's API */ + +#define json_create_object(o) json_object_new_object(o) +#define json_create_array(a) json_object_new_array(a) +#define json_free_object(o) json_object_put(o) +#define json_free_array(a) json_object_put(a) +#define json_object_add_value_uint(o, k, v) \ + json_object_object_add(o, k, json_object_new_int(v)) +#define json_object_add_value_int(o, k, v) \ + json_object_object_add(o, k, json_object_new_int(v)) +#ifdef CONFIG_JSONC_14 +#define json_object_add_value_uint64(o, k, v) \ + json_object_object_add(o, k, json_object_new_uint64(v)) +#else +#define json_object_add_value_uint64(o, k, v) \ + if ((v) > UINT_MAX) { \ + fprintf(stderr, "Value overflow in %s", k); \ + json_object_object_add(o, k, json_object_new_int(-1)); \ + } else \ + json_object_object_add(o, k, json_object_new_int(v)) +#endif +#define json_object_add_value_double(o, k, v) \ + json_object_object_add(o, k, util_json_object_new_double(v)) +#define json_object_add_value_float(o, k, v) \ + json_object_object_add(o, k, json_object_new_double(v)) +#define json_object_add_value_string(o, k, v) \ + json_object_object_add(o, k, json_object_new_string(v)) +#define json_object_add_value_array(o, k, v) \ + json_object_object_add(o, k, v) +#define json_object_add_value_object(o, k, v) \ + json_object_object_add(o, k, v) +#define json_array_add_value_object(o, k) \ + json_object_array_add(o, k) +#define json_array_add_value_string(o, v) \ + json_object_array_add(o, json_object_new_string(v)) +#define json_print_object(o, u) \ + printf("%s", json_object_to_json_string_ext(o, \ + JSON_C_TO_STRING_PRETTY | \ + JSON_C_TO_STRING_NOSLASHESCAPE)) + +struct json_object *util_json_object_new_double(long double d); -void json_print_object(struct json_object *obj, void *); #endif |