Annotation of libelwix/src/json.c, revision 1.6
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.6 ! misho 6: * $Id: json.c,v 1.5.2.1 2018/04/19 16:12:45 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.4 misho 15: Copyright 2004 - 2018
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: }
227: if (jstr[json->h_pos] < 32 || jstr[json->h_pos] > 127) {
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));
430: strncpy(AIT_GET_STR(v), json_tokstr(jstr, tok), AIT_LEN(v) - 1);
431:
432: return v;
433: }
434:
435: /*
436: * json_token2str() - Return token to string
437: *
438: * @jstr = JSON string
439: * @tok = Token for convert
1.5 misho 440: * @return =NULL error or !=NULL allocated str, after use should be json_freestr()|e_free()
1.2 misho 441: */
442: char *
443: json_token2str(const char *jstr, jtok_t * __restrict tok)
444: {
445: char *str = NULL;
446: size_t len;
447:
448: if (!jstr || !tok)
449: return NULL;
450:
451: len = json_toklen(tok);
452: str = e_malloc(len + 1);
453: if (!str)
454: return NULL;
455: else {
456: strncpy(str, json_tokstr(jstr, tok), len);
457: str[len] = 0;
458: }
459:
460: return str;
461: }
462:
463: /*
464: * json_token2num() - Return token to numeric
465: *
466: * @jstr = JSON string
467: * @tok = Token for convert
468: * @return number
469: */
470: long
471: json_token2num(const char *jstr, jtok_t * __restrict tok)
472: {
473: long ret = 0;
474: char *str;
475:
476: str = json_token2str(jstr, tok);
477: if (!str)
478: return 0;
479:
480: ret = strtol(str, NULL, 0);
481: e_free(str);
482: return ret;
483: }
484:
485: /*
1.5 misho 486: * json_token2dbl() - Return token to double
487: *
488: * @jstr = JSON string
489: * @tok = Token for convert
490: * @return number
491: */
492: double
493: json_token2dbl(const char *jstr, jtok_t * __restrict tok)
494: {
495: double ret = 0;
496: char *str;
497:
498: str = json_token2str(jstr, tok);
499: if (!str)
500: return 0;
501:
502: ret = strtod(str, NULL);
1.6 ! misho 503: e_free(str);
! 504: return ret;
! 505: }
! 506:
! 507: /*
! 508: * json_token2bool() - Return token to bool int
! 509: *
! 510: * @jstr = JSON string
! 511: * @tok = Token for convert
! 512: * @return 0 for FALSE and !=0 for TRUE
! 513: */
! 514: int
! 515: json_token2bool(const char *jstr, jtok_t * __restrict tok)
! 516: {
! 517: double ret = 0;
! 518: char *str;
! 519:
! 520: str = json_token2str(jstr, tok);
! 521: if (!str)
! 522: return 0;
! 523:
! 524: switch (*str) {
! 525: case 't':
! 526: case 'T':
! 527: ret = 1;
! 528: break;
! 529: case 'f':
! 530: case 'F':
! 531: ret = 0;
! 532: break;
! 533: default:
! 534: ret = (int) strtol(str, NULL, 10);
! 535: break;
! 536: }
1.5 misho 537: e_free(str);
538: return ret;
539: }
540:
541: /*
542: * json_findbykey() - Find token data by key
1.2 misho 543: *
544: * @jstr = JSON string
545: * @key = Search key
1.4 misho 546: * @type = Search key for particular token type, if is J_UNDEF this mean any type
1.2 misho 547: * @toks = Parsed tokens
548: * @toksnum = Number of parsed tokens
549: * return: =NULL error or !=NULL data token found
550: */
551: jtok_t *
1.4 misho 552: json_findbykey(const char *jstr, const char *key, jtype_t type, jtok_t * __restrict toks, int toksnum)
1.2 misho 553: {
554: jtok_t *tok = NULL;
555: register int i;
556: int klen;
557:
558: if (!jstr || !key || !toks)
559: return NULL;
560: else
561: klen = strlen(key);
562:
563: for (i = 1; i < toksnum; i++) {
564: if (toks[i].tok_type == J_STRING &&
565: klen == toks[i].tok_end - toks[i].tok_start &&
566: !strncmp(jstr + toks[i].tok_start, key, klen)) {
1.4 misho 567: if (type != J_UNDEF) {
568: if (toks[i + 1].tok_type == type) {
569: tok = toks + i + 1;
570: break;
571: }
572: } else {
573: tok = toks + i + 1;
574: break;
575: }
1.2 misho 576: }
577: }
578:
579: return tok;
580: }
581:
582: /*
1.5 misho 583: * json_findbypos() - Find token by position on JSON string
584: *
585: * @pos = Offset from begin of JSON string
586: * @toks = Parsed tokens
587: * @toksnum = Number of parsed tokens
588: * return: =NULL error or !=NULL token found
589: */
590: jtok_t *
591: json_findbypos(u_long pos, jtok_t * __restrict toks, int toksnum)
592: {
593: jtok_t *tok = NULL;
594: register int i;
595:
596: if (toks)
597: return NULL;
598:
599: for (i = 1; i < toksnum; i++)
600: if (pos <= toks[i].tok_end && pos >= toks[i].tok_start) {
601: tok = toks + i;
602: break;
603: }
604:
605: return tok;
606: }
607:
608: /*
1.2 misho 609: * json_token2array() - Convert token to string array
610: *
611: * @jstr = JSON string
612: * @tok = Token for convert
613: * return: =NULL error or !=NULL allocated array with string variables,
614: * after use should be ait_freeVars()
615: */
616: array_t *
617: json_token2array(const char *jstr, jtok_t * __restrict tok)
618: {
619: array_t *arr = NULL;
1.5 misho 620: register int i, j;
1.2 misho 621: int siz;
622: ait_val_t *v;
623: jtok_t *t;
624:
625: if (!jstr || !tok)
626: return NULL;
627:
628: siz = tok->tok_size;
629: if (!siz && json_toktype(tok) != J_ARRAY && json_toktype(tok) != J_OBJECT)
630: siz++;
631:
632: arr = ait_allocVars(siz);
633: if (!arr)
634: return NULL;
635:
636: if (tok->tok_type == J_STRING || tok->tok_type == J_VALUE) {
637: v = ait_getVars(&arr, 0);
638: AIT_SET_STRSIZ(v, json_toklen(tok) + 1);
639: json_tokstrcpy(AIT_GET_STR(v), jstr, tok);
640: } else if (tok->tok_type == J_ARRAY) {
1.5 misho 641: for (i = 0, j = 1; i < tok->tok_size; i++) {
642: t = &tok[i + j];
1.2 misho 643: v = ait_getVars(&arr, i);
644: AIT_SET_STRSIZ(v, json_toklen(t) + 1);
645: json_tokstrcpy(AIT_GET_STR(v), jstr, t);
1.5 misho 646:
647: /* if there we have array from objects should parse all object tokens */
648: while (i < tok->tok_size - 1 && tok->tok_idx != tok[i + j + 1].tok_parent)
649: j++;
1.2 misho 650: }
651: } else if (tok->tok_type == J_OBJECT) {
652: for (i = 0; tok->tok_idx <= tok[i + 1].tok_parent; i++) {
653: t = &tok[i + 1];
654: v = ait_getVars(&arr, i);
655: AIT_SET_STRSIZ(v, json_toklen(t) + 1);
656: json_tokstrcpy(AIT_GET_STR(v), jstr, t);
657: }
658: } else {
659: elwix_SetErr(J_ERR_PARAM, "%s", jerrstr[J_ERR_PARAM]);
660: ait_freeVars(&arr);
661: return NULL;
662: }
663:
664: return arr;
665: }
666:
667:
668:
669: /*
670: * json_add_begin_object() - Adds begin of object {
671: *
672: * @jstr = JSON string
673: * @jlen = JSON string length
674: * @wspace = whitespace include
675: * return: -1 error or !=-1 actual JSON string length
676: */
677: int
678: json_add_begin_object(char * __restrict jstr, int jlen, int wspace)
679: {
680: int len;
681: size_t eos;
682:
683: if (!jstr)
684: return -1;
685: else
686: eos = strlen(jstr);
687:
688:
689: if (wspace)
690: len = strlcat(jstr, "{ ", jlen);
691: else
692: len = strlcat(jstr, "{", jlen);
693:
694: if (len >= jlen) {
695: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
696: jstr[eos] = 0;
697: return -1;
698: }
699:
700: return len;
701: }
702:
703: /*
704: * json_add_end_object() - Adds end of object }
705: *
706: * @jstr = JSON string
707: * @jlen = JSON string length
708: * @wspace = whitespace include
709: * return: -1 error or !=-1 actual JSON string length
710: */
711: int
712: json_add_end_object(char * __restrict jstr, int jlen, int wspace)
713: {
714: int len;
715: size_t eos;
716:
717: if (!jstr)
718: return -1;
719: else
720: eos = strlen(jstr);
721:
722: if (wspace)
723: len = strlcat(jstr, " }", jlen);
724: else
725: len = strlcat(jstr, "}", jlen);
726:
727: if (len >= jlen) {
728: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
729: jstr[eos] = 0;
730: return -1;
731: }
732:
733: return len;
734: }
735:
736: /*
737: * json_add_begin_array() - Adds begin of array [
738: *
739: * @jstr = JSON string
740: * @jlen = JSON string length
741: * @wspace = whitespace include
742: * return: -1 error or !=-1 actual JSON string length
743: */
744: int
745: json_add_begin_array(char * __restrict jstr, int jlen, int wspace)
746: {
747: int len;
748: size_t eos;
749:
750: if (!jstr)
751: return -1;
752: else
753: eos = strlen(jstr);
754:
755: if (wspace)
756: len = strlcat(jstr, "[ ", jlen);
757: else
758: len = strlcat(jstr, "[", jlen);
759:
760: if (len >= jlen) {
761: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
762: jstr[eos] = 0;
763: return -1;
764: }
765:
766: return len;
767: }
768:
769: /*
770: * json_add_end_array() - Adds end of array ]
771: *
772: * @jstr = JSON string
773: * @jlen = JSON string length
774: * @wspace = whitespace include
775: * return: -1 error or !=-1 actual JSON string length
776: */
777: int
778: json_add_end_array(char * __restrict jstr, int jlen, int wspace)
779: {
780: int len;
781: size_t eos;
782:
783: if (!jstr)
784: return -1;
785: else
786: eos = strlen(jstr);
787:
788: if (wspace)
789: len = strlcat(jstr, " ]", jlen);
790: else
791: len = strlcat(jstr, "]", jlen);
792:
793: if (len >= jlen) {
794: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
795: jstr[eos] = 0;
796: return -1;
797: }
798:
799: return len;
800: }
801:
802: /*
803: * json_add_char() - Adds character
804: *
805: * @jstr = JSON string
806: * @jlen = JSON string length
807: * @ch = Character
808: * return: -1 error or !=-1 actual JSON string length
809: */
810: int
811: json_add_char(char * __restrict jstr, int jlen, u_char ch)
812: {
813: int len;
814:
815: if (!jstr)
816: return -1;
817:
818: len = strlen(jstr) + 1;
819: if (len >= jlen) {
820: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
821: return -1;
822: } else {
823: jstr[len++] = (char) ch;
824: jstr[len] = 0;
825: }
826:
827: return len;
828: }
829:
830: /*
831: * json_add_colon() - Adds key/value pair delimiter colon :
832: *
833: * @jstr = JSON string
834: * @jlen = JSON string length
835: * @wspace = whitespace include
836: * return: -1 error or !=-1 actual JSON string length
837: */
838: int
839: json_add_colon(char * __restrict jstr, int jlen, int wspace)
840: {
841: int len;
842: size_t eos;
843:
844: if (!jstr)
845: return -1;
846: else
847: eos = strlen(jstr);
848:
849: if (wspace)
850: len = strlcat(jstr, ": ", jlen);
851: else
852: len = strlcat(jstr, ":", jlen);
853:
854: if (len >= jlen) {
855: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
856: jstr[eos] = 0;
857: return -1;
858: }
859:
860: return len;
861: }
862:
863: /*
864: * json_add_comma() - Adds value delimiter comma ,
865: *
866: * @jstr = JSON string
867: * @jlen = JSON string length
868: * @wspace = whitespace include
869: * return: -1 error or !=-1 actual JSON string length
870: */
871: int
872: json_add_comma(char * __restrict jstr, int jlen, int wspace)
873: {
874: int len;
875: size_t eos;
876:
877: if (!jstr)
878: return -1;
879: else
880: eos = strlen(jstr);
881:
882: if (wspace)
883: len = strlcat(jstr, ", ", jlen);
884: else
885: len = strlcat(jstr, ",", jlen);
886:
887: if (len >= jlen) {
888: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
889: jstr[eos] = 0;
890: return -1;
891: }
892:
893: return len;
894: }
895:
896: /*
897: * json_add_string() - Adds string
898: *
899: * @jstr = JSON string
900: * @jlen = JSON string length
901: * @unquot = Unquoted string
902: * @str = String, it can't be NULL
903: * return: -1 error or !=-1 actual JSON string length
904: */
905: int
906: json_add_string(char * __restrict jstr, int jlen, int unquot, const char *str)
907: {
908: int len;
909: size_t eos;
910:
911: if (!jstr || !str)
912: return -1;
913: else
914: eos = strlen(jstr);
915:
916: if (!unquot) {
917: len = strlcat(jstr, "\"", jlen);
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: len = strlcat(jstr, str, jlen);
925: if (len >= jlen) {
926: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
927: jstr[eos] = 0;
928: return -1;
929: }
930: if (!unquot) {
931: len = strlcat(jstr, "\"", jlen);
932: if (len >= jlen) {
933: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
934: jstr[eos] = 0;
935: return -1;
936: }
937: }
938:
939: return len;
940: }
941:
942: /*
943: * json_add_value() - Adds value
944: *
945: * @jstr = JSON string
946: * @jlen = JSON string length
947: * @unquot = Unquoted number
948: * @num = Number
949: * return: -1 error or !=-1 actual JSON string length
950: */
951: int
952: json_add_value(char * __restrict jstr, int jlen, int unquot, long num)
953: {
954: int len;
955: char wrk[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };
956: size_t eos;
957:
958: if (!jstr)
959: return -1;
960: else
961: eos = strlen(jstr);
962:
963: if (!unquot) {
964: len = strlcat(jstr, "\"", jlen);
965: if (len >= jlen) {
966: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
967: jstr[eos] = 0;
968: return -1;
969: }
970: }
971: snprintf(wrk, sizeof wrk, "%ld", num);
972: len = strlcat(jstr, wrk, jlen);
973: if (len >= jlen) {
974: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
975: jstr[eos] = 0;
976: return -1;
977: }
978: if (!unquot) {
979: len = strlcat(jstr, "\"", jlen);
980: if (len >= jlen) {
981: elwix_SetErr(J_ERR_NOMEM, "%s", jerrstr[J_ERR_NOMEM]);
982: jstr[eos] = 0;
983: return -1;
984: }
985: }
986:
987: return len;
988: }
989:
990: /*
991: * json_add_pair() - Adds key/value pair
992: *
993: * @jstr = JSON string
994: * @jlen = JSON string length
995: * @wspace = whitespace include
996: * @key = Key string
997: * @val = Value string
998: * return: -1 error or !=-1 actual JSON string length
999: */
1000: int
1001: json_add_pair(char * __restrict jstr, int jlen, int wspace, const char *key, const char *val)
1002: {
1003: int len = -1;
1004: size_t eos;
1005:
1006: if (!jstr || !key || !val)
1007: return -1;
1008: else
1009: eos = strlen(jstr);
1010:
1011: if (json_add_string(jstr, jlen, 0, key) == -1) {
1012: jstr[eos] = 0;
1013: return -1;
1014: }
1015: if (json_add_colon(jstr, jlen, wspace) == -1) {
1016: jstr[eos] = 0;
1017: return -1;
1018: }
1019: if ((len = json_add_string(jstr, jlen, 0, val)) == -1) {
1020: jstr[eos] = 0;
1021: return -1;
1022: }
1023:
1024: return len;
1025: }
1026:
1027: /*
1028: * json_add_array() - Adds array
1029: *
1030: * @jstr = JSON string
1031: * @jlen = JSON string length
1032: * @wspace = whitespace include
1033: * @arr = Array with variables
1034: * return: -1 error or !=-1 actual JSON string length
1035: */
1036: int
1037: json_add_array(char * __restrict jstr, int jlen, int wspace, array_t * __restrict arr)
1038: {
1039: int len = -1;
1040: register int i;
1041: ait_val_t *v;
1042: size_t eos;
1043:
1044: if (!jstr || !arr)
1045: return -1;
1046: else
1047: eos = strlen(jstr);
1048:
1049: if (json_add_begin_array(jstr, jlen, wspace) == -1) {
1050: jstr[eos] = 0;
1051: return -1;
1052: }
1053: for (i = 0; i < array_Size(arr); i++) {
1054: v = array(arr, i, ait_val_t*);
1055: if (v) {
1056: if (AIT_TYPE(v) == string) {
1057: if (json_add_string(jstr, jlen, 0, AIT_GET_STR(v)) == -1) {
1058: jstr[eos] = 0;
1059: return -1;
1060: }
1061: } else {
1062: if (json_add_value(jstr, jlen, 0, AIT_GET_LIKE(v, long)) == -1) {
1063: jstr[eos] = 0;
1064: return -1;
1065: }
1066: }
1067: if (i < array_Size(arr) - 1 && json_add_comma(jstr, jlen, wspace) == -1) {
1068: jstr[eos] = 0;
1069: return -1;
1070: }
1071: }
1072: }
1073: if ((len = json_add_end_array(jstr, jlen, wspace)) == -1) {
1074: jstr[eos] = 0;
1075: return -1;
1076: }
1077:
1078: return len;
1.3 misho 1079: }
1080:
1081: /*
1082: * json_dump_yaml() - Dump parsed JSON string to YAML format
1083: *
1084: * @f = Output handler
1085: * @jstr = JSON string
1086: * @toks = JSON tokens
1087: * @toksnum = Number of tokens
1088: * @indent = Start indent spaces
1089: * return: 0 done and 1 added one more item
1090: */
1091: int
1092: json_dump_yaml(FILE *f, const char *jstr, jtok_t *toks, int toksnum, int indent)
1093: {
1094: register int i, j, k;
1095:
1096: if (!toksnum)
1097: return 0;
1098:
1099: if (toks->tok_type == J_VALUE) {
1100: fprintf(f, "%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
1101: return 1;
1102: } else if (toks->tok_type == J_STRING) {
1103: fprintf(f, "%.*s", (int) json_toklen(toks), json_tokstr(jstr, toks));
1104: return 1;
1105: } else if (toks->tok_type == J_OBJECT) {
1106: fprintf(f, "\n");
1107: for (j = i = 0; i < json_toksize(toks); i++) {
1108: for (k = 0; k < indent; k++)
1109: fprintf(f, " ");
1110: j += json_dump_yaml(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1111: fprintf(f, ": ");
1112: j += json_dump_yaml(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1113: fprintf(f, "\n");
1114: }
1115: return j + 1;
1116: } else if (toks->tok_type == J_ARRAY) {
1117: fprintf(f, "\n");
1118: for (j = i = 0; i < json_toksize(toks); i++) {
1119: for (k = 0; k < indent - 1; k++)
1120: fprintf(f, " ");
1121: fprintf(f, " - ");
1122: j += json_dump_yaml(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1.5 misho 1123: fprintf(f, "\n");
1124: }
1125: return j + 1;
1126: }
1127:
1128: return 0;
1129: }
1130:
1131: /*
1132: * json_dump() - Dump parsed JSON string to structure format
1133: *
1134: * @f = Output handler
1135: * @jstr = JSON string
1136: * @toks = JSON tokens
1137: * @toksnum = Number of tokens
1138: * @indent = Start indent spaces
1139: * return: 0 done and 1 added one more item
1140: */
1141: int
1142: json_dump(FILE *f, const char *jstr, jtok_t *toks, int toksnum, int indent)
1143: {
1144: register int i, j, k;
1145:
1146: if (!toksnum)
1147: return 0;
1148:
1149: if (toks->tok_type == J_VALUE) {
1150: fprintf(f, "[idx=%ld type=VALUE start=%ld end=%ld size=%ld parent=%ld] = %.*s",
1151: toks->tok_idx, toks->tok_start, toks->tok_end, toks->tok_size, toks->tok_parent,
1152: (int) json_toklen(toks), json_tokstr(jstr, toks));
1153: return 1;
1154: } else if (toks->tok_type == J_STRING) {
1155: fprintf(f, "[idx=%ld type=STRING start=%ld end=%ld size=%ld parent=%ld] = %.*s",
1156: toks->tok_idx, toks->tok_start, toks->tok_end, toks->tok_size, toks->tok_parent,
1157: (int) json_toklen(toks), json_tokstr(jstr, toks));
1158: return 1;
1159: } else if (toks->tok_type == J_OBJECT) {
1160: fprintf(f, "\n");
1161: fprintf(f, "object:: [idx=%ld type=OBJECT start=%ld end=%ld size=%ld parent=%ld]\n",
1162: toks->tok_idx, toks->tok_start, toks->tok_end, toks->tok_size, toks->tok_parent);
1163: for (j = i = 0; i < json_toksize(toks); i++) {
1164: for (k = 0; k < indent; k++)
1165: fprintf(f, " ");
1166: j += json_dump(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1167: fprintf(f, ": ");
1168: j += json_dump(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1169: fprintf(f, "\n");
1170: }
1171: return j + 1;
1172: } else if (toks->tok_type == J_ARRAY) {
1173: fprintf(f, "\n");
1174: fprintf(f, "array[] [idx=%ld type=ARRAY start=%ld end=%ld size=%ld parent=%ld]",
1175: toks->tok_idx, toks->tok_start, toks->tok_end, toks->tok_size, toks->tok_parent);
1176: for (j = i = 0; i < json_toksize(toks); i++) {
1177: for (k = 0; k < indent - 1; k++)
1178: fprintf(f, " ");
1179: j += json_dump(f, jstr, toks + j + 1, toksnum - j, indent + 1);
1.3 misho 1180: fprintf(f, "\n");
1181: }
1182: return j + 1;
1183: }
1184:
1185: return 0;
1.2 misho 1186: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>