From 1dd3e1c8d7deb0fa6fe6208b2dd8d236d1f3fc2e Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Fri, 12 Apr 2024 12:18:03 +0200 Subject: libjson: new api a lot... changed... Signed-off-by: Anna (navi) Figueiredo Gomes --- include/json.h | 130 ++++++++++++++++++++++++++++++--------------------------- 1 file changed, 69 insertions(+), 61 deletions(-) (limited to 'include') diff --git a/include/json.h b/include/json.h index 138bb15..85749d6 100644 --- a/include/json.h +++ b/include/json.h @@ -7,15 +7,13 @@ struct json { char *key; size_t index; - enum { - JSON_FALSE, - JSON_TRUE, - JSON_OBJECT, - JSON_ARRAY, - JSON_STRING, - JSON_NUMBER, - JSON_NULL, - JSON_INVALID + enum json_type { + JSON_NULL = 0, + JSON_BOOL = 1 << 0, + JSON_STRING = 1 << 1, + JSON_NUMBER = 1 << 2, + JSON_OBJECT = 1 << 3, + JSON_ARRAY = 1 << 4, } type; union { struct json_children { @@ -24,6 +22,7 @@ struct json { } *children; char *string; double num; + bool boolean; }; struct json *parent; @@ -46,109 +45,118 @@ enum json_parse_result { }; struct json *json_parse(const char *str); -struct json *json_parse_len(size_t size, const char str[size]); +struct json *json_parse_sized(size_t len, const char str[len]); struct json *json_new(void); +struct json *json_dup(const struct json *src); void json_clear(struct json *json); void json_detach(struct json *json); void json_delete(struct json *json); +size_t json_len(const struct json *json); + /* ====== json object ====== */ -struct json *json_new_object(void); +struct json *json_object(void); -#define json_object_get(obj, key) _Generic((obj), \ - struct json *: json_object_get_mut, \ - const struct json *: json_object_get_const \ +#define json_get(obj, key) _Generic((obj), \ + struct json *: json_get_mut, \ + const struct json *: json_get_const \ )((obj), (key)) -#define json_object_getn(obj, key, n) _Generic((obj), \ - struct json *: json_object_getn_mut, \ - const struct json *: json_object_getn_const \ - )((obj), (key), (n)) +#define json_get_sized(obj, len, key) _Generic((obj), \ + struct json *: json_get_sized_mut, \ + const struct json *: json_get_sized_const \ + )((obj), (len), (key)) + +struct json *json_get_mut(struct json *obj, const char key[]); +struct json *json_get_sized_mut(struct json *obj, size_t len, const char key[len]); -struct json *json_object_get_mut(struct json *obj, const char *key); -struct json *json_object_getn_mut(struct json *obj, const char *key, size_t n); +const struct json *json_get_const(const struct json *obj, const char key[]); +const struct json *json_get_sized_const(const struct json *obj, size_t len, const char key[len]); -const struct json *json_object_get_const(const struct json *obj, const char *key); -const struct json *json_object_getn_const(const struct json *obj, const char *key, size_t n); +struct json *json_set(struct json *dest, const char *key, struct json *src); +//struct json *json_set_sized(struct json *dest, size_t len, const char key[len], struct json *src); -bool json_object_add(struct json *dest, const char *key, struct json *src); -struct json *json_object_add_object(struct json *dest, const char *key); -struct json *json_object_add_array(struct json *dest, const char *key); -struct json *json_object_add_number(struct json *dest, const char *key, double num); -struct json *json_object_add_string(struct json *dest, const char *key, const char *string); -struct json *json_object_add_bool(struct json *dest, const char *key, bool boolean); -struct json *json_object_add_null(struct json *dest, const char *key); +struct json *json_detach_key(struct json *json, const char *key); +struct json *json_swap_key(struct json *json, const char *key, struct json *src); +void json_delete_key(struct json *json, const char *key); -struct json *json_object_detach(struct json *json, const char *key); -void json_object_delete(struct json *json, const char *key); +struct json *json_detach_key_sized(struct json *json, size_t len, const char key[len]); +struct json *json_swap_key_sized(struct json *json, size_t len, const char key[len], struct json *src); +void json_delete_key_sized(struct json *json, size_t len, const char key[len]); /* ====== json array ====== */ -struct json *json_new_array(void); -bool json_array_append(struct json *dest, struct json *src); +struct json *json_array(void); +//struct json *json_array_from(size_t count, struct json *array[count]); +bool json_append(struct json *dest, struct json *src); +bool json_insert(struct json *dest, size_t index, struct json *src); +bool json_replace(struct json *dest, size_t index, struct json *src); -#define json_array_get(obj, index) _Generic((obj), \ - struct json *: json_array_get_mut, \ - const struct json *: json_array_get_const \ +#define json_at(obj, index) _Generic((obj), \ + struct json *: json_at_mut, \ + const struct json *: json_at_const \ )((obj), (index)) -struct json *json_array_get_mut(struct json *array, size_t index); -const struct json *json_array_get_const(const struct json *array, size_t index); +struct json *json_at_mut(struct json *array, size_t index); +const struct json *json_at_const(const struct json *array, size_t index); -struct json *json_array_add_object(struct json *dest); -struct json *json_array_add_array(struct json *dest); -struct json *json_array_add_number(struct json *dest, double num); -struct json *json_array_add_string(struct json *dest, const char *string); -struct json *json_array_add_bool(struct json *dest, bool boolean); -struct json *json_array_add_null(struct json *dest); - -struct json *json_array_detach(struct json *json, size_t index); -void json_array_delete(struct json *json, size_t index); +struct json *json_detach_index(struct json *json, size_t index); +struct json *json_swap_index(struct json *json, size_t index, struct json *src); +void json_delete_index(struct json *json, size_t index); /* ====== json string ====== */ -struct json *json_new_string(void); -struct json *json_new_string_copy(const char *string); -char *json_string_get(const struct json *string); +struct json *json_string(const char *string); +struct json *json_string_from(char *string); + +#define json_string_get(string) _Generic((string), \ + struct json *: json_string_get_mut, \ + const struct json *: json_string_get_const \ + )(string) + +char *json_string_get_mut(struct json *string); const char *json_string_get_const(const struct json *string); void json_string_set(struct json *dest, const char *string); /* ====== json literals ====== */ -struct json *json_new_number(double number); -struct json *json_new_bool(bool boolean); -struct json *json_new_null(void); +struct json *json_number(double number); +struct json *json_bool(bool boolean); char *json_print(struct json *json); #define json_foreach(e, o) \ - for (e = json_is_object(o) || json_is_array(o) ? o->children->items[0] : NULL; e; e = e->next) + for (struct json *e = json_check_type((o), JSON_OBJECT | JSON_ARRAY) ? (o)->children->items[0] : NULL; e; e = e->next) + +static inline bool json_check_type(const struct json *json, const enum json_type type) { + return json && json->type & type; +} -static inline bool json_is_object(struct json *json) { +static inline bool json_is_object(const struct json *json) { return json && json->type == JSON_OBJECT; } -static inline bool json_is_array(struct json *json) { +static inline bool json_is_array(const struct json *json) { return json && json->type == JSON_ARRAY; } -static inline bool json_is_number(struct json *json) { +static inline bool json_is_number(const struct json *json) { return json && json->type == JSON_NUMBER; } -static inline bool json_is_string(struct json *json) { +static inline bool json_is_string(const struct json *json) { return json && json->type == JSON_STRING; } -static inline bool json_is_bool(struct json *json) { - return json && (json->type == JSON_TRUE || json->type == JSON_FALSE); +static inline bool json_is_bool(const struct json *json) { + return json && json->type == JSON_BOOL; } -static inline bool json_is_null(struct json *json) { +static inline bool json_is_null(const struct json *json) { return json && json->type == JSON_NULL; } -- cgit v1.2.3