simplify parsing of array and object elements

Fri, 01 Nov 2024 18:16:32 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 01 Nov 2024 18:16:32 +0100
changeset 967
a58f602ed2fe
parent 966
1aa7ec3e46e7
child 968
b5814aac3a76

simplify parsing of array and object elements

relates to #431

src/cx/json.h file | annotate | diff | comparison | revisions
src/json.c file | annotate | diff | comparison | revisions
tests/Makefile file | annotate | diff | comparison | revisions
--- a/src/cx/json.h	Fri Nov 01 17:35:42 2024 +0100
+++ b/src/cx/json.h	Fri Nov 01 18:16:32 2024 +0100
@@ -38,6 +38,7 @@
 
 #include "common.h"
 #include "string.h"
+#include "array_list.h"
 
 #ifdef __cplusplus
 extern "C" {
@@ -146,15 +147,11 @@
 };
 
 struct cx_json_array_s {
-    CxJsonValue **array;
-    size_t alloc;
-    size_t size;
+    CX_ARRAY_DECLARE(CxJsonValue*, array);
 };
 
 struct cx_json_object_s {
-    CxJsonObjValue *values;
-    size_t alloc;
-    size_t size;
+    CX_ARRAY_DECLARE(CxJsonObjValue, values);
 };
 
 struct cx_json_obj_value_s {
@@ -280,7 +277,7 @@
 
 __attribute__((__nonnull__))
 static inline size_t cxJsonArrSize(CxJsonValue *value) {
-    return value->value.array.size;
+    return value->value.array.array_size;
 }
 
 __attribute__((__nonnull__, __returns_nonnull__))
--- 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;
--- a/tests/Makefile	Fri Nov 01 17:35:42 2024 +0100
+++ b/tests/Makefile	Fri Nov 01 18:16:32 2024 +0100
@@ -85,7 +85,8 @@
 
 $(TEST_DIR)/test_json$(OBJ_EXT): test_json.c ../src/cx/test.h \
  ../src/cx/common.h ../src/cx/json.h ../src/cx/string.h \
- ../src/cx/allocator.h
+ ../src/cx/allocator.h ../src/cx/array_list.h ../src/cx/list.h \
+ ../src/cx/collection.h ../src/cx/iterator.h ../src/cx/compare.h
 	@echo "Compiling $<"
 	$(CC) -o $@ $(CFLAGS) -c $<
 

mercurial