Mercurial > wgsdk
annotate src/json.c @ 12:dd427b7cc459 default tip
json: replace with nxjson library
more lightweight, reduces the binary size by about 40 kb
author | Paper <paper@paper.us.eu.org> |
---|---|
date | Fri, 15 Mar 2024 20:46:18 -0400 |
parents | e6a594f16403 |
children |
rev | line source |
---|---|
11 | 1 /* |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
2 * Copyright (c) 2013 Yaroslav Stavnichiy <yarosla@gmail.com> |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
3 * |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
4 * This file is part of NXJSON. |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
5 * |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
6 * NXJSON is free software: you can redistribute it and/or modify |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
7 * it under the terms of the GNU Lesser General Public License |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
8 * as published by the Free Software Foundation, either version 3 |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
9 * of the License, or (at your option) any later version. |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
10 * |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
11 * NXJSON is distributed in the hope that it will be useful, |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
14 * GNU Lesser General Public License for more details. |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
15 * |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
16 * You should have received a copy of the GNU Lesser General Public |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
17 * License along with NXJSON. If not, see <http://www.gnu.org/licenses/>. |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
18 */ |
11 | 19 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
20 // this file can be #included in your code |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
21 #ifndef NXJSON_C |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
22 #define NXJSON_C |
11 | 23 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
24 #ifdef __cplusplus |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
25 extern "C" { |
11 | 26 #endif |
27 | |
28 | |
29 #include <stdlib.h> | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
30 #include <stdio.h> |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
31 #include <string.h> |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
32 #include <assert.h> |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
33 #include <errno.h> |
11 | 34 |
35 #include "json.h" | |
36 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
37 // redefine NX_JSON_CALLOC & NX_JSON_FREE to use custom allocator |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
38 #ifndef NX_JSON_CALLOC |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
39 #define NX_JSON_CALLOC() calloc(1, sizeof(nx_json)) |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
40 #define NX_JSON_FREE(json) free((void*)(json)) |
11 | 41 #endif |
42 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
43 // redefine NX_JSON_REPORT_ERROR to use custom error reporting |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
44 #ifndef NX_JSON_REPORT_ERROR |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
45 #define NX_JSON_REPORT_ERROR(msg, p) fprintf(stderr, "NXJSON PARSE ERROR (%d): " msg " at %s\n", __LINE__, p) |
11 | 46 #endif |
47 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
48 #define IS_WHITESPACE(c) ((unsigned char)(c)<=(unsigned char)' ') |
11 | 49 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
50 static nx_json *create_json(nx_json_type type, const char *key, nx_json *parent) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
51 nx_json *js = NX_JSON_CALLOC(); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
52 assert(js); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
53 js->type = type; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
54 js->key = key; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
55 if (!parent->children.last) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
56 parent->children.first = parent->children.last = js; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
57 } else { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
58 parent->children.last->next = js; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
59 parent->children.last = js; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
60 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
61 parent->children.length++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
62 return js; |
11 | 63 } |
64 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
65 void nx_json_free(const nx_json *js) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
66 if (!js) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
67 return; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
68 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
69 if (js->type == NX_JSON_OBJECT || js->type == NX_JSON_ARRAY) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
70 nx_json *p = js->children.first; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
71 nx_json *p1; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
72 while (p) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
73 p1 = p->next; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
74 nx_json_free (p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
75 p = p1; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
76 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
77 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
78 NX_JSON_FREE(js); |
11 | 79 } |
80 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
81 static int unicode_to_utf8(unsigned int codepoint, char *p, char **endp) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
82 // code from http://stackoverflow.com/a/4609989/697313 |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
83 if (codepoint < 0x80) *p++ = codepoint; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
84 else if (codepoint < 0x800) *p++ = 192 + codepoint / 64, *p++ = 128 + codepoint % 64; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
85 else if (codepoint - 0xd800u < 0x800) return 0; // surrogate must have been treated earlier |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
86 else if (codepoint < 0x10000) |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
87 *p++ = 224 + codepoint / 4096, *p++ = 128 + codepoint / 64 % 64, *p++ = 128 + codepoint % 64; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
88 else if (codepoint < 0x110000) |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
89 *p++ = 240 + codepoint / 262144, *p++ = 128 + codepoint / 4096 % 64, *p++ = 128 + codepoint / 64 % 64, *p++ = |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
90 128 + codepoint % 64; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
91 else return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
92 *endp = p; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
93 return 1; |
11 | 94 } |
95 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
96 nx_json_unicode_encoder nx_json_unicode_to_utf8 = unicode_to_utf8; |
11 | 97 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
98 static inline int hex_val(char c) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
99 if (c >= '0' && c <= '9') return c - '0'; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
100 if (c >= 'a' && c <= 'f') return c - 'a' + 10; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
101 if (c >= 'A' && c <= 'F') return c - 'A' + 10; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
102 return -1; |
11 | 103 } |
104 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
105 static char *unescape_string(char *s, char **end, nx_json_unicode_encoder encoder) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
106 char *p = s; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
107 char *d = s; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
108 char c; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
109 while ((c = *p++)) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
110 if (c == '"') { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
111 *d = '\0'; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
112 *end = p; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
113 return s; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
114 } else if (c == '\\') { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
115 switch (*p) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
116 case '\\': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
117 case '/': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
118 case '"': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
119 *d++ = *p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
120 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
121 case 'b': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
122 *d++ = '\b'; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
123 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
124 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
125 case 'f': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
126 *d++ = '\f'; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
127 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
128 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
129 case 'n': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
130 *d++ = '\n'; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
131 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
132 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
133 case 'r': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
134 *d++ = '\r'; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
135 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
136 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
137 case 't': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
138 *d++ = '\t'; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
139 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
140 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
141 case 'u': // unicode |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
142 if (!encoder) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
143 // leave untouched |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
144 *d++ = c; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
145 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
146 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
147 char *ps = p - 1; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
148 int h1, h2, h3, h4; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
149 if ((h1 = hex_val (p[1])) < 0 || (h2 = hex_val (p[2])) < 0 || (h3 = hex_val (p[3])) < 0 || |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
150 (h4 = hex_val (p[4])) < 0) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
151 NX_JSON_REPORT_ERROR("invalid unicode escape", p - 1); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
152 return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
153 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
154 unsigned int codepoint = h1 << 12 | h2 << 8 | h3 << 4 | h4; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
155 if ((codepoint & 0xfc00) == 0xd800) { // high surrogate; need one more unicode to succeed |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
156 p += 6; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
157 if (p[-1] != '\\' || *p != 'u' || (h1 = hex_val (p[1])) < 0 || (h2 = hex_val (p[2])) < 0 || |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
158 (h3 = hex_val (p[3])) < 0 || (h4 = hex_val (p[4])) < 0) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
159 NX_JSON_REPORT_ERROR("invalid unicode surrogate", ps); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
160 return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
161 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
162 unsigned int codepoint2 = h1 << 12 | h2 << 8 | h3 << 4 | h4; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
163 if ((codepoint2 & 0xfc00) != 0xdc00) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
164 NX_JSON_REPORT_ERROR("invalid unicode surrogate", ps); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
165 return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
166 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
167 codepoint = 0x10000 + ((codepoint - 0xd800) << 10) + (codepoint2 - 0xdc00); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
168 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
169 if (!encoder (codepoint, d, &d)) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
170 NX_JSON_REPORT_ERROR("invalid codepoint", ps); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
171 return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
172 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
173 p += 5; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
174 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
175 default: |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
176 // leave untouched |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
177 *d++ = c; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
178 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
179 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
180 } else { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
181 *d++ = c; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
182 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
183 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
184 NX_JSON_REPORT_ERROR("no closing quote for string", s); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
185 return 0; |
11 | 186 } |
187 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
188 static char *skip_block_comment(char *p) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
189 // assume p[-2]=='/' && p[-1]=='*' |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
190 char *ps = p - 2; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
191 if (!*p) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
192 NX_JSON_REPORT_ERROR("endless comment", ps); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
193 return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
194 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
195 REPEAT: |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
196 p = strchr (p + 1, '/'); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
197 if (!p) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
198 NX_JSON_REPORT_ERROR("endless comment", ps); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
199 return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
200 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
201 if (p[-1] != '*') goto REPEAT; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
202 return p + 1; |
11 | 203 } |
204 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
205 static char *parse_key(const char **key, char *p, nx_json_unicode_encoder encoder) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
206 // on '}' return with *p=='}' |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
207 char c; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
208 while ((c = *p++)) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
209 if (c == '"') { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
210 *key = unescape_string (p, &p, encoder); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
211 if (!*key) return 0; // propagate error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
212 while (*p && IS_WHITESPACE(*p)) p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
213 if (*p == ':') return p + 1; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
214 NX_JSON_REPORT_ERROR("unexpected chars", p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
215 return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
216 } else if (IS_WHITESPACE(c) || c == ',') { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
217 // continue |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
218 } else if (c == '}') { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
219 return p - 1; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
220 } else if (c == '/') { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
221 if (*p == '/') { // line comment |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
222 char *ps = p - 1; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
223 p = strchr (p + 1, '\n'); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
224 if (!p) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
225 NX_JSON_REPORT_ERROR("endless comment", ps); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
226 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
227 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
228 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
229 } else if (*p == '*') { // block comment |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
230 p = skip_block_comment (p + 1); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
231 if (!p) return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
232 } else { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
233 NX_JSON_REPORT_ERROR("unexpected chars", p - 1); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
234 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
235 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
236 } else { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
237 NX_JSON_REPORT_ERROR("unexpected chars", p - 1); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
238 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
239 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
240 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
241 NX_JSON_REPORT_ERROR("unexpected chars", p - 1); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
242 return 0; // error |
11 | 243 } |
244 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
245 static char *parse_value(nx_json *parent, const char *key, char *p, nx_json_unicode_encoder encoder) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
246 nx_json *js; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
247 while (1) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
248 switch (*p) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
249 case '\0': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
250 NX_JSON_REPORT_ERROR("unexpected end of text", p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
251 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
252 case ' ': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
253 case '\t': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
254 case '\n': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
255 case '\r': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
256 case ',': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
257 // skip |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
258 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
259 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
260 case '{': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
261 js = create_json (NX_JSON_OBJECT, key, parent); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
262 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
263 while (1) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
264 const char *new_key; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
265 p = parse_key (&new_key, p, encoder); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
266 if (!p) return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
267 if (*p == '}') return p + 1; // end of object |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
268 p = parse_value (js, new_key, p, encoder); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
269 if (!p) return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
270 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
271 case '[': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
272 js = create_json (NX_JSON_ARRAY, key, parent); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
273 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
274 while (1) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
275 p = parse_value (js, 0, p, encoder); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
276 if (!p) return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
277 if (*p == ']') return p + 1; // end of array |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
278 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
279 case ']': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
280 return p; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
281 case '"': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
282 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
283 js = create_json (NX_JSON_STRING, key, parent); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
284 js->text_value = unescape_string (p, &p, encoder); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
285 if (!js->text_value) return 0; // propagate error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
286 return p; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
287 case '-': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
288 case '0': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
289 case '1': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
290 case '2': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
291 case '3': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
292 case '4': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
293 case '5': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
294 case '6': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
295 case '7': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
296 case '8': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
297 case '9': { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
298 js = create_json (NX_JSON_INTEGER, key, parent); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
299 char *pe; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
300 if (*p == '-') { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
301 js->num.s_value = (nxjson_s64) strtoll (p, &pe, 0); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
302 } else { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
303 js->num.u_value = (nxjson_u64) strtoull (p, &pe, 0); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
304 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
305 if (pe == p || errno == ERANGE) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
306 NX_JSON_REPORT_ERROR("invalid number", p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
307 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
308 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
309 if (*pe == '.' || *pe == 'e' || *pe == 'E') { // double value |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
310 js->type = NX_JSON_DOUBLE; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
311 js->num.dbl_value = strtod (p, &pe); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
312 if (pe == p || errno == ERANGE) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
313 NX_JSON_REPORT_ERROR("invalid number", p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
314 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
315 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
316 } else { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
317 if (*p == '-') { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
318 js->num.dbl_value = js->num.s_value; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
319 } else { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
320 js->num.dbl_value = js->num.u_value; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
321 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
322 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
323 return pe; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
324 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
325 case 't': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
326 if (!strncmp (p, "true", 4)) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
327 js = create_json (NX_JSON_BOOL, key, parent); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
328 js->num.u_value = 1; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
329 return p + 4; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
330 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
331 NX_JSON_REPORT_ERROR("unexpected chars", p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
332 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
333 case 'f': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
334 if (!strncmp (p, "false", 5)) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
335 js = create_json (NX_JSON_BOOL, key, parent); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
336 js->num.u_value = 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
337 return p + 5; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
338 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
339 NX_JSON_REPORT_ERROR("unexpected chars", p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
340 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
341 case 'n': |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
342 if (!strncmp (p, "null", 4)) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
343 create_json (NX_JSON_NULL, key, parent); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
344 return p + 4; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
345 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
346 NX_JSON_REPORT_ERROR("unexpected chars", p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
347 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
348 case '/': // comment |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
349 if (p[1] == '/') { // line comment |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
350 char *ps = p; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
351 p = strchr (p + 2, '\n'); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
352 if (!p) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
353 NX_JSON_REPORT_ERROR("endless comment", ps); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
354 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
355 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
356 p++; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
357 } else if (p[1] == '*') { // block comment |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
358 p = skip_block_comment (p + 2); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
359 if (!p) return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
360 } else { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
361 NX_JSON_REPORT_ERROR("unexpected chars", p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
362 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
363 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
364 break; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
365 default: |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
366 NX_JSON_REPORT_ERROR("unexpected chars", p); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
367 return 0; // error |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
368 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
369 } |
11 | 370 } |
371 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
372 const nx_json *nx_json_parse_utf8(char *text) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
373 return nx_json_parse (text, unicode_to_utf8); |
11 | 374 } |
375 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
376 const nx_json *nx_json_parse(char *text, nx_json_unicode_encoder encoder) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
377 nx_json js = {0}; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
378 if (!parse_value (&js, 0, text, encoder)) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
379 if (js.children.first) nx_json_free (js.children.first); |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
380 return 0; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
381 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
382 return js.children.first; |
11 | 383 } |
384 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
385 const nx_json *nx_json_get(const nx_json *json, const char *key) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
386 nx_json *js; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
387 for (js = json->children.first; js; js = js->next) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
388 if (js->key && !strcmp (js->key, key)) return js; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
389 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
390 return NULL; |
11 | 391 } |
392 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
393 const nx_json *nx_json_item(const nx_json *json, int idx) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
394 nx_json *js; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
395 for (js = json->children.first; js; js = js->next) { |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
396 if (!idx--) return js; |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
397 } |
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
398 return NULL; |
11 | 399 } |
400 | |
401 | |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
402 #ifdef __cplusplus |
11 | 403 } |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
404 #endif |
11 | 405 |
12
dd427b7cc459
json: replace with nxjson library
Paper <paper@paper.us.eu.org>
parents:
11
diff
changeset
|
406 #endif /* NXJSON_C */ |