Annotation of libelwix/src/json.c, revision 1.11
1.2 misho 1: /*************************************************************************
2: * (C) 2017 AITNET ltd - Sofia/Bulgaria - <misho@aitnet.org>
3: * by Michael Pounov <misho@elwix.org>
4: *
5: * $Author: misho $
1.11 ! misho 6: * $Id: json.c,v 1.10.4.1 2024/12/04 17:46:50 misho Exp $
1.2 misho 7: *
8: **************************************************************************
9: The ELWIX and AITNET software is distributed under the following
10: terms:
11:
12: All of the documentation and software included in the ELWIX and AITNET
13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
14:
1.10 misho 15: Copyright 2004 - 2024
1.2 misho 16: by Michael Pounov <misho@elwix.org>. All rights reserved.
17:
18: Redistribution and use in source and binary forms, with or without
19: modification, are permitted provided that the following conditions
20: are met:
21: 1. Redistributions of source code must retain the above copyright
22: notice, this list of conditions and the following disclaimer.
23: 2. Redistributions in binary form must reproduce the above copyright
24: notice, this list of conditions and the following disclaimer in the
25: documentation and/or other materials provided with the distribution.
26: 3. All advertising materials mentioning features or use of this software
27: must display the following acknowledgement:
28: This product includes software developed by Michael Pounov <misho@elwix.org>
29: ELWIX - Embedded LightWeight unIX and its contributors.
30: 4. Neither the name of AITNET nor the names of its contributors
31: may be used to endorse or promote products derived from this software
32: without specific prior written permission.
33:
34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
37: ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44: SUCH DAMAGE.
45: */
46: #include "global.h"
47:
48:
49: /* JSON error strings */
50: const char *jerrstr[] = {
51: "No error",
52: "Not enough tokens were provided",
53: "Invalid character",
54: "JSON string isn't full",
55: "Invalid parameter",
56: NULL
57: };
58:
59:
60: /*
61: * json_init() - Initialize JSON handler
62: *
63: * @json = JSON handler, if there is NULL then dynamically will be allocated
64: * @jstrict = JSON strict mode, when we select strict mode every unquoted value is error
65: * return: =NULL error or !=NULL ready for use JSON handler and should be free with json_free()
66: */
67: json_t *
68: json_init(json_t * __restrict json, int jstrict)
69: {
70: json_t *j = json;
71:
72: if (!j) {
73: j = e_malloc(sizeof(json_t));
74: if (!j) {
75: LOGERR;
76: return NULL;
77: }
78: }
79:
80: memset(j, 0, sizeof(json_t));
81: j->h_parent = -1;
82: j->h_strict = jstrict;
83:
84: /* handler is dynamically allocated! */
85: if (!json)
86: j->h_alloc = j;
87:
88: return j;
89: }
90:
91: /*
92: * json_free() - Free JSON handler
93: *
94: * @json = JSON handler
95: * return: none
96: */
97: void
98: json_free(json_t * __restrict json)
99: {
100: if (json) {
101: if (json->h_alloc)
102: e_free(json);
103: else
104: memset(json, 0, sizeof(json_t));
105: }
106: }
107:
108: static jtok_t *
109: json_gettoken(json_t * __restrict json, jtok_t * __restrict jtoks, u_int toksnum)
110: {
111: jtok_t *tok;
112:
113: assert(json || !(!jtoks && toksnum));
114:
115: if (json->h_next >= toksnum) {
116: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
117: return NULL;
118: } else
119: tok = &jtoks[json->h_next];
120: tok->tok_idx = json->h_next++;
121: tok->tok_start = tok->tok_end = tok->tok_parent = -1;
122: tok->tok_size = 0;
123:
124: return tok;
125: }
126:
127: inline void
128: json_filltoken(jtok_t * __restrict tok, jtype_t type, long start, long end, long parent)
129: {
130: assert(tok);
131:
132: tok->tok_type = type;
133: tok->tok_start = start;
134: tok->tok_end = end;
135: tok->tok_parent = parent;
136: tok->tok_size = 0;
137: }
138:
139: static int
140: json_parse_string(json_t * __restrict json, const char *jstr, size_t jlen, jtok_t * __restrict jtoks, u_int toksnum)
141: {
142: jtok_t *tok;
143: u_long pos;
144: char ch;
145: register int i;
146:
147: assert(json || jstr || !(!jtoks && toksnum));
148:
149: for (pos = json->h_pos++; json->h_pos < jlen && jstr[json->h_pos]; json->h_pos++) {
150: ch = jstr[json->h_pos];
151:
152: if (ch == '\"') {
153: if (!jtoks)
154: return 0;
155: if (!(tok = json_gettoken(json, jtoks, toksnum))) {
156: json->h_pos = pos;
157: return -1;
158: } else
159: json_filltoken(tok, J_STRING, pos + 1, json->h_pos, json->h_parent);
160: return 0;
161: }
162:
163: if (ch == '\\' && json->h_pos + 1 < jlen) {
164: switch (jstr[++json->h_pos]) {
165: case '\"':
166: case '/':
167: case '\\':
168: case 'b':
169: case 'f':
170: case 'r':
171: case 'n':
172: case 't':
173: /* Allowed escaped symbols */
174: break;
175: case 'u':
176: /* Allows escaped symbol \uXXXX */
177: json->h_pos++;
178: for (i = 0; i < 4 && json->h_pos < jlen && jstr[json->h_pos]; i++, json->h_pos++) {
179: /* If it isn't a hex character we have an error */
180: if (!((jstr[json->h_pos] >= 48 && jstr[json->h_pos] <= 57) || /* 0-9 */
181: (jstr[json->h_pos] >= 65 && jstr[json->h_pos] <= 70) || /* A-F */
182: (jstr[json->h_pos] >= 97 && jstr[json->h_pos] <= 102))) { /* a-f */
183: json->h_pos = pos;
184: elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
185: return -1;
186: }
187: }
188: json->h_pos--;
189: break;
190: default:
191: /* Unexpected symbol */
192: json->h_pos = pos;
193: elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
194: return -1;
195: }
196: }
197: }
198:
199: json->h_pos = pos;
200: elwix_SetErr(J_ERR_PART, "%s", jerrstr[J_ERR_PART]);
201: return -1;
202: }
203:
204: static int
205: json_parse_value(json_t * __restrict json, const char *jstr, size_t jlen, jtok_t * __restrict jtoks, u_int toksnum)
206: {
207: jtok_t *tok;
208: u_long pos;
209:
210: assert(json || jstr || !(!jtoks && toksnum));
211:
212: for (pos = json->h_pos; json->h_pos < jlen && jstr[json->h_pos]; json->h_pos++) {
213: switch (jstr[json->h_pos]) {
214: case ':':
215: if (json->h_strict)
216: goto found;
217: break;
218: case '\t':
219: case '\r':
220: case '\n':
221: case ' ':
222: case ',':
223: case ']':
224: case '}':
225: goto found;
226: }
1.8 misho 227: if (jstr[json->h_pos] < 32 || (u_char) jstr[json->h_pos] > 127) {
1.2 misho 228: json->h_pos = pos;
229: elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
230: return -1;
231: }
232: }
233:
234: if (json->h_strict) {
235: json->h_pos = pos;
236: elwix_SetErr(J_ERR_PART, "%s", jerrstr[J_ERR_PART]);
237: return -1;
238: }
239: found:
240: if (jtoks) {
241: if (!(tok = json_gettoken(json, jtoks, toksnum))) {
242: json->h_pos = pos;
243: return -1;
244: } else
245: json_filltoken(tok, J_VALUE, pos, json->h_pos, json->h_parent);
246: }
247:
248: json->h_pos--;
249: return 0;
250: }
251:
252: /*
253: * json_parse() - Parse JSON string
254: *
255: * @json = JSON handler
256: * @jstr = JSON string
257: * @jlen = JSON string length
258: * @jtoks = Token array
259: * @toksnum = Token array size, return number of allocated tokens in array
260: * return: -1 error or number of found tokens
261: */
262: u_int
263: json_parse(json_t * __restrict json, const char *jstr, size_t jlen, jtok_t * __restrict jtoks, u_int toksnum)
264: {
265: register int i;
266: register u_int cx;
267: jtype_t type;
268: jtok_t *tok;
269: char ch;
270:
271: if (!json || !jstr || (!jtoks && toksnum)) {
272: elwix_SetErr(J_ERR_PARAM, "%s", jerrstr[J_ERR_PARAM]);
273: return (u_int) -1;
274: }
275:
276: for (cx = json->h_next; json->h_pos < jlen && jstr[json->h_pos]; json->h_pos++) {
277: switch ((ch = jstr[json->h_pos])) {
278: case '{':
279: case '[':
280: cx++; /* start new object/array token */
281: if (!jtoks)
282: break;
283:
284: tok = json_gettoken(json, jtoks, toksnum);
285: if (!tok)
286: return (u_int) -1;
287: if (json->h_parent != -1) {
288: jtoks[json->h_parent].tok_size++;
289: tok->tok_parent = json->h_parent;
290: }
291: tok->tok_type = (ch == '{' ? J_OBJECT : J_ARRAY);
292: tok->tok_start = json->h_pos;
293: json->h_parent = json->h_next - 1;
294: break;
295: case '}':
296: case ']':
297: if (!jtoks)
298: break;
299:
300: if (json->h_next < 1) {
301: elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
302: return (u_int) -1;
303: }
304:
305: type = (ch == '}' ? J_OBJECT : J_ARRAY);
306: tok = &jtoks[json->h_next - 1];
307: while (42) {
308: if (tok->tok_start != -1 && tok->tok_end == -1) {
309: if (tok->tok_type != type) {
310: elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
311: return (u_int) -1;
312: }
313: tok->tok_end = json->h_pos + 1;
314: json->h_parent = tok->tok_parent;
315: break;
316: }
317: if (tok->tok_parent == -1) {
318: if (tok->tok_type != type || json->h_parent == -1) {
319: elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
320: return (u_int) -1;
321: }
322: break;
323: }
324: tok = &jtoks[tok->tok_parent];
325: }
326: break;
327: case '\"':
328: if (json_parse_string(json, jstr, jlen, jtoks, toksnum) == -1)
329: return (u_int) -1;
330: cx++; /* start new string token */
331: if (jtoks && json->h_parent != -1)
332: jtoks[json->h_parent].tok_size++;
333: break;
334: case '\t':
335: case '\r':
336: case '\n':
337: case ' ':
338: /* whitespace, skip */
339: break;
340: case ':':
341: json->h_parent = json->h_next - 1;
342: break;
343: case ',':
344: if (jtoks && json->h_parent != -1 &&
345: jtoks[json->h_parent].tok_type != J_OBJECT &&
346: jtoks[json->h_parent].tok_type != J_ARRAY)
347: json->h_parent = jtoks[json->h_parent].tok_parent;
348: break;
349: case '-':
350: case '0':
351: case '1':
352: case '2':
353: case '3':
354: case '4':
355: case '5':
356: case '6':
357: case '7':
358: case '8':
359: case '9':
360: case 't':
361: case 'f':
362: case 'n':
363: if (json->h_strict) {
364: if (jtoks && json->h_parent != -1) {
365: /* they must not be keys of the object */
366: if (jtoks[json->h_parent].tok_type == J_OBJECT ||
367: (jtoks[json->h_parent].tok_type == J_STRING &&
368: jtoks[json->h_parent].tok_size)) {
369: elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
370: return (u_int) -1;
371: }
372: }
373:
374: if (json_parse_value(json, jstr, jlen, jtoks, toksnum) == -1)
375: return (u_int) -1;
376: cx++; /* start new value token */
377: if (jtoks && json->h_parent != -1)
378: jtoks[json->h_parent].tok_size++;
379: break;
380: }
381: default:
382: if (json->h_strict) {
383: elwix_SetErr(J_ERR_INVAL, "%s", jerrstr[J_ERR_INVAL]);
384: return (u_int) -1;
385: }
386:
387: if (json_parse_value(json, jstr, jlen, jtoks, toksnum) == -1)
388: return (u_int) -1;
389: cx++; /* start new value token */
390: if (jtoks && json->h_parent != -1)
391: jtoks[json->h_parent].tok_size++;
392: break;
393: }
394: }
395:
396: if (jtoks) {
397: for (i = json->h_next - 1; i >= 0; i--) {
398: /* unmatched opened object or array */
399: if (jtoks[i].tok_start != -1 && jtoks[i].tok_end == -1) {
400: elwix_SetErr(J_ERR_PART, "%s", jerrstr[J_ERR_PART]);
401: return (u_int) -1;
402: }
403: }
404: } else
405: cx++; /* increment needed tokens number for termination empty token */
406:
407: return cx;
408: }
409:
410: /*
411: * json_token2val() - Return token to AIT variable
412: *
413: * @jstr = JSON string
414: * @tok = Token for convert
415: * @return =NULL error or !=NULL allocated variable, after use should be ait_freeVar()
416: */
417: ait_val_t *
418: json_token2val(const char *jstr, jtok_t * __restrict tok)
419: {
420: ait_val_t *v = NULL;
421:
422: if (!jstr || !tok)
423: return NULL;
424:
425: v = ait_allocVar();
426: if (!v)
427: return NULL;
428:
429: AIT_SET_STRSIZ(v, json_toklen(tok));
1.10 misho 430: if (AIT_GET_STR(v))
431: strncpy(AIT_GET_STR(v), json_tokstr(jstr, tok), AIT_LEN(v) - 1);
432: else
433: ait_freeVar(&v);
1.2 misho 434:
435: return v;
436: }
437:
438: /*
439: * json_token2str() - Return token to string
440: *
441: * @jstr = JSON string
442: * @tok = Token for convert
1.5 misho 443: * @return =NULL error or !=NULL allocated str, after use should be json_freestr()|e_free()
1.2 misho 444: */
445: char *
446: json_token2str(const char *jstr, jtok_t * __restrict tok)
447: {
1.8 misho 448: char *s, *s2, *wrk, *str = NULL;
1.2 misho 449: size_t len;
450:
451: if (!jstr || !tok)
452: return NULL;
453:
1.8 misho 454:
1.2 misho 455: len = json_toklen(tok);
456: str = e_malloc(len + 1);
457: if (!str)
458: return NULL;
459: else {
1.8 misho 460: memset(str, 0, len + 1);
461:
462: wrk = e_strdup(json_tokstr(jstr, tok));
463: wrk[len] = 0;
464: for (s = wrk, s2 = str; *s; s++)
465: *s2++ = (*s != '\\') ? *s : *++s;
466: e_free(wrk);
1.2 misho 467: }
468:
469: return str;
470: }
471:
472: /*
473: * json_token2num() - Return token to numeric
474: *
475: * @jstr = JSON string
476: * @tok = Token for convert
477: * @return number
478: */
479: long
480: json_token2num(const char *jstr, jtok_t * __restrict tok)
481: {
482: long ret = 0;
483: char *str;
484:
485: str = json_token2str(jstr, tok);
486: if (!str)
487: return 0;
488:
489: ret = strtol(str, NULL, 0);
490: e_free(str);
491: return ret;
492: }
493:
494: /*
1.5 misho 495: * json_token2dbl() - Return token to double
496: *
497: * @jstr = JSON string
498: * @tok = Token for convert
499: * @return number
500: */
501: double
502: json_token2dbl(const char *jstr, jtok_t * __restrict tok)
503: {
504: double ret = 0;
505: char *str;
506:
507: str = json_token2str(jstr, tok);
508: if (!str)
509: return 0;
510:
511: ret = strtod(str, NULL);
1.6 misho 512: e_free(str);
513: return ret;
514: }
515:
516: /*
517: * json_token2bool() - Return token to bool int
518: *
519: * @jstr = JSON string
520: * @tok = Token for convert
521: * @return 0 for FALSE and !=0 for TRUE
522: */
523: int
524: json_token2bool(const char *jstr, jtok_t * __restrict tok)
525: {
526: double ret = 0;
527: char *str;
528:
529: str = json_token2str(jstr, tok);
530: if (!str)
531: return 0;
532:
533: switch (*str) {
534: case 't':
535: case 'T':
536: ret = 1;
537: break;
538: case 'f':
539: case 'F':
540: ret = 0;
541: break;
542: default:
543: ret = (int) strtol(str, NULL, 10);
544: break;
545: }
1.5 misho 546: e_free(str);
547: return ret;
548: }
549:
550: /*
551: * json_findbykey() - Find token data by key
1.2 misho 552: *
553: * @jstr = JSON string
554: * @key = Search key
1.4 misho 555: * @type = Search key for particular token type, if is J_UNDEF this mean any type
1.2 misho 556: * @toks = Parsed tokens
557: * @toksnum = Number of parsed tokens
558: * return: =NULL error or !=NULL data token found
559: */
560: jtok_t *
1.4 misho 561: json_findbykey(const char *jstr, const char *key, jtype_t type, jtok_t * __restrict toks, int toksnum)
1.2 misho 562: {
563: jtok_t *tok = NULL;
564: register int i;
565: int klen;
566:
567: if (!jstr || !key || !toks)
568: return NULL;
569: else
570: klen = strlen(key);
571:
572: for (i = 1; i < toksnum; i++) {
1.9 misho 573: if (toks[i].tok_type == J_STRING && toks[i].tok_size == 1 &&
1.2 misho 574: klen == toks[i].tok_end - toks[i].tok_start &&
575: !strncmp(jstr + toks[i].tok_start, key, klen)) {
1.4 misho 576: if (type != J_UNDEF) {
577: if (toks[i + 1].tok_type == type) {
578: tok = toks + i + 1;
579: break;
580: }
581: } else {
582: tok = toks + i + 1;
583: break;
584: }
1.2 misho 585: }
586: }
587:
588: return tok;
589: }
590:
591: /*
1.11 ! misho 592: * json_findbykeyatscope() - Find token data by key at particular scope
! 593: *
! 594: * @scope = Search at object scope, =0 main object scope
! 595: * @jstr = JSON string
! 596: * @key = Search key
! 597: * @type = Search key for particular token type, if is J_UNDEF this mean any type
! 598: * @toks = Parsed tokens
! 599: * @toksnum = Number of parsed tokens
! 600: * return: =NULL error or !=NULL data token found
! 601: */
! 602: jtok_t *
! 603: json_findbykeyatscope(long scope, const char *jstr, const char *key, jtype_t type, jtok_t * __restrict toks, int toksnum)
! 604: {
! 605: jtok_t *tok = NULL;
! 606: register int i;
! 607: int klen;
! 608:
! 609: if (!jstr || !key || !toks)
! 610: return NULL;
! 611: else
! 612: klen = strlen(key);
! 613:
! 614: for (i = 1; i < toksnum; i++) {
! 615: if (toks[i].tok_type == J_STRING && toks[i].tok_size == 1 &&
! 616: toks[i].tok_parent == scope &&
! 617: klen == toks[i].tok_end - toks[i].tok_start &&
! 618: !strncmp(jstr + toks[i].tok_start, key, klen)) {
! 619: if (type != J_UNDEF) {
! 620: if (toks[i + 1].tok_type == type) {
! 621: tok = toks + i + 1;
! 622: break;
! 623: }
! 624: } else {
! 625: tok = toks + i + 1;
! 626: break;
! 627: }
! 628: }
! 629: }
! 630:
! 631: return tok;
! 632: }
! 633:
! 634: /*
1.5 misho 635: * json_findbypos() - Find token by position on JSON string
636: *
637: * @pos = Offset from begin of JSON string
638: * @toks = Parsed tokens
639: * @toksnum = Number of parsed tokens
640: * return: =NULL error or !=NULL token found
641: */
642: jtok_t *
643: json_findbypos(u_long pos, jtok_t * __restrict toks, int toksnum)
644: {
645: jtok_t *tok = NULL;
646: register int i;
647:
1.10 misho 648: if (!toks)
1.5 misho 649: return NULL;
650:
651: for (i = 1; i < toksnum; i++)
652: if (pos <= toks[i].tok_end && pos >= toks[i].tok_start) {
653: tok = toks + i;
654: break;
655: }
656:
657: return tok;
658: }
659:
660: /*
1.2 misho 661: * json_token2array() - Convert token to string array
662: *
663: * @jstr = JSON string
664: * @tok = Token for convert
665: * return: =NULL error or !=NULL allocated array with string variables,
666: * after use should be ait_freeVars()
667: */
668: array_t *
669: json_token2array(const char *jstr, jtok_t * __restrict tok)
670: {
671: array_t *arr = NULL;
1.5 misho 672: register int i, j;
1.2 misho 673: int siz;
674: ait_val_t *v;
675: jtok_t *t;
676:
677: if (!jstr || !tok)
678: return NULL;
679:
680: siz = tok->tok_size;
681: if (!siz && json_toktype(tok) != J_ARRAY && json_toktype(tok) != J_OBJECT)
682: siz++;
683:
684: arr = ait_allocVars(siz);
685: if (!arr)
686: return NULL;
687:
688: if (tok->tok_type == J_STRING || tok->tok_type == J_VALUE) {
689: v = ait_getVars(&arr, 0);
690: AIT_SET_STRSIZ(v, json_toklen(tok) + 1);
1.10 misho 691: if (AIT_GET_STR(v)) {
692: json_tokstrcpy(AIT_GET_STR(v), jstr, tok);
693: } else {
694: ait_freeVar(&v);
695: }
1.2 misho 696: } else if (tok->tok_type == J_ARRAY) {
1.5 misho 697: for (i = 0, j = 1; i < tok->tok_size; i++) {
698: t = &tok[i + j];
1.2 misho 699: v = ait_getVars(&arr, i);
700: AIT_SET_STRSIZ(v, json_toklen(t) + 1);
1.10 misho 701: if (AIT_GET_STR(v)) {
702: json_tokstrcpy(AIT_GET_STR(v), jstr, t);
703: } else {
704: ait_freeVar(&v);
705: }
1.5 misho 706:
707: /* if there we have array from objects should parse all object tokens */
708: while (i < tok->tok_size - 1 && tok->tok_idx != tok[i + j + 1].tok_parent)
709: j++;
1.2 misho 710: }
711: } else if (tok->tok_type == J_OBJECT) {
712: for (i = 0; tok->tok_idx <= tok[i + 1].tok_parent; i++) {
713: t = &tok[i + 1];
714: v = ait_getVars(&arr, i);
715: AIT_SET_STRSIZ(v, json_toklen(t) + 1);
1.10 misho 716: if (AIT_GET_STR(v)) {
717: json_tokstrcpy(AIT_GET_STR(v), jstr, t);
718: } else {
719: ait_freeVar(&v);
720: }
1.2 misho 721: }
722: } else {
723: elwix_SetErr(J_ERR_PARAM, "%s", jerrstr[J_ERR_PARAM]);
724: ait_freeVars(&arr);
725: return NULL;
726: }
727:
728: return arr;
729: }
730:
731:
732:
733: /*
734: * json_add_begin_object() - Adds begin of object {
735: *
736: * @jstr = JSON string
737: * @jlen = JSON string length
738: * @wspace = whitespace include
739: * return: -1 error or !=-1 actual JSON string length
740: */
741: int
742: json_add_begin_object(char * __restrict jstr, int jlen, int wspace)
743: {
744: int len;
745: size_t eos;
746:
747: if (!jstr)
748: return -1;
749: else
750: eos = strlen(jstr);
751:
752:
753: if (wspace)
754: len = strlcat(jstr, "{ ", jlen);
755: else
756: len = strlcat(jstr, "{", jlen);
757:
758: if (len >= jlen) {
759: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
760: jstr[eos] = 0;
761: return -1;
762: }
763:
764: return len;
765: }
766:
767: /*
768: * json_add_end_object() - Adds end of object }
769: *
770: * @jstr = JSON string
771: * @jlen = JSON string length
772: * @wspace = whitespace include
773: * return: -1 error or !=-1 actual JSON string length
774: */
775: int
776: json_add_end_object(char * __restrict jstr, int jlen, int wspace)
777: {
778: int len;
779: size_t eos;
780:
781: if (!jstr)
782: return -1;
783: else
784: eos = strlen(jstr);
785:
786: if (wspace)
787: len = strlcat(jstr, " }", jlen);
788: else
789: len = strlcat(jstr, "}", jlen);
790:
791: if (len >= jlen) {
792: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
793: jstr[eos] = 0;
794: return -1;
795: }
796:
797: return len;
798: }
799:
800: /*
801: * json_add_begin_array() - Adds begin of array [
802: *
803: * @jstr = JSON string
804: * @jlen = JSON string length
805: * @wspace = whitespace include
806: * return: -1 error or !=-1 actual JSON string length
807: */
808: int
809: json_add_begin_array(char * __restrict jstr, int jlen, int wspace)
810: {
811: int len;
812: size_t eos;
813:
814: if (!jstr)
815: return -1;
816: else
817: eos = strlen(jstr);
818:
819: if (wspace)
820: len = strlcat(jstr, "[ ", jlen);
821: else
822: len = strlcat(jstr, "[", jlen);
823:
824: if (len >= jlen) {
825: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
826: jstr[eos] = 0;
827: return -1;
828: }
829:
830: return len;
831: }
832:
833: /*
834: * json_add_end_array() - Adds end of array ]
835: *
836: * @jstr = JSON string
837: * @jlen = JSON string length
838: * @wspace = whitespace include
839: * return: -1 error or !=-1 actual JSON string length
840: */
841: int
842: json_add_end_array(char * __restrict jstr, int jlen, int wspace)
843: {
844: int len;
845: size_t eos;
846:
847: if (!jstr)
848: return -1;
849: else
850: eos = strlen(jstr);
851:
852: if (wspace)
853: len = strlcat(jstr, " ]", jlen);
854: else
855: len = strlcat(jstr, "]", jlen);
856:
857: if (len >= jlen) {
858: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
859: jstr[eos] = 0;
860: return -1;
861: }
862:
863: return len;
864: }
865:
866: /*
867: * json_add_char() - Adds character
868: *
869: * @jstr = JSON string
870: * @jlen = JSON string length
871: * @ch = Character
872: * return: -1 error or !=-1 actual JSON string length
873: */
874: int
875: json_add_char(char * __restrict jstr, int jlen, u_char ch)
876: {
877: int len;
878:
879: if (!jstr)
880: return -1;
881:
882: len = strlen(jstr) + 1;
883: if (len >= jlen) {
884: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
885: return -1;
886: } else {
887: jstr[len++] = (char) ch;
888: jstr[len] = 0;
889: }
890:
891: return len;
892: }
893:
894: /*
895: * json_add_colon() - Adds key/value pair delimiter colon :
896: *
897: * @jstr = JSON string
898: * @jlen = JSON string length
899: * @wspace = whitespace include
900: * return: -1 error or !=-1 actual JSON string length
901: */
902: int
903: json_add_colon(char * __restrict jstr, int jlen, int wspace)
904: {
905: int len;
906: size_t eos;
907:
908: if (!jstr)
909: return -1;
910: else
911: eos = strlen(jstr);
912:
913: if (wspace)
914: len = strlcat(jstr, ": ", jlen);
915: else
916: len = strlcat(jstr, ":", jlen);
917:
918: if (len >= jlen) {
919: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
920: jstr[eos] = 0;
921: return -1;
922: }
923:
924: return len;
925: }
926:
927: /*
928: * json_add_comma() - Adds value delimiter comma ,
929: *
930: * @jstr = JSON string
931: * @jlen = JSON string length
932: * @wspace = whitespace include
933: * return: -1 error or !=-1 actual JSON string length
934: */
935: int
936: json_add_comma(char * __restrict jstr, int jlen, int wspace)
937: {
938: int len;
939: size_t eos;
940:
941: if (!jstr)
942: return -1;
943: else
944: eos = strlen(jstr);
945:
946: if (wspace)
947: len = strlcat(jstr, ", ", jlen);
948: else
949: len = strlcat(jstr, ",", jlen);
950:
951: if (len >= jlen) {
952: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
953: jstr[eos] = 0;
954: return -1;
955: }
956:
957: return len;
958: }
959:
960: /*
961: * json_add_string() - Adds string
962: *
963: * @jstr = JSON string
964: * @jlen = JSON string length
965: * @unquot = Unquoted string
966: * @str = String, it can't be NULL
967: * return: -1 error or !=-1 actual JSON string length
968: */
969: int
970: json_add_string(char * __restrict jstr, int jlen, int unquot, const char *str)
971: {
972: int len;
973: size_t eos;
974:
975: if (!jstr || !str)
976: return -1;
977: else
978: eos = strlen(jstr);
979:
980: if (!unquot) {
981: len = strlcat(jstr, "\"", jlen);
982: if (len >= jlen) {
983: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
984: jstr[eos] = 0;
985: return -1;
986: }
987: }
988: len = strlcat(jstr, str, jlen);
989: if (len >= jlen) {
990: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
991: jstr[eos] = 0;
992: return -1;
993: }
994: if (!unquot) {
995: len = strlcat(jstr, "\"", jlen);
996: if (len >= jlen) {
997: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
998: jstr[eos] = 0;
999: return -1;
1000: }
1001: }
1002:
1003: return len;
1004: }
1005:
1006: /*
1007: * json_add_value() - Adds value
1008: *
1009: * @jstr = JSON string
1010: * @jlen = JSON string length
1011: * @unquot = Unquoted number
1012: * @num = Number
1013: * return: -1 error or !=-1 actual JSON string length
1014: */
1015: int
1016: json_add_value(char * __restrict jstr, int jlen, int unquot, long num)
1017: {
1018: int len;
1019: char wrk[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };
1020: size_t eos;
1021:
1022: if (!jstr)
1023: return -1;
1024: else
1025: eos = strlen(jstr);
1026:
1027: if (!unquot) {
1028: len = strlcat(jstr, "\"", jlen);
1029: if (len >= jlen) {
1030: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
1031: jstr[eos] = 0;
1032: return -1;
1033: }
1034: }
1035: snprintf(wrk, sizeof wrk, "%ld", num);
1036: len = strlcat(jstr, wrk, jlen);
1037: if (len >= jlen) {
1038: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
1039: jstr[eos] = 0;
1040: return -1;
1041: }
1042: if (!unquot) {
1043: len = strlcat(jstr, "\"", jlen);
1044: if (len >= jlen) {
1045: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
1046: jstr[eos] = 0;
1047: return -1;
1048: }
1049: }
1050:
1051: return len;
1052: }
1053:
1054: /*
1055: * json_add_pair() - Adds key/value pair
1056: *
1057: * @jstr = JSON string
1058: * @jlen = JSON string length
1059: * @wspace = whitespace include
1060: * @key = Key string
1061: * @val = Value string
1062: * return: -1 error or !=-1 actual JSON string length
1063: */
1064: int
1065: json_add_pair(char * __restrict jstr, int jlen, int wspace, const char *key, const char *val)
1066: {
1067: int len = -1;
1068: size_t eos;
1069:
1070: if (!jstr || !key || !val)
1071: return -1;
1072: else
1073: eos = strlen(jstr);
1074:
1075: if (json_add_string(jstr, jlen, 0, key) == -1) {
1076: jstr[eos] = 0;
1077: return -1;
1078: }
1079: if (json_add_colon(jstr, jlen, wspace) == -1) {
1080: jstr[eos] = 0;
1081: return -1;
1082: }
1083: if ((len = json_add_string(jstr, jlen, 0, val)) == -1) {
1.7 misho 1084: jstr[eos] = 0;
1085: return -1;
1086: }
1087:
1088: return len;
1089: }
1090:
1091: /*
1092: * json_add_pair2() - Adds key/value pair with formated args
1093: *
1094: * @jstr = JSON string
1095: * @jlen = JSON string length
1096: * @wspace = whitespace include
1097: * @key = Key string
1098: * @fmt = Format string for values
1099: * return: -1 error or !=-1 actual JSON string length
1100: */
1101: int
1102: json_add_pair2(char * __restrict jstr, int jlen, int wspace, const char *key, const char *fmt, ...)
1103: {
1104: int len = -1;
1105: size_t eos;
1106: va_list lst;
1107: char szStr[BUFSIZ] = { [0 ... BUFSIZ - 1] = 0 };
1108:
1109: if (!jstr || !key || !fmt)
1110: return -1;
1111: else
1112: eos = strlen(jstr);
1113:
1114: if (json_add_string(jstr, jlen, 0, key) == -1) {
1115: jstr[eos] = 0;
1116: return -1;
1117: }
1118: if (json_add_colon(jstr, jlen, wspace) == -1) {
1119: jstr[eos] = 0;
1120: return -1;
1121: }
1122: va_start(lst, fmt);
1123: vsnprintf(szStr, sizeof szStr, fmt, lst);
1124: va_end(lst);
1125: if ((len = json_add_string(jstr, jlen, 0, szStr)) == -1) {
1.2 misho 1126: jstr[eos] = 0;
1127: return -1;
1128: }
1129:
1130: return len;
1131: }
1132:
1133: /*
1134: * json_add_array() - Adds array
1135: *
1136: * @jstr = JSON string
1137: * @jlen = JSON string length
1138: * @wspace = whitespace include
1139: * @arr = Array with variables
1140: * return: -1 error or !=-1 actual JSON string length
1141: */
1142: int
1143: json_add_array(char * __restrict jstr, int jlen, int wspace, array_t * __restrict arr)
1144: {
1145: int len = -1;
1146: register int i;
1147: ait_val_t *v;
1148: size_t eos;
1149:
1150: if (!jstr || !arr)
1151: return -1;
1152: else
1153: eos = strlen(jstr);
1154:
1155: if (json_add_begin_array(jstr, jlen, wspace) == -1) {
1156: jstr[eos] = 0;
1157: return -1;
1158: }
1159: for (i = 0; i < array_Size(arr); i++) {
1160: v = array(arr, i, ait_val_t*);
1161: if (v) {
1162: if (AIT_TYPE(v) == string) {
1163: if (json_add_string(jstr, jlen, 0, AIT_GET_STR(v)) == -1) {
1164: jstr[eos] = 0;
1165: return -1;
1166: }
1167: } else {
1168: if (json_add_value(jstr, jlen, 0, AIT_GET_LIKE(v, long)) == -1) {
1169: jstr[eos] = 0;
1170: return -1;
1171: }
1172: }
1173: if (i < array_Size(arr) - 1 && json_add_comma(jstr, jlen, wspace) == -1) {
1174: jstr[eos] = 0;
1175: return -1;
1176: }
1177: }
1178: }
1179: if ((len = json_add_end_array(jstr, jlen, wspace)) == -1) {
1180: jstr[eos] = 0;
1181: return -1;
1182: }
1183:
1184: return len;
1.3 misho 1185: }
1186:
1187: /*
1188: * json_dump_yaml() - Dump parsed JSON string to YAML format
1189: *
1190: * @f = Output handler
1191: * @jstr = JSON string
1192: * @toks = JSON tokens
1193: * @toksnum = Number of tokens
1194: * @indent = Start indent spaces
1195: * return: 0 done and 1 added one more item
1196: */
1197: int
1198: json_dump_yaml(FILE *f, const char *jstr, jtok_t *toks, int toksnum, int indent)
1199: {
1200: register int i, j, k;
1201:
1.10 misho 1202: if (!toksnum || !toks)
1.3 misho 1203: return 0;
1204:
1205: if (toks->tok_type == J_VALUE) {
1206: fprintf(f, "%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
1207: return 1;
1208: } else if (toks->tok_type == J_STRING) {
1209: fprintf(f, "%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
1210: return 1;
1211: } else if (toks->tok_type == J_OBJECT) {
1212: fprintf(f, "\n");
1213: for (j = i = 0; i < json_toksize(toks); i++) {
1214: for (k = 0; k < indent; k++)
1215: fprintf(f, " ");
1216: j += json_dump_yaml(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1217: fprintf(f, ": ");
1218: j += json_dump_yaml(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1219: fprintf(f, "\n");
1220: }
1221: return j + 1;
1222: } else if (toks->tok_type == J_ARRAY) {
1223: fprintf(f, "\n");
1224: for (j = i = 0; i < json_toksize(toks); i++) {
1225: for (k = 0; k < indent - 1; k++)
1226: fprintf(f, " ");
1227: fprintf(f, " - ");
1228: j += json_dump_yaml(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1.5 misho 1229: fprintf(f, "\n");
1230: }
1231: return j + 1;
1232: }
1233:
1234: return 0;
1235: }
1236:
1237: /*
1238: * json_dump() - Dump parsed JSON string to structure format
1239: *
1240: * @f = Output handler
1241: * @jstr = JSON string
1242: * @toks = JSON tokens
1243: * @toksnum = Number of tokens
1244: * @indent = Start indent spaces
1245: * return: 0 done and 1 added one more item
1246: */
1247: int
1248: json_dump(FILE *f, const char *jstr, jtok_t *toks, int toksnum, int indent)
1249: {
1250: register int i, j, k;
1251:
1252: if (!toksnum)
1253: return 0;
1254:
1255: if (toks->tok_type == J_VALUE) {
1256: fprintf(f, "[idx=%ld type=VALUE start=%ld end=%ld size=%ld parent=%ld] = %.*s",
1257: toks->tok_idx, toks->tok_start, toks->tok_end, toks->tok_size, toks->tok_parent,
1258: (int) json_toklen(toks), json_tokstr(jstr, toks));
1259: return 1;
1260: } else if (toks->tok_type == J_STRING) {
1261: fprintf(f, "[idx=%ld type=STRING start=%ld end=%ld size=%ld parent=%ld] = %.*s",
1262: toks->tok_idx, toks->tok_start, toks->tok_end, toks->tok_size, toks->tok_parent,
1263: (int) json_toklen(toks), json_tokstr(jstr, toks));
1264: return 1;
1265: } else if (toks->tok_type == J_OBJECT) {
1266: fprintf(f, "\n");
1267: fprintf(f, "object:: [idx=%ld type=OBJECT start=%ld end=%ld size=%ld parent=%ld]\n",
1268: toks->tok_idx, toks->tok_start, toks->tok_end, toks->tok_size, toks->tok_parent);
1269: for (j = i = 0; i < json_toksize(toks); i++) {
1270: for (k = 0; k < indent; k++)
1271: fprintf(f, " ");
1272: j += json_dump(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1273: fprintf(f, ": ");
1274: j += json_dump(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1275: fprintf(f, "\n");
1276: }
1277: return j + 1;
1278: } else if (toks->tok_type == J_ARRAY) {
1279: fprintf(f, "\n");
1280: fprintf(f, "array[] [idx=%ld type=ARRAY start=%ld end=%ld size=%ld parent=%ld]",
1281: toks->tok_idx, toks->tok_start, toks->tok_end, toks->tok_size, toks->tok_parent);
1282: for (j = i = 0; i < json_toksize(toks); i++) {
1283: for (k = 0; k < indent - 1; k++)
1284: fprintf(f, " ");
1285: j += json_dump(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1.3 misho 1286: fprintf(f, "\n");
1287: }
1288: return j + 1;
1289: }
1290:
1291: return 0;
1.2 misho 1292: }
1.11 ! misho 1293:
! 1294: /*
! 1295: * json_objscope() - Find object scope of key
! 1296: *
! 1297: * @key = Key of object, if it is =NULL, then return 0 (default scope)
! 1298: * @jstr = JSON string
! 1299: * @toks = JSON tokens
! 1300: * @toksnum = Number of tokens
! 1301: * return: -1 on error or >=0 scope of object
! 1302: */
! 1303: long
! 1304: json_objscope(const char *key, const char *jstr, jtok_t * __restrict toks, int toksnum)
! 1305: {
! 1306: long scope = 0;
! 1307: register int i;
! 1308: int klen;
! 1309:
! 1310: if (!key)
! 1311: return 0; /* default scope */
! 1312:
! 1313: if (!jstr || !toks)
! 1314: return -1;
! 1315: else
! 1316: klen = strlen(key);
! 1317:
! 1318: for (i = 1; i < toksnum; i++)
! 1319: if (toks[i].tok_type == J_STRING && toks[i].tok_size == 1 &&
! 1320: klen == toks[i].tok_end - toks[i].tok_start &&
! 1321: !strncmp(jstr + toks[i].tok_start, key, klen))
! 1322: if (toks[i + 1].tok_type == J_OBJECT) {
! 1323: scope = toks[i + 1].tok_idx;
! 1324: break;
! 1325: }
! 1326:
! 1327: return scope;
! 1328: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>