src/json.c

changeset 1037
83620ba72cc1
parent 1033
e3009345984b
--- a/src/json.c	Fri Dec 20 16:56:20 2024 +0100
+++ b/src/json.c	Fri Dec 20 21:09:20 2024 +0100
@@ -375,16 +375,6 @@
     return v;
 }
 
-static int json_obj_add_entry(const CxJson *json, char *name) {
-    CxJsonObjValue kv = {name, NULL};
-    assert(json->vbuf_size > 0);
-    CxJsonValue *parent = json->vbuf[json->vbuf_size - 1];
-    assert(parent != NULL);
-    assert(parent->type == CX_JSON_OBJECT);
-    CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL);
-    return cx_array_simple_add_a(&value_realloc, parent->value.object.values, kv);
-}
-
 #define JP_STATE_VALUE_BEGIN         0
 #define JP_STATE_VALUE_END          10
 #define JP_STATE_VALUE_BEGIN_OBJ     1
@@ -568,7 +558,15 @@
             if (name.ptr == NULL) {
                 return_rec(CX_JSON_VALUE_ALLOC_FAILED);
             }
-            json_obj_add_entry(json, name.ptr);
+            CxJsonObjValue kv = {name, NULL};
+            assert(json->vbuf_size > 0);
+            CxJsonValue *parent = json->vbuf[json->vbuf_size - 1];
+            assert(parent != NULL);
+            assert(parent->type == CX_JSON_OBJECT);
+            CxArrayReallocator value_realloc = cx_array_reallocator(json->allocator, NULL);
+            if (cx_array_simple_add_a(&value_realloc, parent->value.object.values, kv)) {
+                return_rec(CX_JSON_VALUE_ALLOC_FAILED);
+            }
 
             // next state
             json_add_state(json, JP_STATE_OBJ_COLON);
@@ -643,7 +641,7 @@
             CxJsonObject obj = value->value.object;
             for (size_t i = 0; i < obj.values_size; i++) {
                 cxJsonValueFree(obj.values[i].value);
-                cxFree(value->allocator, obj.values[i].name);
+                cx_strfree_a(value->allocator, &obj.values[i].name);
             }
             cxFree(value->allocator, obj.values);
             break;
@@ -707,11 +705,11 @@
     return iter;
 }
 
-CxJsonValue *cxJsonObjGet(const CxJsonValue *value, const char *name) {
+CxJsonValue *cx_json_obj_get_cxstr(const CxJsonValue *value, cxstring name) {
     const CxJsonObject *obj = &(value->value.object);
     // TODO: think about sorting the object so that we can use binary search here
     for (size_t i = 0; i < obj->values_size; i++) {
-        if (0 == strcmp(name, obj->values[i].name)) {
+        if (0 == cx_strcmp(name, cx_strcast(obj->values[i].name))) {
             return obj->values[i].value;
         }
     }

mercurial