Enhance handling of circular references
[jansson.git] / src / value.c
index d8e9e9a..1982b90 100644 (file)
 #include "utf.h"
 #include "util.h"
 
-#define container_of(ptr_, type_, member_)  \
-    ((type_ *)((char *)ptr_ - (size_t)&((type_ *)0)->member_))
-
-typedef struct {
-    json_t json;
-    hashtable_t hashtable;
-} json_object_t;
-
-typedef struct {
-    json_t json;
-    unsigned int size;
-    unsigned int entries;
-    json_t **table;
-} json_array_t;
-
-typedef struct {
-    json_t json;
-    char *value;
-} json_string_t;
-
-typedef struct {
-    json_t json;
-    double value;
-} json_real_t;
-
-typedef struct {
-    json_t json;
-    int value;
-} json_integer_t;
-
-#define json_to_object(json_)  container_of(json_, json_object_t, json)
-#define json_to_array(json_)   container_of(json_, json_array_t, json)
-#define json_to_string(json_)  container_of(json_, json_string_t, json)
-#define json_to_real(json_)   container_of(json_, json_real_t, json)
-#define json_to_integer(json_) container_of(json_, json_integer_t, json)
 
 static inline void json_init(json_t *json, json_type type)
 {
@@ -98,6 +63,9 @@ json_t *json_object(void)
         free(object);
         return NULL;
     }
+
+    object->visited = 0;
+
     return &object->json;
 }
 
@@ -107,6 +75,17 @@ static void json_delete_object(json_object_t *object)
     free(object);
 }
 
+unsigned int json_object_size(const json_t *json)
+{
+    json_object_t *object;
+
+    if(!json_is_object(json))
+        return -1;
+
+    object = json_to_object(json);
+    return object->hashtable.size;
+}
+
 json_t *json_object_get(const json_t *json, const char *key)
 {
     json_object_t *object;
@@ -125,7 +104,7 @@ int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value)
     if(!key || !value)
         return -1;
 
-    if(!json_is_object(json))
+    if(!json_is_object(json) || json == value)
     {
         json_decref(value);
         return -1;
@@ -168,6 +147,43 @@ int json_object_del(json_t *json, const char *key)
     return hashtable_del(&object->hashtable, key);
 }
 
+int json_object_clear(json_t *json)
+{
+    json_object_t *object;
+
+    if(!json_is_object(json))
+        return -1;
+
+    object = json_to_object(json);
+    hashtable_clear(&object->hashtable);
+
+    return 0;
+}
+
+int json_object_update(json_t *object, json_t *other)
+{
+    void *iter;
+
+    if(!json_is_object(object) || !json_is_object(other))
+        return -1;
+
+    iter = json_object_iter(other);
+    while(iter) {
+        const char *key;
+        json_t *value;
+
+        key = json_object_iter_key(iter);
+        value = json_object_iter_value(iter);
+
+        if(json_object_set(object, key, value))
+            return -1;
+
+        iter = json_object_iter_next(other, iter);
+    }
+
+    return 0;
+}
+
 void *json_object_iter(json_t *json)
 {
     json_object_t *object;
@@ -225,6 +241,8 @@ json_t *json_array(void)
         return NULL;
     }
 
+    array->visited = 0;
+
     return &array->json;
 }
 
@@ -267,7 +285,7 @@ int json_array_set_new(json_t *json, unsigned int index, json_t *value)
     if(!value)
         return -1;
 
-    if(!json_is_array(json))
+    if(!json_is_array(json) || json == value)
     {
         json_decref(value);
         return -1;
@@ -335,7 +353,7 @@ int json_array_append_new(json_t *json, json_t *value)
     if(!value)
         return -1;
 
-    if(!json_is_array(json))
+    if(!json_is_array(json) || json == value)
     {
         json_decref(value);
         return -1;
@@ -361,7 +379,7 @@ int json_array_insert_new(json_t *json, unsigned int index, json_t *value)
     if(!value)
         return -1;
 
-    if(!json_is_array(json)) {
+    if(!json_is_array(json) || json == value) {
         json_decref(value);
         return -1;
     }
@@ -490,6 +508,25 @@ const char *json_string_value(const json_t *json)
     return json_to_string(json)->value;
 }
 
+int json_string_set(const json_t *json, const char *value)
+{
+    char *dup;
+    json_string_t *string;
+
+    if(!json_is_string(json) || !value || !utf8_check_string(value, -1))
+        return -1;
+
+    dup = strdup(value);
+    if(!dup)
+        return -1;
+
+    string = json_to_string(json);
+    free(string->value);
+    string->value = dup;
+
+    return 0;
+}
+
 static void json_delete_string(json_string_t *string)
 {
     free(string->value);
@@ -518,6 +555,16 @@ int json_integer_value(const json_t *json)
     return json_to_integer(json)->value;
 }
 
+int json_integer_set(const json_t *json, int value)
+{
+    if(!json_is_integer(json))
+        return -1;
+
+    json_to_integer(json)->value = value;
+
+    return 0;
+}
+
 static void json_delete_integer(json_integer_t *integer)
 {
     free(integer);
@@ -545,7 +592,17 @@ double json_real_value(const json_t *json)
     return json_to_real(json)->value;
 }
 
-static void json_delete_real (json_real_t *real)
+int json_real_set(const json_t *json, double value)
+{
+    if(!json_is_real(json))
+        return 0;
+
+    json_to_real(json)->value = value;
+
+    return 0;
+}
+
+static void json_delete_real(json_real_t *real)
 {
     free(real);
 }