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"
14: "\"groups\": [\"users\", \"\", \"wheel\", \"audio\", \"video\"], \"testend\":{\"x\" : \"8\", \"y\":\"7\", \"z\" : \"9\"}}";
15: int i, ret;
16: jtok_t *tok, toks[36];
17: ait_val_t *v;
18: char *str;
19: array_t *arr;
20:
21: json_init(json, 0);
22: ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
23: if (ret == -1) {
24: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
25: return 1;
26: } else
27: printf("We are need from %d tokens\n", ret);
28: json_init(json, 1);
29: ret = json_parse(json, teststr, strlen(teststr), NULL, 0);
30: if (ret == -1) {
31: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
32: return 1;
33: } else
34: printf("(strict) We are need from %d tokens\n", ret);
35: json_init(json, 0);
36: ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
37: if (ret == -1) {
38: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
39: return 1;
40: } else
41: printf("We parsed %d tokens next=%lu\n", ret, json->h_next);
42: memset(toks, 0, sizeof toks);
43: json_init(json, 1);
44: ret = json_parse(json, teststr, strlen(teststr), toks, sizeof toks/sizeof *toks);
45: if (ret == -1) {
46: printf("Error:: #%d - %s\n", elwix_GetErrno(), elwix_GetError());
47: return 1;
48: } else
49: printf("(strict) We parsed %d tokens next=%lu\n", ret, json->h_next);
50:
51: if (!(tok = json_findbykey(teststr, "boza s kosmi", toks, ret)))
52: printf("Key=\"boza s kosmi\" not found!\n");
53: tok = json_findbykey(teststr, "user", toks, ret);
54: v = json_token2val(teststr, tok);
55: 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));
56: ait_freeVar(&v);
57: tok = json_findbykey(teststr, "admin", toks, ret);
58: str = json_token2str(teststr, tok);
59: 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);
60: e_free(str);
61: tok = json_findbykey(teststr, "uid", toks, ret);
62: 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,
63: json_token2num(teststr, tok));
64: arr = json_token2array(teststr, tok);
65: for (i = 0; i < array_Size(arr); i++)
66: printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
67: ait_freeVars(&arr);
68:
69: tok = json_findbykey(teststr, "groups", toks, ret);
70: str = json_token2str(teststr, tok);
71: arr = json_token2array(teststr, tok);
72: 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);
73: e_free(str);
74: for (i = 0; i < array_Size(arr); i++)
75: printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
76: ait_freeVars(&arr);
77:
78: tok = json_findbykey(teststr, "objects", toks, ret);
79: str = json_token2str(teststr, tok);
80: arr = json_token2array(teststr, tok);
81: 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);
82: e_free(str);
83: for (i = 0; i < array_Size(arr); i++)
84: printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
85: ait_freeVars(&arr);
86:
87: tok = json_findbykey(teststr, "testend", toks, ret);
88: str = json_token2str(teststr, tok);
89: arr = json_token2array(teststr, tok);
90: 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);
91: e_free(str);
92: for (i = 0; i < array_Size(arr); i++)
93: printf("arr[%d]=%s\n", i, AIT_GET_STR(array(arr, i, ait_val_t*)));
94: ait_freeVars(&arr);
95:
96: return 0;
97: }
98:
99: int
100: run_dump(json_t *json)
101: {
102: int rlen, toksnum, wait4eof = 0;
103: char buf[BUFSIZ];
104: ait_val_t v;
105: jtok_t *toks, *t;
106:
107: toks = e_malloc(sizeof(jtok_t));
108: if (!toks)
109: return 1;
110: else
111: toksnum = 1;
112: AIT_SET_STRSIZ(&v, 1);
113: while (42) {
114: memset(buf, 0, sizeof buf);
115: rlen = fread(buf, 1, sizeof buf, stdin);
116: switch (rlen) {
117: case -1:
118: printf("Error:: fread() #%d - %s\n", errno, strerror(errno));
119: AIT_FREE_VAL(&v);
120: e_free(toks);
121: return 2;
122: case 0:
123: if (wait4eof)
124: return 0;
125: printf("Error:: Unexpected termination!\n");
126: AIT_FREE_VAL(&v);
127: e_free(toks);
128: return 3;
129: default:
130: AIT_SET_STRCAT(&v, buf);
131: break;
132: }
133: again:
134: rlen = json_parse(json, AIT_GET_STR(&v), AIT_LEN(&v), toks, toksnum);
135: if (rlen == (u_int) -1) {
136: if (elwix_GetErrno() == J_ERR_NOMEM) {
137: toksnum *= 2;
138: t = e_realloc(toks, sizeof(jtok_t) * toksnum);
139: if (t) {
140: toks = t;
141: goto again;
142: }
143: }
144: } else {
145: dump(AIT_GET_STR(&v), toks, json->h_next, 0);
146: wait4eof = 1;
147: }
148: }
149: AIT_FREE_VAL(&v);
150: e_free(toks);
151:
152: return 0;
153: }
154:
155: int
156: main(int argc, char **argv)
157: {
158: json_t json;
159:
160: if (argc < 2)
161: return run_simple(&json);
162: else
163: return run_dump(&json);
164:
165: return 0;
166: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>