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