From 46bc58545b95ee1cba1292dcaeb46c60533e0184 Mon Sep 17 00:00:00 2001 From: "Anna (navi) Figueiredo Gomes" Date: Thu, 7 Mar 2024 21:50:08 +0100 Subject: json.h, object.c, array.c: const correct getters Signed-off-by: Anna (navi) Figueiredo Gomes --- include/json.h | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'include/json.h') 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); -- cgit v1.2.3