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 jsoneq(const char *json, jsmntok_t *tok, const char *s) {
13 if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
14 strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
20 static int dump(const char *js, jsmntok_t *t, size_t count, int indent) {
25 if (t->type == JSMN_PRIMITIVE) {
26 printf("%.*s", t->end - t->start, js+t->start);
28 } else if (t->type == JSMN_STRING) {
29 printf("'%.*s'", t->end - t->start, js+t->start);
30 /*if (jsoneq(js, t, "image") == 0) {
31 printf("- image: %.*s\n", t->end-t->start,
35 } else if (t->type == JSMN_OBJECT) {
38 for (i = 0; i < t->size; i++) {
39 //for (k = 0; k < indent; k++) printf("\t");
40 j += dump(js, t+1+j, count-j, indent+1);
42 j += dump(js, t+1+j, count-j, indent+1);
46 } else if (t->type == JSMN_ARRAY) {
49 for (i = 0; i < t->size; i++) {
50 //for (k = 0; k < indent-1; k++) printf("\t");
52 j += dump(js, t+1+j, count-j, indent+1);
71 FILE *fh = fopen("../../../../data/test.map", "r");
76 /* Allocate some tokens as a start */
77 tok = malloc(sizeof(*tok) * tokcount);
79 fprintf(stderr, "malloc(): errno=%d\n", errno);
84 /* Read another chunk */
85 r = fread(buf, 1, sizeof(buf), fh);
87 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
91 if (eof_expected != 0) {
94 fprintf(stderr, "fread(): unexpected EOF\n");
99 js = realloc(js, jslen + r + 1);
101 fprintf(stderr, "realloc(): errno=%d\n", errno);
104 strncpy(js + jslen, buf, r);
108 r = jsmn_parse(&p, js, jslen, tok, tokcount);
110 if (r == JSMN_ERROR_NOMEM) {
111 tokcount = tokcount * 2;
112 tok = realloc(tok, sizeof(*tok) * tokcount);
114 fprintf(stderr, "realloc(): errno=%d\n", errno);
120 dump(js, tok, p.toknext, 0);
121 //fprintf(stdout, "[[[[%d]]]]\n", sizeof(tok));
122 printf("[\n%d\n]", jslen);