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