add more json deserialization tests

Mon, 05 Jan 2026 21:37:39 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 05 Jan 2026 21:37:39 +0100
changeset 44
3bac2715ccb9
parent 43
73c1ced3a620
child 45
a8b1df52e63e

add more json deserialization tests

dbutils/json.c file | annotate | diff | comparison | revisions
test/json.c file | annotate | diff | comparison | revisions
test/json.h file | annotate | diff | comparison | revisions
test/main.c file | annotate | diff | comparison | revisions
--- a/dbutils/json.c	Sun Jan 04 18:09:59 2026 +0100
+++ b/dbutils/json.c	Mon Jan 05 21:37:39 2026 +0100
@@ -227,14 +227,24 @@
                 if(field->builder.add) {
                     for(int i=0;i<child->array.size;i++) {
                         CxJsonValue *elm = child->array.data[i];
+                        void *elm_obj = NULL;
+                        bool add_elm = false;
                         if(cxJsonIsObject(elm)) {
-                            void *elm_obj = jsonToObj(field->builder.type, a, elm, depth+1, error);
+                            elm_obj = jsonToObj(field->builder.type, a, elm, depth+1, error);
                             if(!elm_obj) {
                                 free(obj); // TODO: improve obj cleanup
                                 return NULL;
                             }
+                            add_elm = true;
+                            
+                        } else if(cxJsonIsLiteral(elm) && elm->literal == CX_JSON_NULL) {
+                            add_elm = true;
+                        }
+                        
+                        if(add_elm) {
                             if(field->builder.add(&field->builder, obj, field->builder.type, elm_obj, NULL, a)) {
                                 free(obj); // TODO: improve obj cleanup
+                                free(elm_obj);
                                 *error = 3;
                                 return NULL;
                             }
--- a/test/json.c	Sun Jan 04 18:09:59 2026 +0100
+++ b/test/json.c	Mon Jan 05 21:37:39 2026 +0100
@@ -397,7 +397,7 @@
         
         CX_TEST_ASSERT(obj);
         CX_TEST_ASSERT(!cx_strcmp(obj->test, "hello world"));
-        CX_TEST_ASSERT(obj->test2List);
+        CX_TEST_ASSERT(obj->test2List != NULL);
         CX_TEST_ASSERT(cxListSize(obj->test2List) == 2);
         
         Test2 *elm0 = cxListAt(obj->test2List, 0);
@@ -415,3 +415,54 @@
         free(obj);
     }
 }
+
+CX_TEST(testJsonToObjectWithEmptyArray) {
+    const char *jsonStr =
+        "{"
+        "\"test\":\"hello world 2\","
+        "\"test2List\":[ ]"
+        "}";
+    
+    CxJsonValue *json;
+    cxJsonFromString(NULL, jsonStr, &json);
+    
+    CX_TEST_DO {
+        Test5 *obj = dbuJsonToObject(test5_class, NULL, json);
+        
+        CX_TEST_ASSERT(obj);
+        CX_TEST_ASSERT(!cx_strcmp(obj->test, "hello world 2"));
+        CX_TEST_ASSERT(obj->test2List == NULL || cxListSize(obj->test2List) == 0);
+        
+        free(obj);
+    }
+}
+
+CX_TEST(testJsonToObjectWithNullArray) {
+    const char *jsonStr =
+        "{"
+        "\"test\":\"hello world 3\","
+        "\"test2List\":[ null, null, null ]"
+        "}";
+    
+    CxJsonValue *json;
+    cxJsonFromString(NULL, jsonStr, &json);
+    
+    CX_TEST_DO {
+        Test5 *obj = dbuJsonToObject(test5_class, NULL, json);
+        
+        CX_TEST_ASSERT(obj);
+        CX_TEST_ASSERT(!cx_strcmp(obj->test, "hello world 3"));
+        CX_TEST_ASSERT(obj->test2List != NULL);
+        CX_TEST_ASSERT(cxListSize(obj->test2List) == 3);
+        
+        Test2 *elm0 = cxListAt(obj->test2List, 0);
+        Test2 *elm1 = cxListAt(obj->test2List, 1);
+        Test2 *elm2 = cxListAt(obj->test2List, 2);
+        
+        CX_TEST_ASSERT(elm0 == NULL);
+        CX_TEST_ASSERT(elm1 == NULL);
+        CX_TEST_ASSERT(elm2 == NULL);
+        
+        free(obj);
+    }
+}
--- a/test/json.h	Sun Jan 04 18:09:59 2026 +0100
+++ b/test/json.h	Mon Jan 05 21:37:39 2026 +0100
@@ -47,6 +47,8 @@
 CX_TEST(testJsonToSimpleObject);
 CX_TEST(testJsonToObjectWithObjChild);
 CX_TEST(testJsonToObjectWithArray);
+CX_TEST(testJsonToObjectWithEmptyArray);
+CX_TEST(testJsonToObjectWithNullArray);
 
 
 #ifdef __cplusplus
--- a/test/main.c	Sun Jan 04 18:09:59 2026 +0100
+++ b/test/main.c	Mon Jan 05 21:37:39 2026 +0100
@@ -147,6 +147,8 @@
     cx_test_register(suite, testJsonToSimpleObject);
     cx_test_register(suite, testJsonToObjectWithObjChild);
     cx_test_register(suite, testJsonToObjectWithArray);
+    cx_test_register(suite, testJsonToObjectWithEmptyArray);
+    cx_test_register(suite, testJsonToObjectWithNullArray);
     
     cx_test_run_stdout(suite);
     

mercurial