test/json.c

changeset 28
e46f9f254fcd
parent 26
dc36aa437249
child 29
b8c826c720f3
--- a/test/json.c	Tue Dec 09 19:40:08 2025 +0100
+++ b/test/json.c	Wed Dec 10 18:36:02 2025 +0100
@@ -48,8 +48,27 @@
     double d;
 } Test1;
 
+typedef struct Test2 {
+    char *name;
+    int i;
+} Test2;
+
+typedef struct Test3 {
+    char *test3;
+    Test2 *test2;
+} Test3;
+
+typedef struct Test4 {
+    int id;
+    Test3 *test3;
+    Test2 *test2;
+} Test4;
+
 static DBUContext *ctx;
 static DBUClass *test1_class;
+static DBUClass *test2_class;
+static DBUClass *test3_class;
+static DBUClass *test4_class;
 
 int init_json_tests(void) {
     ctx = dbuContextCreate();
@@ -68,6 +87,19 @@
     dbuClassAdd(test1_class, Test1, bt);
     dbuClassAdd(test1_class, Test1, bf);
     dbuClassAdd(test1_class, Test1, d);
+    
+    test2_class = dbuRegisterClassWithoutPK(ctx, "test2", sizeof(Test2));
+    dbuClassAdd(test2_class, Test2, name);
+    dbuClassAdd(test2_class, Test2, i);
+    
+    test3_class = dbuRegisterClassWithoutPK(ctx, "test3", sizeof(Test3));
+    dbuClassAdd(test3_class, Test3, test3);
+    dbuClassAddObj(test3_class, "test2", offsetof(Test3, test2), test2_class);
+    
+    test4_class = dbuRegisterClassWithoutPK(ctx, "test4", sizeof(Test4));
+    dbuClassAdd(test4_class, Test4, id);
+    dbuClassAddObj(test4_class, "test2", offsetof(Test4, test2), test2_class);
+    dbuClassAddObj(test4_class, "test3", offsetof(Test4, test3), test3_class);
 }
 
 void cleanup_json_tests(void) {
@@ -94,7 +126,7 @@
         CxJsonValue *value = dbuObjectToJson(test1_class, &test, NULL);
         
         CX_TEST_ASSERT(value);
-        CX_TEST_ASSERT(value->type == CX_JSON_OBJECT);
+        CX_TEST_ASSERT(cxJsonIsObject(value));
         
         CxJsonValue *str = cxJsonObjGet(value, "str");
         CxJsonValue *str2 = cxJsonObjGet(value, "str2");
@@ -133,6 +165,77 @@
         CX_TEST_ASSERT(i64->value.integer == test.i64);
         CX_TEST_ASSERT(u64->value.integer == test.u64);
         CX_TEST_ASSERT(d->value.number < test.d + 0.1 && d->value.number > test.d - 0.1);
+        
+        cxJsonValueFree(value);
     }
 }
 
+CX_TEST(testObjectToJsonChildObj) {
+    Test2 t2_1;
+    t2_1.i = 12;
+    t2_1.name = "t2_1";
+    
+    Test2 t2_2;
+    t2_2.i = 45;
+    t2_2.name = "t2_2";
+    
+    Test3 t3;
+    t3.test3 = "t3";
+    t3.test2 = &t2_1;
+    
+    Test4 t4;
+    t4.id = 15;
+    t4.test2 = &t2_2;
+    t4.test3 = &t3;
+    
+    CX_TEST_DO {
+        CxJsonValue *value = dbuObjectToJson(test4_class, &t4, NULL);
+        
+        CX_TEST_ASSERT(value);
+        CX_TEST_ASSERT(cxJsonIsObject(value));
+        
+        CxJsonValue *v = cxJsonObjGet(value, "id");
+        CX_TEST_ASSERT(v);
+        CX_TEST_ASSERT(cxJsonIsInteger(v));
+        CX_TEST_ASSERT(v->value.integer == t4.id);
+        
+        v = cxJsonObjGet(value, "test2");
+        CX_TEST_ASSERT(v);
+        CX_TEST_ASSERT(cxJsonIsObject(v));
+        
+        CxJsonValue *s = cxJsonObjGet(v, "i");
+        CX_TEST_ASSERT(s);
+        CX_TEST_ASSERT(cxJsonIsInteger(s));
+        CX_TEST_ASSERT(s->value.integer == t2_2.i);
+        
+        s = cxJsonObjGet(v, "name");
+        CX_TEST_ASSERT(s);
+        CX_TEST_ASSERT(cxJsonIsString(s));
+        CX_TEST_ASSERT(!cx_strcmp(s->value.string, t2_2.name));
+        
+        v = cxJsonObjGet(value, "test3");
+        CX_TEST_ASSERT(v);
+        CX_TEST_ASSERT(cxJsonIsObject(v));
+        
+        s = cxJsonObjGet(v, "test3");
+        CX_TEST_ASSERT(s);
+        CX_TEST_ASSERT(cxJsonIsString(s));
+        CX_TEST_ASSERT(!cx_strcmp(s->value.string, t3.test3));
+        
+        s = cxJsonObjGet(v, "test2");
+        CX_TEST_ASSERT(s);
+        CX_TEST_ASSERT(cxJsonIsObject(s));
+        
+        CxJsonValue *x = cxJsonObjGet(s, "i");
+        CX_TEST_ASSERT(x);
+        CX_TEST_ASSERT(cxJsonIsInteger(x));
+        CX_TEST_ASSERT(x->value.integer == t2_1.i);
+        
+        x = cxJsonObjGet(s, "name");
+        CX_TEST_ASSERT(x);
+        CX_TEST_ASSERT(cxJsonIsString(x));
+        CX_TEST_ASSERT(!cx_strcmp(x->value.string, t2_1.name));
+        
+        cxJsonValueFree(value);
+    }
+}

mercurial