8 * An example of reading JSON from stdin and printing its content to stdout.
9 * The output looks like YAML, but I'm not sure if it's really compatible.
12 static int dump(const char *js, jsmntok_t *t, size_t count, int indent) {
17 if (t->type == JSMN_PRIMITIVE) {
18 printf("%.*s", t->end - t->start, js+t->start);
20 } else if (t->type == JSMN_STRING) {
21 printf("'%.*s'", t->end - t->start, js+t->start);
23 } else if (t->type == JSMN_OBJECT) {
26 for (i = 0; i < t->size; i++) {
27 for (k = 0; k < indent; k++) printf(" ");
28 j += dump(js, t+1+j, count-j, indent+1);
30 j += dump(js, t+1+j, count-j, indent+1);
34 } else if (t->type == JSMN_ARRAY) {
37 for (i = 0; i < t->size; i++) {
38 for (k = 0; k < indent-1; k++) printf(" ");
40 j += dump(js, t+1+j, count-j, indent+1);
49 FILE *fh = fopen("../../../../data/test.map", "r");
63 /* Allocate some tokens as a start */
64 tok = malloc(sizeof(*tok) * tokcount);
66 fprintf(stderr, "malloc(): errno=%d\n", errno);
71 /* Read another chunk */
72 r = fread(buf, 1, sizeof(buf), fh);
74 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
78 if (eof_expected != 0) {
81 fprintf(stderr, "fread(): unexpected EOF\n");
86 js = realloc(js, jslen + r + 1);
88 fprintf(stderr, "realloc(): errno=%d\n", errno);
91 strncpy(js + jslen, buf, r);
95 r = jsmn_parse(&p, js, jslen, tok, tokcount);
97 if (r == JSMN_ERROR_NOMEM) {
98 tokcount = tokcount * 2;
99 tok = realloc(tok, sizeof(*tok) * tokcount);
101 fprintf(stderr, "realloc(): errno=%d\n", errno);
107 dump(js, tok, p.toknext, 0);