fix cxJsonObjPut() for existing keys

Thu, 26 Dec 2024 18:32:05 +0100

author
Mike Becker <universe@uap-core.de>
date
Thu, 26 Dec 2024 18:32:05 +0100
changeset 1055
221e2e2f2c06
parent 1054
fb1076ead56f
child 1056
e180bd389fbc

fix cxJsonObjPut() for existing keys

issue #532

src/json.c file | annotate | diff | comparison | revisions
--- a/src/json.c	Wed Dec 25 19:33:04 2024 +0100
+++ b/src/json.c	Thu Dec 26 18:32:05 2024 +0100
@@ -805,6 +805,16 @@
 }
 
 int cxJsonObjPut(CxJsonValue* obj, cxstring name, CxJsonValue* child) {
+    // TODO: optimize - issue #462
+    for (size_t i = 0; i < obj->value.object.values_size; i++) {
+        if (0 == cx_strcmp(name, cx_strcast(obj->value.object.values[i].name))) {
+            // free the original value
+            cxJsonValueFree(obj->value.object.values[i].value);
+            obj->value.object.values[i].value = child;
+            return 0;
+        }
+    }
+
     CxArrayReallocator value_realloc = cx_array_reallocator(obj->allocator, NULL);
     assert(obj->type == CX_JSON_OBJECT);
     cxmutstr k = cx_strdup_a(obj->allocator, name);

mercurial