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 //what the fuck is going on here?
18 if (t->type == JSMN_PRIMITIVE) {
19 printf("%.*s", t->end - t->start, js+t->start);
20 printf("%s\n", js+t->start);
22 } else if (t->type == JSMN_STRING) {
23 printf("'%.*s'", t->end - t->start, js+t->start);
25 } else if (t->type == JSMN_OBJECT) {
28 for (i = 0; i < t->size; i++) {
29 for (k = 0; k < indent; k++) printf(" ");
30 j += dump(js, t+1+j, count-j, indent+1);
32 j += dump(js, t+1+j, count-j, indent+1);
36 } else if (t->type == JSMN_ARRAY) {
39 for (i = 0; i < t->size; i++) {
40 for (k = 0; k < indent-1; k++) printf(" ");
42 j += dump(js, t+1+j, count-j, indent+1);
50 static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
51 if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
52 strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
59 FILE *fh = fopen("../../../../data/test.map", "r");
73 /* Allocate some tokens as a start */
74 tok = malloc(sizeof(*tok) * tokcount);
76 fprintf(stderr, "malloc(): errno=%d\n", errno);
81 /* Read another chunk */
82 r = fread(buf, 1, sizeof(buf), fh);
84 fprintf(stderr, "fread(): %d, errno=%d\n", r, errno);
88 if (eof_expected != 0) {
91 fprintf(stderr, "fread(): unexpected EOF\n");
96 js = realloc(js, jslen + r + 1);
98 fprintf(stderr, "realloc(): errno=%d\n", errno);
101 strncpy(js + jslen, buf, r);
105 r = jsmn_parse(&p, js, jslen, tok, tokcount);
107 if (r == JSMN_ERROR_NOMEM) {
108 tokcount = tokcount * 2;
109 tok = realloc(tok, sizeof(*tok) * tokcount);
111 fprintf(stderr, "realloc(): errno=%d\n", errno);
117 dump(js, tok, p.toknext, 0);