xref: /freebsd/contrib/ntp/libjsmn/example/simple.c (revision b5ff185e19f6013ca565b2a15bc2d6abce933f46)
1*276da39aSCy Schubert #include <stdio.h>
2*276da39aSCy Schubert #include <string.h>
3*276da39aSCy Schubert #include "../jsmn.h"
4*276da39aSCy Schubert 
5*276da39aSCy Schubert /*
6*276da39aSCy Schubert  * A small example of jsmn parsing when JSON structure is known and number of
7*276da39aSCy Schubert  * tokens is predictable.
8*276da39aSCy Schubert  */
9*276da39aSCy Schubert 
10*276da39aSCy Schubert const char *JSON_STRING =
11*276da39aSCy Schubert 	"{\"user\": \"johndoe\", \"admin\": false, \"uid\": 1000,\n  "
12*276da39aSCy Schubert 	"\"groups\": [\"users\", \"wheel\", \"audio\", \"video\"]}";
13*276da39aSCy Schubert 
jsoneq(const char * json,jsmntok_t * tok,const char * s)14*276da39aSCy Schubert static int jsoneq(const char *json, jsmntok_t *tok, const char *s) {
15*276da39aSCy Schubert 	if (tok->type == JSMN_STRING && (int) strlen(s) == tok->end - tok->start &&
16*276da39aSCy Schubert 			strncmp(json + tok->start, s, tok->end - tok->start) == 0) {
17*276da39aSCy Schubert 		return 0;
18*276da39aSCy Schubert 	}
19*276da39aSCy Schubert 	return -1;
20*276da39aSCy Schubert }
21*276da39aSCy Schubert 
main()22*276da39aSCy Schubert int main() {
23*276da39aSCy Schubert 	int i;
24*276da39aSCy Schubert 	int r;
25*276da39aSCy Schubert 	jsmn_parser p;
26*276da39aSCy Schubert 	jsmntok_t t[128]; /* We expect no more than 128 tokens */
27*276da39aSCy Schubert 
28*276da39aSCy Schubert 	jsmn_init(&p);
29*276da39aSCy Schubert 	r = jsmn_parse(&p, JSON_STRING, strlen(JSON_STRING), t, sizeof(t)/sizeof(t[0]));
30*276da39aSCy Schubert 	if (r < 0) {
31*276da39aSCy Schubert 		printf("Failed to parse JSON: %d\n", r);
32*276da39aSCy Schubert 		return 1;
33*276da39aSCy Schubert 	}
34*276da39aSCy Schubert 
35*276da39aSCy Schubert 	/* Assume the top-level element is an object */
36*276da39aSCy Schubert 	if (r < 1 || t[0].type != JSMN_OBJECT) {
37*276da39aSCy Schubert 		printf("Object expected\n");
38*276da39aSCy Schubert 		return 1;
39*276da39aSCy Schubert 	}
40*276da39aSCy Schubert 
41*276da39aSCy Schubert 	/* Loop over all keys of the root object */
42*276da39aSCy Schubert 	for (i = 1; i < r; i++) {
43*276da39aSCy Schubert 		if (jsoneq(JSON_STRING, &t[i], "user") == 0) {
44*276da39aSCy Schubert 			/* We may use strndup() to fetch string value */
45*276da39aSCy Schubert 			printf("- User: %.*s\n", t[i+1].end-t[i+1].start,
46*276da39aSCy Schubert 					JSON_STRING + t[i+1].start);
47*276da39aSCy Schubert 			i++;
48*276da39aSCy Schubert 		} else if (jsoneq(JSON_STRING, &t[i], "admin") == 0) {
49*276da39aSCy Schubert 			/* We may additionally check if the value is either "true" or "false" */
50*276da39aSCy Schubert 			printf("- Admin: %.*s\n", t[i+1].end-t[i+1].start,
51*276da39aSCy Schubert 					JSON_STRING + t[i+1].start);
52*276da39aSCy Schubert 			i++;
53*276da39aSCy Schubert 		} else if (jsoneq(JSON_STRING, &t[i], "uid") == 0) {
54*276da39aSCy Schubert 			/* We may want to do strtol() here to get numeric value */
55*276da39aSCy Schubert 			printf("- UID: %.*s\n", t[i+1].end-t[i+1].start,
56*276da39aSCy Schubert 					JSON_STRING + t[i+1].start);
57*276da39aSCy Schubert 			i++;
58*276da39aSCy Schubert 		} else if (jsoneq(JSON_STRING, &t[i], "groups") == 0) {
59*276da39aSCy Schubert 			int j;
60*276da39aSCy Schubert 			printf("- Groups:\n");
61*276da39aSCy Schubert 			if (t[i+1].type != JSMN_ARRAY) {
62*276da39aSCy Schubert 				continue; /* We expect groups to be an array of strings */
63*276da39aSCy Schubert 			}
64*276da39aSCy Schubert 			for (j = 0; j < t[i+1].size; j++) {
65*276da39aSCy Schubert 				jsmntok_t *g = &t[i+j+2];
66*276da39aSCy Schubert 				printf("  * %.*s\n", g->end - g->start, JSON_STRING + g->start);
67*276da39aSCy Schubert 			}
68*276da39aSCy Schubert 			i += t[i+1].size + 1;
69*276da39aSCy Schubert 		} else {
70*276da39aSCy Schubert 			printf("Unexpected key: %.*s\n", t[i].end-t[i].start,
71*276da39aSCy Schubert 					JSON_STRING + t[i].start);
72*276da39aSCy Schubert 		}
73*276da39aSCy Schubert 	}
74*276da39aSCy Schubert 	return 0;
75*276da39aSCy Schubert }
76