summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/json.h27
-rw-r--r--src/array.c8
-rw-r--r--src/object.c12
3 files changed, 39 insertions, 8 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);
diff --git a/src/array.c b/src/array.c
index edf7ed9..c34a8e5 100644
--- a/src/array.c
+++ b/src/array.c
@@ -66,13 +66,17 @@ bool json_array_append(struct json *dest, struct json *src) {
return true;
}
-struct json *json_array_get(const struct json *json, size_t index) {
- if (index > json->children->nitems)
+struct json *json_array_get_mut(struct json *json, size_t index) {
+ if (json->type != JSON_ARRAY || index > json->children->nitems)
return NULL;
return json->children->items[index];
}
+const struct json *json_array_get_const(const struct json *array, size_t index) {
+ return json_array_get_mut((struct json *) array, index);
+}
+
struct json *json_array_add_object(struct json *dest) {
struct json *obj = json_new_object();
json_array_append(dest, obj);
diff --git a/src/object.c b/src/object.c
index 7178bdd..fea4787 100644
--- a/src/object.c
+++ b/src/object.c
@@ -133,7 +133,7 @@ bool json_object_add(struct json *dest, const char *key, struct json *src) {
return true;
}
-struct json *json_object_get(const struct json *obj, const char *key) {
+struct json *json_object_get_mut(struct json *obj, const char *key) {
if (obj->type != JSON_OBJECT) return NULL;
struct json *j = NULL;
json_foreach(j, obj) {
@@ -143,7 +143,7 @@ struct json *json_object_get(const struct json *obj, const char *key) {
return j;
}
-struct json *json_object_getn(const struct json *obj, const char *key, size_t n) {
+struct json *json_object_getn_mut(struct json *obj, const char *key, size_t n) {
if (obj->type != JSON_OBJECT) return NULL;
struct json *j = NULL;
json_foreach(j, obj) {
@@ -153,6 +153,14 @@ struct json *json_object_getn(const struct json *obj, const char *key, size_t n)
return j;
}
+const struct json *json_object_get_const(const struct json *obj, const char *key) {
+ return json_object_get_mut((struct json *) obj, key);
+}
+
+const struct json *json_object_getn_const(const struct json *obj, const char *key, size_t n) {
+ return json_object_getn_mut((struct json *) obj, key, n);
+}
+
struct json *json_object_add_object(struct json *dest, const char *key) {
struct json *obj = json_new_object();
json_object_add(dest, key, obj);