src/json.c

changeset 967
a58f602ed2fe
parent 965
dfdfedbe2c86
child 969
72e5432f6b42
--- a/src/json.c	Fri Nov 01 17:35:42 2024 +0100
+++ b/src/json.c	Fri Nov 01 18:16:32 2024 +0100
@@ -30,7 +30,6 @@
 #include <ctype.h>
 
 #include "cx/json.h"
-#include "cx/array_list.h"
 
 /*
  * RFC 8259
@@ -566,74 +565,11 @@
     return 0;
 }
 
-static int obj_init_values(CxJson *p, CxJsonValue *v) {
-    v->value.object.values = calloc(p->reader_array_alloc, sizeof(CxJsonObjValue));
-    if (!v->value.object.values) {
-        return -1;
-    }
-    v->value.object.alloc = p->reader_array_alloc;
-    v->value.object.size = 0;
-
-    return 0;
-}
-
-static int obj_add_value(CxJson *p, CxJsonValue *parent, CxJsonObjValue v) {
-    if (!parent->value.object.values) {
-        if (obj_init_values(p, parent)) {
-            return -1;
-        }
-    }
-
-    if (parent->value.object.size == parent->value.object.alloc) {
-        parent->value.object.alloc *= 2;
-        if (cx_reallocate(&parent->value.object.values,
-                          sizeof(CxJsonObjValue) * parent->value.object.alloc)) {
-            return -1;
-        }
-    }
-
-    parent->value.object.values[parent->value.object.size++] = v;
-
-    return 0;
-}
-
-static int array_init(CxJson *p, CxJsonValue *v) {
-    v->value.array.array = calloc(p->reader_array_alloc, sizeof(CxJsonValue *));
-    if (!v->value.array.array) {
-        return -1;
-    }
-    v->value.array.alloc = p->reader_array_alloc;
-    v->value.array.size = 0;
-
-    return 0;
-}
-
-static int array_add_value(CxJson *p, CxJsonValue *parent, CxJsonValue *v) {
-    if (!parent->value.array.array) {
-        if (array_init(p, parent)) {
-            return -1;
-        }
-    }
-
-    if (parent->value.array.size == parent->value.array.alloc) {
-        parent->value.array.alloc *= 2;
-        if (cx_reallocate(parent->value.array.array,
-                          sizeof(CxJsonValue *) * parent->value.array.alloc)) {
-            return -1;
-        }
-    }
-
-    parent->value.array.array[parent->value.array.size++] = v;
-
-    return 0;
-}
-
 static int add_to_parent(CxJson *p, CxJsonValue *parent, CxJsonValue *v) {
     if (!parent) {
         return -1; // shouldn't happen but who knows
     }
 
-    int ret = 0;
     if (parent->type == CX_JSON_OBJECT) {
         if (!p->value_name || p->value_name_len == 0) {
             return -1;
@@ -645,14 +581,12 @@
         newvalue.name = valuename;
         newvalue.value = v;
 
-        ret = obj_add_value(p, parent, newvalue);
+        return cx_array_simple_add(parent->value.object.values, newvalue);
     } else if (parent->type == CX_JSON_ARRAY) {
-        ret = array_add_value(p, parent, v);
+        return cx_array_simple_add(parent->value.array.array, v);
     } else {
-        ret = -1; // should also never happen
+        return -1; // should also never happen
     }
-
-    return ret;
 }
 
 
@@ -792,7 +726,7 @@
     switch (value->type) {
         case CX_JSON_OBJECT: {
             CxJsonObject obj = value->value.object;
-            for (size_t i = 0; i < obj.size; i++) {
+            for (size_t i = 0; i < obj.values_size; i++) {
                 cxJsonValueFree(obj.values[i].value);
                 free(obj.values[i].name);
             }
@@ -801,7 +735,7 @@
         }
         case CX_JSON_ARRAY: {
             CxJsonArray array = value->value.array;
-            for (size_t i = 0; i < array.size; i++) {
+            for (size_t i = 0; i < array.array_size; i++) {
                 cxJsonValueFree(array.array[i]);
             }
             free(array.array);
@@ -819,7 +753,7 @@
 }
 
 CxJsonValue *cxJsonArrGet(CxJsonValue *value, size_t index) {
-    if (index >= value->value.array.size) {
+    if (index >= value->value.array.array_size) {
         return &cx_json_value_nothing;
     }
     return value->value.array.array[index];
@@ -828,7 +762,7 @@
 CxJsonValue *cxJsonObjGet(CxJsonValue *value, const char *name) {
     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->size; i++) {
+    for (size_t i = 0; i < obj->values_size; i++) {
         // TODO: we might want to store names as cxmutstr
         if (0 == strcmp(name, obj->values[i].name)) {
             return obj->values[i].value;

mercurial