• source navigation  • diff markup  • identifier search  • freetext search  • 

Sources/json-c/tests/test_visit.c

  1 #include <assert.h>
  2 #include <stddef.h>
  3 #include <stdio.h>
  4 #include <stdlib.h>
  5 #include <string.h>
  6 
  7 #include "json.h"
  8 #include "json_tokener.h"
  9 #include "json_visit.h"
 10 
 11 static json_c_visit_userfunc emit_object;
 12 static json_c_visit_userfunc skip_arrays;
 13 static json_c_visit_userfunc pop_and_stop;
 14 static json_c_visit_userfunc err_on_subobj2;
 15 static json_c_visit_userfunc pop_array;
 16 static json_c_visit_userfunc stop_array;
 17 static json_c_visit_userfunc err_return;
 18 
 19 int main(void)
 20 {
 21         MC_SET_DEBUG(1);
 22 
 23         const char *input = "{\
 24                 \"obj1\": 123,\
 25                 \"obj2\": {\
 26                         \"subobj1\": \"aaa\",\
 27                         \"subobj2\": \"bbb\",\
 28                         \"subobj3\": [ \"elem1\", \"elem2\", true ],\
 29                 },\
 30                 \"obj3\": 1.234,\
 31                 \"obj4\": [ true, false, null ]\
 32         }";
 33 
 34         json_object *jso = json_tokener_parse(input);
 35         printf("jso.to_string()=%s\n", json_object_to_json_string(jso));
 36 
 37         int rv;
 38         rv = json_c_visit(jso, 0, emit_object, NULL);
 39         printf("json_c_visit(emit_object)=%d\n", rv);
 40         printf("================================\n\n");
 41 
 42         rv = json_c_visit(jso, 0, skip_arrays, NULL);
 43         printf("json_c_visit(skip_arrays)=%d\n", rv);
 44         printf("================================\n\n");
 45 
 46         rv = json_c_visit(jso, 0, pop_and_stop, NULL);
 47         printf("json_c_visit(pop_and_stop)=%d\n", rv);
 48         printf("================================\n\n");
 49 
 50         rv = json_c_visit(jso, 0, err_on_subobj2, NULL);
 51         printf("json_c_visit(err_on_subobj2)=%d\n", rv);
 52         printf("================================\n\n");
 53 
 54         rv = json_c_visit(jso, 0, pop_array, NULL);
 55         printf("json_c_visit(pop_array)=%d\n", rv);
 56         printf("================================\n\n");
 57 
 58         rv = json_c_visit(jso, 0, stop_array, NULL);
 59         printf("json_c_visit(stop_array)=%d\n", rv);
 60         printf("================================\n\n");
 61 
 62         rv = json_c_visit(jso, 0, err_return, NULL);
 63         printf("json_c_visit(err_return)=%d\n", rv);
 64         printf("================================\n\n");
 65 
 66         json_object_put(jso);
 67 
 68         return 0;
 69 }
 70 
 71 static int emit_object(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
 72                        size_t *jso_index, void *userarg)
 73 {
 74         printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
 75                (jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
 76                json_object_to_json_string(jso));
 77         return JSON_C_VISIT_RETURN_CONTINUE;
 78 }
 79 
 80 static int skip_arrays(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
 81                        size_t *jso_index, void *userarg)
 82 {
 83         (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
 84         if (json_object_get_type(jso) == json_type_array)
 85                 return JSON_C_VISIT_RETURN_SKIP;
 86         return JSON_C_VISIT_RETURN_CONTINUE;
 87 }
 88 
 89 static int pop_and_stop(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
 90                         size_t *jso_index, void *userarg)
 91 {
 92         (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
 93         if (jso_key != NULL && strcmp(jso_key, "subobj1") == 0)
 94         {
 95                 printf("POP after handling subobj1\n");
 96                 return JSON_C_VISIT_RETURN_POP;
 97         }
 98         if (jso_key != NULL && strcmp(jso_key, "obj3") == 0)
 99         {
100                 printf("STOP after handling obj3\n");
101                 return JSON_C_VISIT_RETURN_STOP;
102         }
103         return JSON_C_VISIT_RETURN_CONTINUE;
104 }
105 
106 static int err_on_subobj2(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
107                           size_t *jso_index, void *userarg)
108 {
109         (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
110         if (jso_key != NULL && strcmp(jso_key, "subobj2") == 0)
111         {
112                 printf("ERROR after handling subobj1\n");
113                 return JSON_C_VISIT_RETURN_ERROR;
114         }
115         return JSON_C_VISIT_RETURN_CONTINUE;
116 }
117 
118 static int pop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
119                      size_t *jso_index, void *userarg)
120 {
121         (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
122         if (jso_index != NULL && (*jso_index == 0))
123         {
124                 printf("POP after handling array[0]\n");
125                 return JSON_C_VISIT_RETURN_POP;
126         }
127         return JSON_C_VISIT_RETURN_CONTINUE;
128 }
129 
130 static int stop_array(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
131                       size_t *jso_index, void *userarg)
132 {
133         (void)emit_object(jso, flags, parent_jso, jso_key, jso_index, userarg);
134         if (jso_index != NULL && (*jso_index == 0))
135         {
136                 printf("STOP after handling array[1]\n");
137                 return JSON_C_VISIT_RETURN_STOP;
138         }
139         return JSON_C_VISIT_RETURN_CONTINUE;
140 }
141 
142 static int err_return(json_object *jso, int flags, json_object *parent_jso, const char *jso_key,
143                       size_t *jso_index, void *userarg)
144 {
145         printf("flags: 0x%x, key: %s, index: %ld, value: %s\n", flags,
146                (jso_key ? jso_key : "(null)"), (jso_index ? (long)*jso_index : -1L),
147                json_object_to_json_string(jso));
148         return 100;
149 }
150 

This page was automatically generated by LXR 0.3.1.  •  OpenWrt