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];
203 js_ctx->index[js_ctx->depth] = 0;
204 js_ctx->values[js_ctx->depth] = ov;
208 case JSON_TYPE_STRING:
209 jsontree_write_string(js_ctx, ((
struct jsontree_string *)v)->value);
213 jsontree_write_uint(js_ctx, ((
struct jsontree_uint *)v)->value);
217 jsontree_write_int(js_ctx, ((
struct jsontree_int *)v)->value);
220 case JSON_TYPE_CALLBACK: {
221 struct jsontree_callback *callback;
223 callback = (
struct jsontree_callback *)v;
224 if(js_ctx->index[js_ctx->depth] == 0) {
226 js_ctx->callback_state = 0;
228 if(callback->output == NULL) {
229 jsontree_write_string(js_ctx,
"");
230 }
else if(callback->output(js_ctx)) {
232 js_ctx->index[js_ctx->depth]++;
237 case JSON_TYPE_S8PTR:
238 jsontree_write_int(js_ctx, *((int8_t *)((
struct jsontree_ptr *)v)->value));
241 case JSON_TYPE_U8PTR:
242 jsontree_write_uint(js_ctx, *((uint8_t *)((
struct jsontree_ptr *)v)->value));
245 case JSON_TYPE_S16PTR:
246 jsontree_write_int(js_ctx, *((int16_t *)((
struct jsontree_ptr *)v)->value));
249 case JSON_TYPE_U16PTR:
250 jsontree_write_uint(js_ctx, *((uint16_t *)((
struct jsontree_ptr *)v)->value));
253 case JSON_TYPE_S32PTR:
254 jsontree_write_int(js_ctx, *((int32_t *)((
struct jsontree_ptr *)v)->value));
257 case JSON_TYPE_U32PTR:
258 jsontree_write_uint(js_ctx, *((uint32_t *)((
struct jsontree_ptr *)v)->value));
263 PRINTF(
"\nError: Illegal json type:'%c'\n", v->type);
267 if(js_ctx->depth > 0) {
269 js_ctx->index[js_ctx->depth]++;
275 static struct jsontree_value *
276 find_next(
struct jsontree_context *js_ctx)
278 struct jsontree_value *v;
282 v = js_ctx->values[js_ctx->depth];
286 case JSON_TYPE_OBJECT:
287 case JSON_TYPE_ARRAY: {
288 struct jsontree_array *o = (
struct jsontree_array *)v;
289 struct jsontree_value *ov;
291 index = js_ctx->index[js_ctx->depth];
292 if(index >= o->count) {
297 if(v->type == JSON_TYPE_OBJECT) {
298 ov = ((
struct jsontree_object *)o)->pairs[index].value;
300 ov = o->values[index];
304 js_ctx->index[js_ctx->depth] = 0;
305 js_ctx->values[js_ctx->depth] = ov;
314 if(js_ctx->depth > 0) {
316 js_ctx->index[js_ctx->depth]++;
323 struct jsontree_value *
324 jsontree_find_next(
struct jsontree_context *js_ctx,
int type)
326 struct jsontree_value *v;
328 while((v = find_next(js_ctx)) != NULL && v->type != type &&
329 js_ctx->path < js_ctx->depth) {
332 js_ctx->callback_state = 0;
333 return js_ctx->path < js_ctx->depth ? v : NULL;