3 STRING_ITERATOR(_json, string_null, 0);
4 // Store interleaved keys/values in a string buffer
9 /** parse a json object */
10 bool _json_parse_object();
11 bool _json_parse_members();
12 bool _json_parse_pair();
13 bool _json_parse_array();
14 bool _json_parse_value();
15 bool _json_parse_true();
16 bool _json_parse_false();
17 bool _json_parse_null();
18 bool _json_parse_string(bool add);
19 bool _json_parse_number();
20 bool _json_parse_float();
21 bool _json_parse_int();
23 #define JSON_BEGIN() int __i = STRING_ITERATOR_SAVE(_json)
24 #define JSON_FAIL(reason) goto fail
28 STRING_ITERATOR_LOAD(_json, __i); \
36 bool _json_parse_object() {
38 if (STRING_ITERATOR_GET(_json) != '{') JSON_FAIL("expected '{'");
39 WITH(int, _json_keys, bufstr_add(_json_buffer, "", 0), _json_parse_members());
40 if (STRING_ITERATOR_GET(_json) != '}') JSON_FAIL("expected '}'");
45 bool _json_parse_members() {
48 if (!_json_parse_pair()) JSON_FAIL("expected pair");
49 if (STRING_ITERATOR_PEEK(_json) == ',') {
50 STRING_ITERATOR_NEXT(_json);
59 bool _json_parse_pair() {
61 if (!_json_parse_string(false)) JSON_FAIL("expected string");
62 string key = _json_temp;
63 bufstr_set(_json_buffer, _json_keys, cons(bufstr_get(_json_buffer, _json_keys), key));
64 key = _json_ns ? strcat(_json_ns, ".", key) : key;
65 bufstr_add(_json_buffer, key, 0);
66 if (STRING_ITERATOR_GET(_json) != ':') JSON_FAIL("expected ':'");
67 bool ret = false; WITH(string, _json_ns, key, ret = _json_parse_value());
68 if (!ret) JSON_FAIL("expected value");
73 bool _json_parse_array() {
75 if (STRING_ITERATOR_GET(_json) != '[') JSON_FAIL("expected '['");
76 int len = bufstr_add(_json_buffer, "0", 0);
77 if (len) bufstr_set(_json_buffer, len - 1, strcat(bufstr_get(_json_buffer, len - 1), ".length"));
78 bool required = false;
79 for (int n = 0; ; n++) {
81 key = _json_ns ? strcat(_json_ns, ".", key) : key;
82 int it = bufstr_add(_json_buffer, key, 0);
83 bool ret = false; WITH(string, _json_ns, key, ret = _json_parse_value());
85 bufstr_free(_json_buffer, it);
86 if (required) JSON_FAIL("expected value"); else break;
88 bufstr_set(_json_buffer, len, ftos(n + 1));
89 if (STRING_ITERATOR_PEEK(_json) == ',') {
90 STRING_ITERATOR_NEXT(_json);
96 if (STRING_ITERATOR_GET(_json) != ']') JSON_FAIL("expected ']'");
101 bool _json_parse_value() {
103 if (!(_json_parse_string(true)
104 || _json_parse_number()
105 || _json_parse_object()
106 || _json_parse_array()
107 || _json_parse_true()
108 || _json_parse_false()
109 || _json_parse_null())) JSON_FAIL("expected value");
114 bool _json_parse_true() {
116 if (!(STRING_ITERATOR_GET(_json) == 't'
117 && STRING_ITERATOR_GET(_json) == 'r'
118 && STRING_ITERATOR_GET(_json) == 'u'
119 && STRING_ITERATOR_GET(_json) == 'e'))
120 JSON_FAIL("expected 'true'");
121 bufstr_add(_json_buffer, "1", 0);
126 bool _json_parse_false() {
128 if (!(STRING_ITERATOR_GET(_json) == 'f'
129 && STRING_ITERATOR_GET(_json) == 'a'
130 && STRING_ITERATOR_GET(_json) == 'l'
131 && STRING_ITERATOR_GET(_json) == 's'
132 && STRING_ITERATOR_GET(_json) == 'e'))
133 JSON_FAIL("expected 'false'");
134 bufstr_add(_json_buffer, "0", 0);
139 bool _json_parse_null() {
141 if (!(STRING_ITERATOR_GET(_json) == 'n'
142 && STRING_ITERATOR_GET(_json) == 'u'
143 && STRING_ITERATOR_GET(_json) == 'l'
144 && STRING_ITERATOR_GET(_json) == 'l'))
145 JSON_FAIL("expected 'null'");
146 bufstr_add(_json_buffer, "", 0);
151 bool _json_parse_string(bool add) {
153 if (STRING_ITERATOR_GET(_json) != '"') JSON_FAIL("expected opening '\"'");
155 for (int c; (c = STRING_ITERATOR_GET(_json)); ) {
157 STRING_ITERATOR_UNGET(_json);
159 } else if (c == '\\') {
161 switch (STRING_ITERATOR_GET(_json)) {
163 JSON_FAIL("expected ( '\"' | '\\' | 'n' | 't' )");
164 case '"': esc = "\""; break;
165 case '\\': esc = "\\"; break;
166 case 'n': esc = "\n"; break;
167 case 't': esc = "\t"; break;
168 case 'u': esc = "\\u"; break; // TODO
169 case '/': esc = "/"; break;
173 s = strcat(s, chr2str(c));
176 if (STRING_ITERATOR_GET(_json) != '"') JSON_FAIL("expected closing '\"'");
177 if (add) bufstr_add(_json_buffer, s, 0);
183 bool _json_parse_number() {
185 if (!(_json_parse_float() || _json_parse_int())) JSON_FAIL("expected number");
190 bool _json_parse_float() {
194 for (int c; (c = STRING_ITERATOR_GET(_json)); ) {
195 if (!(c >= '0' && c <= '9')) {
196 if (c == '.' && needdot) {
200 STRING_ITERATOR_UNGET(_json);
204 s = strcat(s, chr2str(c));
206 if (s == "") JSON_FAIL("expected float");
207 bufstr_add(_json_buffer, s, 0);
212 bool _json_parse_int() {
215 for (int c; (c = STRING_ITERATOR_GET(_json)); ) {
216 if (!(c >= '0' && c <= '9')) {
217 STRING_ITERATOR_UNGET(_json);
220 if (s == "" && c == '0') JSON_FAIL("expected [1-9]");
221 s = strcat(s, chr2str(c));
223 if (s == "") JSON_FAIL("expected int");
224 if (ftos(stof(s)) != s) JSON_FAIL("expected int");
225 bufstr_add(_json_buffer, s, 0);
230 int json_parse(string in, bool() func) {
233 int o = strstrofs(in, "\"", 0);
235 string part = substring(in, 0, o + 1); in = substring(in, o + 1, -1);
236 part = strreplace(" ", "", part);
237 part = strreplace("\n", "", part);
238 trimmed = strcat(trimmed, part);
242 part = strreplace(" ", "", part);
243 part = strreplace("\n", "", part);
244 trimmed = strcat(trimmed, part);
249 int o = strstrofs(in, "\"", 0);
250 int esc = strstrofs(in, "\\\"", 0);
251 if (o < esc || esc < 0) {
253 string part = substring(in, 0, o + 1); in = substring(in, o + 1, -1);
254 trimmed = strcat(trimmed, part);
258 string part = substring(in, 0, esc + 2); in = substring(in, esc + 2, -1);
259 trimmed = strcat(trimmed, part);
265 STRING_ITERATOR_SET(_json, trimmed, 0);
266 _json_buffer = buf_create();
269 buf_del(_json_buffer);
276 string json_get(int buf, string key)
278 for (int i = 1, n = buf_getsize(buf); i < n; i += 2) {
279 if (bufstr_get(buf, i) == key) return bufstr_get(buf, i + 1);
285 void json_del(int buf)
291 void json_dump(int buf)
293 for (int i = 0, n = buf_getsize(buf); i < n; ++i) {
294 print(bufstr_get(buf, i), "\n");
305 \"m_string\": \"\\\"string\\\"\",\n\
310 \"m_arr\": [ ]\n}"; // "
312 int buf = json_parse(s, _json_parse_object);