Annotation of libelwix/example/test_json.c, revision 1.5
1.2 misho 1: #include <stdio.h>
2: #include <string.h>
3: #include <unistd.h>
4: #include <fcntl.h>
5: #include <errno.h>
6: #include <sys/types.h>
7: #include <elwix.h>
8:
9:
10: int
11: run_simple(json_t *json)
12: {
13: static const char *teststr = "{\"user\": \"johndoe\", { \"meow\", \"\", \"\aaa\"}, \"admin\": false, \"uid\": 1000, \"objects\" : { \"a\":\"1\", \"b\" : \"2\" ,\"c\" : \"3\", , \"oho\"},\n"
1.5 ! misho 14: "\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"], \"testend\":{\"x\" : \"8\", \"y\":\"7\", \"z\" : \"9\"}}";
1.2 misho 15: // static const char *teststr = "{}\n[]\n\"sdfsdf\"";
16: int i, ret;
17: jtok_t *tok, toks[36];
18: ait_val_t *v;
19: char *str;
20: array_t *arr;
21:
22: json_init(json, 0);
23: ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
24: if (ret == -1) {
25: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
26: return 1;
27: } else
28: printf("We are need from %d tokens\n", ret);
29: json_init(json, 1);
30: ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
31: if (ret == -1) {
32: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
33: return 1;
34: } else
35: printf("(strict) We are need from %d tokens\n", ret);
36: json_init(json, 0);
37: ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
38: if (ret == -1) {
39: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
40: return 1;
41: } else
42: printf("We parsed %d tokens next=%lu\n", ret, json->h_next);
43: memset(toks, 0, sizeof toks);
44: json_init(json, 1);
45: ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
46: if (ret == -1) {
47: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
48: return 1;
49: } else
50: printf("(strict) We parsed %d tokens next=%lu\n", ret, json->h_next);
51:
1.3 misho 52: if (!(tok = json_findbykey(teststr, "boza s kosmi", J_UNDEF, toks, ret)))
1.2 misho 53: printf("Key=\"boza s kosmi\" not found!\n");
1.3 misho 54: tok = json_findbykey(teststr, "user", J_UNDEF, toks, ret);
1.2 misho 55: if (tok) {
56: v = json_token2val(teststr, tok);
57: printf("Key=\"user\" data parent=%ld idx=%ld size=%ld type=%d %s\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type, AIT_GET_STR(v));
58: ait_freeVar(&v);
59: }
1.3 misho 60: tok = json_findbykey(teststr, "admin", J_VALUE, toks, ret);
1.2 misho 61: if (tok) {
62: str = json_token2str(teststr, tok);
63: printf("Key=\"admin\" data parent=%ld idx=%ld size=%ld type=%d %s\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type, str);
64: e_free(str);
65: }
1.3 misho 66: tok = json_findbykey(teststr, "uid", J_UNDEF, toks, ret);
1.2 misho 67: if (tok) {
68: printf("Key=\"uid\" data parent=%ld idx=%ld size=%ld type=%d %ld\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type,
69: json_token2num(teststr, tok));
70: arr = json_token2array(teststr, tok);
71: for (i = 0; i < array_Size(arr); i++)
72: printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
73: ait_freeVars(&arr);
74: }
75:
1.3 misho 76: tok = json_findbykey(teststr, "groups", J_UNDEF, toks, ret);
1.2 misho 77: if (tok) {
78: str = json_token2str(teststr, tok);
79: arr = json_token2array(teststr, tok);
80: printf("Key=\"groups\" data parent=%ld idx=%ld size=%ld type=%d %s\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type, str);
81: e_free(str);
82: for (i = 0; i < array_Size(arr); i++)
83: printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
84: ait_freeVars(&arr);
85: }
86:
1.3 misho 87: tok = json_findbykey(teststr, "objects", J_UNDEF, toks, ret);
1.2 misho 88: if (tok) {
89: str = json_token2str(teststr, tok);
90: arr = json_token2array(teststr, tok);
91: printf("Key=\"objects\" data parent=%ld idx=%ld size=%ld type=%d %s\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type, str);
92: e_free(str);
93: for (i = 0; i < array_Size(arr); i++)
94: printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
95: ait_freeVars(&arr);
96: }
97:
1.3 misho 98: tok = json_findbykey(teststr, "testend", J_UNDEF, toks, ret);
1.2 misho 99: if (tok) {
100: str = json_token2str(teststr, tok);
101: arr = json_token2array(teststr, tok);
102: printf("Key=\"testend\" data parent=%ld idx=%ld size=%ld type=%d %s\n", tok->tok_parent, tok->tok_idx, tok->tok_size, tok->tok_type, str);
103: e_free(str);
104: for (i = 0; i < array_Size(arr); i++)
105: printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
106: ait_freeVars(&arr);
107: }
108:
109: return 0;
110: }
111:
112: static int
113: dump(const char *jstr, jtok_t *toks, int toksnum, int indent)
114: {
115: register int i, j, k;
116:
117: if (!toksnum)
118: return 0;
119:
120: if (toks->tok_type == J_VALUE) {
121: printf("%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
122: return 1;
123: } else if (toks->tok_type == J_STRING) {
124: printf("%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
125: return 1;
126: } else if (toks->tok_type == J_OBJECT) {
127: printf("\n");
128: for (j = i = 0; i < json_toksize(toks); i++) {
129: for (k = 0; k < indent; k++)
130: printf(" ");
131: j += dump(jstr, toks + j + 1, toksnum - j, indent + 1);
132: printf(": ");
133: j += dump(jstr, toks + j + 1, toksnum - j, indent + 1);
134: printf("\n");
135: }
136: return j + 1;
137: } else if (toks->tok_type == J_ARRAY) {
138: printf("\n");
139: for (j = i = 0; i < json_toksize(toks); i++) {
140: for (k = 0; k < indent - 1; k++)
141: printf(" ");
142: printf(" - ");
143: j += dump(jstr, toks + j + 1, toksnum - j, indent + 1);
144: printf("\n");
145: }
146: return j + 1;
147: }
148:
149: return 0;
150: }
151:
152: int
153: run_dump(json_t *json, char **args)
154: {
155: int rlen, toksnum, wait4eof = 0;
156: char buf[BUFSIZ];
157: ait_val_t v;
158: jtok_t *toks, *t;
159: FILE *f;
160:
161: json_init(json, 0);
162: f = fopen(args[1], "r");
163: if (!f) {
164: printf("fopen(%s) #%d - %s\n", *args, errno, strerror(errno));
165: return 1;
166: }
167:
168: toks = e_malloc(sizeof(jtok_t));
169: if (!toks) {
170: fclose(f);
171: return 1;
172: } else
173: toksnum = 1;
174: AIT_SET_STRSIZ(&v, 1);
175: while (42) {
176: memset(buf, 0, sizeof buf);
177: rlen = fread(buf, 1, sizeof buf, f);
178: switch (rlen) {
179: case -1:
180: printf("Error:: fread() #%d - %s\n", errno, strerror(errno));
181: AIT_FREE_VAL(&v);
182: e_free(toks);
183: fclose(f);
184: return 2;
185: case 0:
186: if (wait4eof)
187: return 0;
188: printf("Error:: Unexpected termination!\n");
189: AIT_FREE_VAL(&v);
190: e_free(toks);
191: fclose(f);
192: return 3;
193: default:
194: AIT_SET_STRCAT(&v, buf);
195: break;
196: }
197: again:
198: rlen = json_parse(json, AIT_GET_STR(&v), AIT_LEN(&v), toks, toksnum);
199: if (rlen == (u_int) -1) {
200: if (elwix_GetErrno() == J_ERR_NOMEM) {
201: toksnum *= 2;
202: t = e_realloc(toks, sizeof(jtok_t) * toksnum);
203: if (t) {
204: toks = t;
205: goto again;
206: }
207: }
208: } else {
209: dump(AIT_GET_STR(&v), toks, json->h_next, 0);
210: wait4eof = 1;
211: }
212: }
213: AIT_FREE_VAL(&v);
214: e_free(toks);
215: fclose(f);
216:
217: return 0;
218: }
219:
220: int
221: run_make()
222: {
223: char str[BUFSIZ];
224: ait_val_t *v;
225: array_t *arr;
226:
227: arr = ait_allocVars(4);
228: v = ait_getVars(&arr, 0);
229: AIT_SET_I16(v, 4444);
230: v = ait_getVars(&arr, 1);
231: AIT_SET_STR(v, "sezam");
232: v = ait_getVars(&arr, 2);
233: AIT_SET_U8(v, 0x1c);
234: v = ait_getVars(&arr, 3);
235: AIT_SET_STR(v, "0x45af");
236:
237: json_add_begin_object(str, sizeof str, 42);
238: json_add_pair(str, sizeof str, 42, "boza", "s kosmi");
239: json_add_comma(str, sizeof str, 42);
240: json_add_string(str, sizeof str, 0, "number");
241: json_add_colon(str, sizeof str, 42);
242: json_add_value(str, sizeof str, 0, 1234567890);
243: json_add_comma(str, sizeof str, 42);
244: json_add_string(str, sizeof str, 0, "obj__");
245: json_add_colon(str, sizeof str, 42);
246: json_add_begin_object(str, sizeof str, 42);
247: json_add_pair(str, sizeof str, 42, "subkey", "val0");
248: json_add_end_object(str, sizeof str, 42);
249: json_add_comma(str, sizeof str, 42);
250: json_add_string(str, sizeof str, 0, "arr4e");
251: json_add_colon(str, sizeof str, 42);
252: json_add_array(str, sizeof str, 42, arr);
253: json_add_end_object(str, sizeof str, 42);
254: ait_freeVars(&arr);
255:
256: printf("%s\n", str);
257: return 0;
258: }
259:
260: int
1.4 misho 261: run_stdin(json_t *json)
262: {
263: char szJSON[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 };
264: jtok_t *tok, toks[1024];
1.5 ! misho 265: int ret, scope;
1.4 misho 266: char *str;
267:
268: printf("Input JSON> ");
269: fgets(szJSON, sizeof szJSON, stdin);
270: printf("Readed JSON> %s", szJSON);
271:
272: json_init(json, 0);
273: ret = json_parse(json, szJSON, strlen(szJSON), toks, sizeof toks/sizeof *toks);
274: if (ret == -1) {
275: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
276: return 1;
277: } else
278: printf("We parsed %d tokens next=%lu\n", ret, json->h_next);
279:
280: printf("dump=%d\n", json_dump(stdout, szJSON, toks, json->h_next, 0));
281:
282: tok = json_findbykey(szJSON, "aaa", J_STRING, toks, ret);
283: if (tok) {
284: str = json_token2str(szJSON, tok);
285: if (str) {
286: printf("str=%s\n", str);
287: json_freestr(str);
288: }
289: }
1.5 ! misho 290:
! 291: printf("Default scope=%ld, scope for \"zzz\"=%ld\n",
! 292: json_objscope(NULL, szJSON, toks, ret),
! 293: (scope = json_objscope("zzz", szJSON, toks, ret)));
! 294:
! 295: tok = json_findbykeyatscope(0, szJSON, "t1", J_UNDEF, toks, ret);
! 296: if (tok) {
! 297: str = json_token2str(szJSON, tok);
! 298: if (str) {
! 299: printf("default scope t1=%s\n", str);
! 300: json_freestr(str);
! 301: }
! 302: }
! 303: tok = json_findbykeyatscope(scope, szJSON, "t1", J_UNDEF, toks, ret);
! 304: if (tok) {
! 305: str = json_token2str(szJSON, tok);
! 306: if (str) {
! 307: printf("scope=%ld object zzz t1=%s\n", scope, str);
! 308: json_freestr(str);
! 309: }
! 310: }
! 311:
! 312: scope = json_objscope("z", szJSON, toks, ret);
! 313: tok = json_findbykeyatscope(scope, szJSON, "t1", J_UNDEF, toks, ret);
! 314: if (tok) {
! 315: str = json_token2str(szJSON, tok);
! 316: if (str) {
! 317: printf("scope=%ld object z t1=%s\n", scope, str);
! 318: json_freestr(str);
! 319: }
! 320: }
! 321:
1.4 misho 322: return 0;
323: }
324:
325: int
1.2 misho 326: main(int argc, char **argv)
327: {
328: json_t json;
329:
330: if (argc < 2)
1.4 misho 331: return run_stdin(&json);
332: else if (argc < 3)
1.2 misho 333: return run_simple(&json);
1.4 misho 334: else if (argc < 4)
1.2 misho 335: return run_make();
336: else
337: return run_dump(&json, argv);
338:
339: return 0;
340: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>