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_int(
const struct jsontree_context *js_ctx,
int value)
94 buf[l--] =
'0' + (value % 10);
96 }
while(value > 0 && l >= 0);
98 while(++l <
sizeof(buf)) {
99 js_ctx->putchar(buf[l]);
104 jsontree_setup(
struct jsontree_context *js_ctx,
struct jsontree_value *root,
105 int (* putchar)(
int))
107 js_ctx->values[0] = root;
108 js_ctx->putchar = putchar;
110 jsontree_reset(js_ctx);
114 jsontree_reset(
struct jsontree_context *js_ctx)
117 js_ctx->index[0] = 0;
121 jsontree_path_name(
const struct jsontree_context *js_ctx,
int depth)
123 if(depth < js_ctx->depth && js_ctx->values[depth]->type == JSON_TYPE_OBJECT) {
124 return ((
struct jsontree_object *)js_ctx->values[depth])->
125 pairs[js_ctx->index[depth]].name;
131 jsontree_print_next(
struct jsontree_context *js_ctx)
133 struct jsontree_value *v;
136 v = js_ctx->values[js_ctx->depth];
140 case JSON_TYPE_OBJECT:
141 case JSON_TYPE_ARRAY: {
142 struct jsontree_array *o = (
struct jsontree_array *)v;
143 struct jsontree_value *ov;
145 index = js_ctx->index[js_ctx->depth];
147 js_ctx->putchar(v->type);
148 js_ctx->putchar(
'\n');
150 if(index >= o->count) {
151 js_ctx->putchar(
'\n');
152 js_ctx->putchar(v->type + 2);
158 js_ctx->putchar(
',');
159 js_ctx->putchar(
'\n');
161 if(v->type == JSON_TYPE_OBJECT) {
162 jsontree_write_string(js_ctx,
163 ((
struct jsontree_object *)o)->pairs[index].name);
164 js_ctx->putchar(
':');
165 ov = ((
struct jsontree_object *)o)->pairs[index].value;
167 ov = o->values[index];
171 js_ctx->index[js_ctx->depth] = 0;
172 js_ctx->values[js_ctx->depth] = ov;
176 case JSON_TYPE_STRING:
177 jsontree_write_string(js_ctx, ((
struct jsontree_string *)v)->value);
181 jsontree_write_int(js_ctx, ((
struct jsontree_int *)v)->value);
184 case JSON_TYPE_CALLBACK: {
185 struct jsontree_callback *callback;
187 callback = (
struct jsontree_callback *)v;
188 if(js_ctx->index[js_ctx->depth] == 0) {
190 js_ctx->callback_state = 0;
192 if(callback->output ==
NULL) {
193 jsontree_write_string(js_ctx,
"");
194 }
else if(callback->output(js_ctx)) {
196 js_ctx->index[js_ctx->depth]++;
203 PRINTF(
"\nError: Illegal json type:'%c'\n", v->type);
207 if(js_ctx->depth > 0) {
209 js_ctx->index[js_ctx->depth]++;
215 static struct jsontree_value *
216 find_next(
struct jsontree_context *js_ctx)
218 struct jsontree_value *v;
222 v = js_ctx->values[js_ctx->depth];
226 case JSON_TYPE_OBJECT:
227 case JSON_TYPE_ARRAY: {
228 struct jsontree_array *o = (
struct jsontree_array *)v;
229 struct jsontree_value *ov;
231 index = js_ctx->index[js_ctx->depth];
232 if(index >= o->count) {
237 if(v->type == JSON_TYPE_OBJECT) {
238 ov = ((
struct jsontree_object *)o)->pairs[index].value;
240 ov = o->values[index];
244 js_ctx->index[js_ctx->depth] = 0;
245 js_ctx->values[js_ctx->depth] = ov;
254 if(js_ctx->depth > 0) {
256 js_ctx->index[js_ctx->depth]++;
263 struct jsontree_value *
264 jsontree_find_next(
struct jsontree_context *js_ctx,
int type)
266 struct jsontree_value *v;
268 while((v = find_next(js_ctx)) !=
NULL && v->type != type &&
269 js_ctx->path < js_ctx->depth) {
272 js_ctx->callback_state = 0;
273 return js_ctx->path < js_ctx->depth ? v :
NULL;
#define NULL
The null pointer.