42 #include "jsonparse.h" 48 #define PRINTF(...) printf(__VA_ARGS__) 55 jsontree_write_atom(
const struct jsontree_context *js_ctx,
const char *text)
60 while(*text !=
'\0') {
61 js_ctx->putchar(*text++);
67 jsontree_write_string(
const struct jsontree_context *js_ctx,
const char *text)
71 while(*text !=
'\0') {
73 js_ctx->putchar(
'\\');
75 js_ctx->putchar(*text++);
82 jsontree_write_uint(
const struct jsontree_context *js_ctx,
unsigned int value)
89 buf[l--] =
'0' + (value % 10);
91 }
while(value > 0 && l >= 0);
93 while(++l <
sizeof(buf)) {
94 js_ctx->putchar(buf[l]);
99 jsontree_write_int(
const struct jsontree_context *js_ctx,
int value)
102 js_ctx->putchar(
'-');
106 jsontree_write_uint(js_ctx, value);
110 jsontree_setup(
struct jsontree_context *js_ctx,
struct jsontree_value *root,
111 int (* putchar)(
int))
113 js_ctx->values[0] = root;
114 js_ctx->putchar = putchar;
116 jsontree_reset(js_ctx);
120 jsontree_reset(
struct jsontree_context *js_ctx)
123 js_ctx->index[0] = 0;
127 jsontree_path_name(
const struct jsontree_context *js_ctx,
int depth)
129 if(depth < js_ctx->depth && js_ctx->values[depth]->type == JSON_TYPE_OBJECT) {
130 return ((
struct jsontree_object *)js_ctx->values[depth])->
131 pairs[js_ctx->index[depth]].name;
137 jsontree_print_next(
struct jsontree_context *js_ctx)
139 struct jsontree_value *v;
145 v = js_ctx->values[js_ctx->depth];
149 case JSON_TYPE_OBJECT:
150 case JSON_TYPE_ARRAY: {
151 struct jsontree_array *o = (
struct jsontree_array *)v;
152 struct jsontree_value *ov;
154 index = js_ctx->index[js_ctx->depth];
156 js_ctx->putchar(v->type);
158 js_ctx->putchar(
'\n');
161 if(index >= o->count) {
163 js_ctx->putchar(
'\n');
164 indent = js_ctx->depth;
166 js_ctx->putchar(
' ');
167 js_ctx->putchar(
' ');
170 js_ctx->putchar(v->type + 2);
176 js_ctx->putchar(
',');
178 js_ctx->putchar(
'\n');
183 indent = js_ctx->depth + 1;
185 js_ctx->putchar(
' ');
186 js_ctx->putchar(
' ');
190 if(v->type == JSON_TYPE_OBJECT) {
191 jsontree_write_string(js_ctx,
192 ((
struct jsontree_object *)o)->pairs[index].name);
193 js_ctx->putchar(
':');
195 js_ctx->putchar(
' ');
197 ov = ((
struct jsontree_object *)o)->pairs[index].value;
199 ov = o->values[index];
201 if(js_ctx->depth >= JSONTREE_MAX_DEPTH - 1) {
206 js_ctx->index[js_ctx->depth] = 0;
207 js_ctx->values[js_ctx->depth] = ov;
211 case JSON_TYPE_STRING:
212 jsontree_write_string(js_ctx, ((
struct jsontree_string *)v)->value);
216 jsontree_write_uint(js_ctx, ((
struct jsontree_uint *)v)->value);
220 jsontree_write_int(js_ctx, ((
struct jsontree_int *)v)->value);
223 case JSON_TYPE_CALLBACK: {
224 struct jsontree_callback *callback;
226 callback = (
struct jsontree_callback *)v;
227 if(js_ctx->index[js_ctx->depth] == 0) {
229 js_ctx->callback_state = 0;
231 if(callback->output == NULL) {
232 jsontree_write_string(js_ctx,
"");
233 }
else if(callback->output(js_ctx)) {
235 js_ctx->index[js_ctx->depth]++;
240 case JSON_TYPE_S8PTR:
241 jsontree_write_int(js_ctx, *((int8_t *)((
struct jsontree_ptr *)v)->value));
244 case JSON_TYPE_U8PTR:
245 jsontree_write_uint(js_ctx, *((uint8_t *)((
struct jsontree_ptr *)v)->value));
248 case JSON_TYPE_S16PTR:
249 jsontree_write_int(js_ctx, *((int16_t *)((
struct jsontree_ptr *)v)->value));
252 case JSON_TYPE_U16PTR:
253 jsontree_write_uint(js_ctx, *((uint16_t *)((
struct jsontree_ptr *)v)->value));
256 case JSON_TYPE_S32PTR:
257 jsontree_write_int(js_ctx, *((int32_t *)((
struct jsontree_ptr *)v)->value));
260 case JSON_TYPE_U32PTR:
261 jsontree_write_uint(js_ctx, *((uint32_t *)((
struct jsontree_ptr *)v)->value));
266 PRINTF(
"\nError: Illegal json type:'%c'\n", v->type);
270 if(js_ctx->depth > 0) {
272 js_ctx->index[js_ctx->depth]++;
278 static struct jsontree_value *
279 find_next(
struct jsontree_context *js_ctx)
281 struct jsontree_value *v;
285 v = js_ctx->values[js_ctx->depth];
289 case JSON_TYPE_OBJECT:
290 case JSON_TYPE_ARRAY: {
291 struct jsontree_array *o = (
struct jsontree_array *)v;
292 struct jsontree_value *ov;
294 index = js_ctx->index[js_ctx->depth];
295 if(index >= o->count) {
300 if(v->type == JSON_TYPE_OBJECT) {
301 ov = ((
struct jsontree_object *)o)->pairs[index].value;
303 ov = o->values[index];
305 if(js_ctx->depth >= JSONTREE_MAX_DEPTH - 1) {
310 js_ctx->index[js_ctx->depth] = 0;
311 js_ctx->values[js_ctx->depth] = ov;
320 if(js_ctx->depth > 0) {
322 js_ctx->index[js_ctx->depth]++;
329 struct jsontree_value *
330 jsontree_find_next(
struct jsontree_context *js_ctx,
int type)
332 struct jsontree_value *v;
334 while((v = find_next(js_ctx)) != NULL && v->type != type &&
335 js_ctx->path < js_ctx->depth) {
338 js_ctx->callback_state = 0;
339 return js_ctx->path < js_ctx->depth ? v : NULL;