diff options
author | Anna (navi) Figueiredo Gomes <navi@vlhl.dev> | 2024-03-07 21:50:08 +0100 |
---|---|---|
committer | Anna (navi) Figueiredo Gomes <navi@vlhl.dev> | 2024-03-07 21:50:08 +0100 |
commit | 46bc58545b95ee1cba1292dcaeb46c60533e0184 (patch) | |
tree | 26b43cf1ae0c6b8e9ee90d0d27491e6448e74d19 /include/json.h | |
parent | 00585c403abf45ac15b53176262aee4b05641f23 (diff) |
json.h, object.c, array.c: const correct getters
Signed-off-by: Anna (navi) Figueiredo Gomes <navi@vlhl.dev>
Diffstat (limited to 'include/json.h')
-rw-r--r-- | include/json.h | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/include/json.h b/include/json.h index 9cc5bd1..138bb15 100644 --- a/include/json.h +++ b/include/json.h @@ -57,11 +57,24 @@ void json_delete(struct json *json); /* ====== json object ====== */ struct json *json_new_object(void); -bool json_object_add(struct json *dest, const char *key, struct json *src); -struct json *json_object_get(const struct json *obj, const char *key); -struct json *json_object_getn(const struct json *obj, const char *key, size_t n); +#define json_object_get(obj, key) _Generic((obj), \ + struct json *: json_object_get_mut, \ + const struct json *: json_object_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)) + +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_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); + +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); @@ -77,7 +90,13 @@ void json_object_delete(struct json *json, const char *key); struct json *json_new_array(void); bool json_array_append(struct json *dest, struct json *src); -struct json *json_array_get(const struct json *array, size_t index); +#define json_array_get(obj, index) _Generic((obj), \ + struct json *: json_array_get_mut, \ + const struct json *: json_array_get_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_array_add_object(struct json *dest); struct json *json_array_add_array(struct json *dest); |