src/json.c

changeset 1338
cc153bffea28
parent 1337
6dfa1eb58ce3
child 1340
31c61b6dcaa5
--- a/src/json.c	Thu Aug 14 23:03:01 2025 +0200
+++ b/src/json.c	Fri Aug 15 17:42:01 2025 +0200
@@ -46,22 +46,17 @@
     return cx_strcmp(cx_strcast(left->name), cx_strcast(right->name));
 }
 
-static CxJsonObjValue *json_find_objvalue(const CxJsonValue *obj, cxstring name) {
+static size_t json_find_objvalue(const CxJsonValue *obj, cxstring name) {
     assert(obj->type == CX_JSON_OBJECT);
     CxJsonObjValue kv_dummy;
     kv_dummy.name = cx_mutstrn((char*) name.ptr, name.length);
-    size_t index = cx_array_binary_search(
+    return cx_array_binary_search(
             obj->value.object.values,
             obj->value.object.values_size,
             sizeof(CxJsonObjValue),
             &kv_dummy,
             json_cmp_objvalue
     );
-    if (index == obj->value.object.values_size) {
-        return NULL;
-    } else {
-        return &obj->value.object.values[index];
-    }
 }
 
 static int json_add_objvalue(CxJsonValue *objv, CxJsonObjValue member) {
@@ -1156,11 +1151,25 @@
 }
 
 CxJsonValue *cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name) {
-    CxJsonObjValue *member = json_find_objvalue(value, name);
-    if (member == NULL) {
+    size_t index = json_find_objvalue(value, name);
+    if (index == value->value.object.values_size) {
         return &cx_json_value_nothing;
     } else {
-        return member->value;
+        return value->value.object.values[index].value;
+    }
+}
+
+CxJsonValue *cx_json_obj_remove_cxstr(CxJsonValue *value, cxstring name) {
+    size_t index = json_find_objvalue(value, name);
+    if (index == value->value.object.values_size) {
+        return NULL;
+    } else {
+        CxJsonObjValue kv = value->value.object.values[index];
+        cx_strfree_a(value->allocator, &kv.name);
+        // TODO: replace with cx_array_remove()
+        value->value.object.values_size--;
+        memmove(value->value.object.values + index, value->value.object.values + index + 1, (value->value.object.values_size - index) * sizeof(CxJsonObjValue));
+        return kv.value;
     }
 }
 

mercurial