]> git.ipfire.org Git - thirdparty/json-c.git/commitdiff
update json_visit testcase 522/head
authordota17 <chenguopingdota@163.com>
Tue, 31 Dec 2019 03:27:09 +0000 (11:27 +0800)
committerdota17 <chenguopingdota@163.com>
Thu, 2 Jan 2020 08:41:37 +0000 (16:41 +0800)
tests/test_visit.c
tests/test_visit.expected

index 4046133c0b2a20772c3bcd1187c4193d24d04029..53df1909c2e60f99a46d5e4e1c6b168580dfbe97 100644 (file)
@@ -12,6 +12,9 @@ static json_c_visit_userfunc emit_object;
 static json_c_visit_userfunc skip_arrays;
 static json_c_visit_userfunc pop_and_stop;
 static json_c_visit_userfunc err_on_subobj2;
+static json_c_visit_userfunc pop_array;
+static json_c_visit_userfunc stop_array;
+static json_c_visit_userfunc err_return;
 
 int main(void)
 {
@@ -48,6 +51,18 @@ int main(void)
        printf("json_c_visit(err_on_subobj2)=%d\n", rv);
        printf("================================\n\n");
 
+       rv = json_c_visit(jso, 0, pop_array, NULL);
+       printf("json_c_visit(pop_array)=%d\n", rv);
+       printf("================================\n\n");
+
+       rv = json_c_visit(jso, 0, stop_array, NULL);
+       printf("json_c_visit(stop_array)=%d\n", rv);
+       printf("================================\n\n");
+
+       rv = json_c_visit(jso, 0, err_return, NULL);
+       printf("json_c_visit(err_return)=%d\n", rv);
+       printf("================================\n\n");
+
        json_object_put(jso);
 
        return 0;
@@ -111,3 +126,43 @@ static int err_on_subobj2(json_object *jso, int flags,
        return JSON_C_VISIT_RETURN_CONTINUE;
 }
 
+static int pop_array(json_object *jso, int flags,
+                     json_object *parent_jso,
+                     const char *jso_key,
+                     size_t *jso_index, void *userarg)
+{
+       (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
+       if (jso_index != NULL && (*jso_index == 0))
+       {
+               printf("POP after handling array[0]\n");
+               return JSON_C_VISIT_RETURN_POP;
+       }
+       return JSON_C_VISIT_RETURN_CONTINUE;
+}
+
+static int stop_array(json_object *jso, int flags,
+                     json_object *parent_jso,
+                     const char *jso_key,
+                     size_t *jso_index, void *userarg)
+{
+       (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
+       if (jso_index != NULL && (*jso_index == 0))
+       {
+               printf("STOP after handling array[1]\n");
+               return JSON_C_VISIT_RETURN_STOP;
+       }
+       return JSON_C_VISIT_RETURN_CONTINUE;
+}
+
+static int err_return(json_object *jso, int flags,
+                     json_object *parent_jso,
+                     const char *jso_key,
+                     size_t *jso_index, void *userarg)
+{
+       printf("flags: 0x%x, key: %s, index: %ld, value: %s\n",
+              flags,
+              (jso_key ? jso_key : "(null)"),
+              (jso_index ? (long)*jso_index : -1L),
+              json_object_to_json_string(jso));
+       return 100;
+}
index 2f05622b9c6a9164b56a0347e9f46d7aa799c01e..5f323174bba6da8fef8666f1c5b183b78633f01a 100644 (file)
@@ -53,3 +53,37 @@ ERROR after handling subobj1
 json_c_visit(err_on_subobj2)=-1
 ================================
 
+flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
+flags: 0x0, key: obj1, index: -1, value: 123
+flags: 0x0, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
+flags: 0x0, key: subobj1, index: -1, value: "aaa"
+flags: 0x0, key: subobj2, index: -1, value: "bbb"
+flags: 0x0, key: subobj3, index: -1, value: [ "elem1", "elem2", true ]
+flags: 0x0, key: (null), index: 0, value: "elem1"
+POP after handling array[0]
+flags: 0x2, key: subobj3, index: -1, value: [ "elem1", "elem2", true ]
+flags: 0x2, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
+flags: 0x0, key: obj3, index: -1, value: 1.234
+flags: 0x0, key: obj4, index: -1, value: [ true, false, null ]
+flags: 0x0, key: (null), index: 0, value: true
+POP after handling array[0]
+flags: 0x2, key: obj4, index: -1, value: [ true, false, null ]
+flags: 0x2, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
+json_c_visit(pop_array)=0
+================================
+
+flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
+flags: 0x0, key: obj1, index: -1, value: 123
+flags: 0x0, key: obj2, index: -1, value: { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }
+flags: 0x0, key: subobj1, index: -1, value: "aaa"
+flags: 0x0, key: subobj2, index: -1, value: "bbb"
+flags: 0x0, key: subobj3, index: -1, value: [ "elem1", "elem2", true ]
+flags: 0x0, key: (null), index: 0, value: "elem1"
+STOP after handling array[1]
+json_c_visit(stop_array)=0
+================================
+
+flags: 0x0, key: (null), index: -1, value: { "obj1": 123, "obj2": { "subobj1": "aaa", "subobj2": "bbb", "subobj3": [ "elem1", "elem2", true ] }, "obj3": 1.234, "obj4": [ true, false, null ] }
+json_c_visit(err_return)=-1
+================================
+