add dbuJsonToList

Tue, 06 Jan 2026 20:50:10 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 06 Jan 2026 20:50:10 +0100
changeset 45
a8b1df52e63e
parent 44
3bac2715ccb9
child 46
96e139ab57f2

add dbuJsonToList

dbutils/dbutils/json.h file | annotate | diff | comparison | revisions
dbutils/json.c file | annotate | diff | comparison | revisions
--- a/dbutils/dbutils/json.h	Mon Jan 05 21:37:39 2026 +0100
+++ b/dbutils/dbutils/json.h	Tue Jan 06 20:50:10 2026 +0100
@@ -48,6 +48,7 @@
 CxJsonValue* dbuObjectToJson2(DBUClass *type, void *obj, const CxAllocator *a, bool setNull, bool forceString);
 
 void* dbuJsonToObject(DBUClass *type, const CxAllocator *a, CxJsonValue *value);
+CxList* dbuJsonToList(DBUClass *type, const CxAllocator *a, CxJsonValue *value);
 
 
 #ifdef __cplusplus
--- a/dbutils/json.c	Mon Jan 05 21:37:39 2026 +0100
+++ b/dbutils/json.c	Tue Jan 06 20:50:10 2026 +0100
@@ -274,3 +274,35 @@
     int error;
     return jsonToObj(type, a, value, 0, &error);
 }
+
+static CxList* jsonToArray(DBUClass *type, const CxAllocator *a, CxJsonValue *value, size_t elmSize, int *error) {
+    if(!cxJsonIsArray(value)) {
+        *error = 1;
+        return NULL;
+    }
+    
+    CxList *list = cxArrayListCreate(a, elmSize, value->array.size);
+    if(!list) {
+        *error = 3;
+    }
+    
+    for(size_t i=0;i<value->array.size;i++) {
+        void *elm = jsonToObj(type, a, value->array.data[i], 1, error);
+        if(!elm) {
+            cxListFree(list);
+            return NULL;
+        }
+        if(cxListAdd(list, elm)) {
+            *error = 3;
+            cxListFree(list);
+            return NULL;
+        }
+    }
+    
+    return list;
+}
+
+CxList* dbuJsonToList(DBUClass *type, const CxAllocator *a, CxJsonValue *value) {
+    int error;
+    return jsonToArray(type, a, value, CX_STORE_POINTERS, &error);
+}

mercurial