-
Notifications
You must be signed in to change notification settings - Fork 4
/
json.c
469 lines (381 loc) · 8.76 KB
/
json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
/*
* This file is part of libpjf
* Copyright (C) 2011 Paweł Foremski <[email protected]>
* Copyright (C) 2009 ASN Sp. z o.o.
* Copyright (C) 2008 Ivo van Poorten
*
* License: GNU Lesser General Public License version 2.1
* Imported from zzjson library by: Pawel Foremski <[email protected]>
*/
#include <ctype.h>
#include <string.h>
#include <math.h>
#include "lib.h"
#define GETC() (json->txt[json->i++])
#define UNGETC() (json->i--)
#define SKIPWS() skipws(json)
#define INC_DEPTH() do { \
json->depth++; \
if (json->depth > 50) \
return err(json, 20, "document too deep"); \
} while (0);
#define DEC_DEPTH() do { \
json->depth--; \
if (json->depth < 0) \
return err(json, 21, "internal error"); \
} while (0);
#define IS_LOOSE_KEYCHAR(c) (isalnum(c) || c == '-' || c == '_')
static ut *parse_array(json *json);
static ut *parse_object(json *json);
static char skipws(json *json)
{
char c;
while ((c = GETC())) {
switch (c) {
case ' ':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
continue;
}
break;
}
return c;
}
static ut *err(json *json, int code, const char *msg)
{
return ut_new_err(
code,
msg,
mmatic_sprintf(json, "i=%d", json->i), // TODO
json);
}
static ut *parse_string(json *json)
{
bool loose = 0;
char c, l1, l2, l3, l4;
xstr *str = xstr_create("", json);
c = SKIPWS();
if (c != '"') {
if (json->loose) { /* allow for keys in object without apostrophes */
loose = 1;
UNGETC();
} else {
return err(json, 8, "string: expected \" at the start");
}
}
while ((c = GETC())) {
if (c == '"') {
break;
} else if (loose && !IS_LOOSE_KEYCHAR(c)) {
UNGETC();
c = '"';
break;
}
if (c >= 0 && c <= 31) {
return err(json, 9, "string: control characters not allowed");
} else if (c == '\\') {
c = GETC();
switch (c) {
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 'n': c = '\n'; break;
case 'r': c = '\r'; break;
case 't': c = '\t'; break;
case 'u': /* \uHHHH */
l1 = GETC();
l2 = GETC();
l3 = GETC();
l4 = GETC();
utf8_parse_xcp(str, l1, l2, l3, l4);
continue;
case '\\':
case '/':
case '"':
break;
default:
return err(json, 10, "string: illegal escape character");
}
}
xstr_append_char(str, c);
}
if (c != '"')
return err(json, 11, "string: expected \" at the end");
return ut_new_xstr(str, json);
}
static ut *parse_number(json *json)
{
unsigned long long ival = 0, expo = 0;
double frac = 0.0, fracshft = 10.0;
int sign = 1, signexpo = 1;
char c;
bool dbl = 0;
c = SKIPWS();
if (c == '-') { sign = -1; c = GETC(); }
if (c == '0') { c = GETC(); goto skip; }
if (!isdigit(c))
return err(json, 16, "number: digit expected");
while (isdigit(c)) {
ival *= 10;
ival += c - '0';
c = GETC();
}
skip:
if (c != '.') goto skipfrac;
else dbl = true;
c = GETC();
if (!isdigit(c))
return err(json, 17, "number: digit expected");
while (isdigit(c)) {
frac += (double)(c - '0') / fracshft;
fracshft *= 10.0;
c = GETC();
}
skipfrac:
if (c != 'e' && c != 'E') goto skipexpo;
else dbl = true;
c = GETC();
if (c == '+') c = GETC();
else if (c == '-') { signexpo = -1; c = GETC(); }
if (!isdigit(c))
return err(json, 18, "number: digit expected");
while (isdigit(c)) {
expo *= 10;
expo += c - '0';
c = GETC();
}
skipexpo:
UNGETC();
if (dbl)
return ut_new_double(
(sign * ((long long) ival + frac)) * pow(10.0, (double) signexpo * expo),
json);
else
return ut_new_int(
sign * ival,
json);
}
static bool check_literal(json *json, const char *s)
{
int i = json->i, l = strlen(s);
json->i += l;
return (strncmp(json->txt + i, s, strlen(s)) == 0);
}
static ut *parse_true(json *json)
{
if (check_literal(json, "true"))
return ut_new_bool(true, json);
else
return err(json, 12, "true: expected \"true\"");
}
static ut *parse_false(json *json)
{
if (check_literal(json, "false"))
return ut_new_bool(false, json);
else
return err(json, 13, "false: expected \"false\"");
}
static ut *parse_null(json *json)
{
if (check_literal(json, "null"))
return ut_new_null(json);
else
return err(json, 14, "null: expected \"null\"");
}
static ut *parse_value(json *json)
{
char c;
c = SKIPWS(); UNGETC();
switch (c) {
case '"': return parse_string(json);
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case '-':
return parse_number(json);
case '{': return parse_object(json);
case '[': return parse_array(json);
case 't': return parse_true(json);
case 'f': return parse_false(json);
case 'n': return parse_null(json);
}
return err(json, 15, "value: encountered invalid character");
}
static ut *parse_array(json *json)
{
tlist *list = tlist_create(NULL, json);
ut *val;
char c;
c = SKIPWS();
if (c != '[')
return err(json, 3, "array: expected '['");
INC_DEPTH();
c = SKIPWS();
while (c > 0 && c != ']') {
UNGETC();
val = parse_value(json);
if (!ut_ok(val)) // TODO: convert into exceptions?
return val;
c = SKIPWS();
if (!(c == ',' || c == ']'))
return err(json, 4, "array: expected ',' or ']'");
tlist_push(list, val);
if (c == ',')
c = SKIPWS();
}
if (c != ']')
return err(json, 5, "array: expected ']'");
DEC_DEPTH();
return ut_new_uttlist(list, json);
}
static ut *parse_object(json *json)
{
char c;
thash *hash = thash_create(NULL, NULL, NULL, true, json);
ut *key, *val;
c = SKIPWS();
if (c != '{')
return err(json, 2, "object: expected '{'");
INC_DEPTH();
c = SKIPWS();
while (c > 0 && c != '}') {
UNGETC();
key = parse_string(json);
if (!ut_ok(key)) return key;
c = SKIPWS();
if (c != ':' && !(json->loose && c == '='))
return err(json, 6, "object: expected ':'");
val = parse_value(json);
if (!ut_ok(val)) return val;
c = SKIPWS();
if (!(c == ',' || c == '}')) {
if (json->loose) {
UNGETC();
c = ',';
} else {
return err(json, 7, "object: expected ',' or '}'");
}
}
thash_set(hash, ut_char(key), val);
if (c == ',')
c = SKIPWS();
}
if (c != '}')
return err(json, 19, "object: expected '}'");
DEC_DEPTH();
return ut_new_utthash(hash, json);
}
ut *json_parse(json *json, const char *txt)
{
json->txt = txt;
json->i = 0;
return parse_value(json);
}
json *json_create(void *mm)
{
json *j = mmatic_alloc(mm, sizeof(json));
j->txt = NULL;
j->i = 0;
j->depth = 0;
j->loose = false;
return j;
}
bool json_setopt(json *j, enum json_option o, long v)
{
switch (o) {
case JSON_LOOSE:
j->loose = (bool) v;
break;
default:
return false;
}
return true;
}
char *json_escape(json *json, const char *str)
{
bool bs;
char c;
xstr *xs = xstr_create("", json);
xstr_reserve(xs, 1.1 * strlen(str));
while ((c = *str++)) {
bs = true;
switch (c) {
case '\\':
if (*str == 'u') bs = false; /* copy \uHHHH verbatim */
break;
case '"': break;
case '\b': c = 'b'; break;
case '\f': c = 'f'; break;
case '\n': c = 'n'; break;
case '\r': c = 'r'; break;
case '\t': c = 't'; break;
default: bs = false; break;
}
if (bs) xstr_append_char(xs, '\\');
xstr_append_char(xs, c);
}
return xstr_string(xs);
}
char *json_print(json *json, ut *var)
{
void *mm = json;
char *k, *str;
ut *el;
xstr *xs;
switch (var->type) {
case T_STRING:
str = mmatic_sprintf(mm, "\"%s\"",
json_escape(json, xstr_string(var->d.as_xstr)));
break;
case T_INT:
str = mmatic_sprintf(mm, "%d", var->d.as_int);
break;
case T_DOUBLE:
str = mmatic_sprintf(mm, "%g", var->d.as_double);
break;
case T_LIST:
xs = MMXSTR_CREATE("[ ");
tlist_iter_loop(var->d.as_tlist, el) {
xstr_append(xs, json_print(json, el));
xstr_append(xs, ", ");
}
if (xstr_length(xs) > 2) xstr_cut(xs, 2);
xstr_append(xs, " ]");
str = xstr_string(xs);
break;
case T_HASH:
xs = MMXSTR_CREATE("{ ");
thash_iter_loop(var->d.as_thash, k, el) {
xstr_append_char(xs, '"');
xstr_append(xs, k);
xstr_append(xs, "\": ");
xstr_append(xs, json_print(json, el));
xstr_append(xs, ", ");
}
if (xstr_length(xs) > 2) xstr_cut(xs, 2);
xstr_append(xs, " }");
str = xstr_string(xs);
break;
case T_BOOL:
str = var->d.as_bool ? "true" : "false";
break;
case T_NULL:
str = "null";
break;
case T_ERR:
if (var->d.as_err->data)
str = mmatic_sprintf(mm, "{ \"code\": %d, \"message\": \"%s\", \"data\": \"%s\" }",
var->d.as_err->code,
json_escape(json, var->d.as_err->msg),
json_escape(json, var->d.as_err->data));
else
str = mmatic_sprintf(mm, "{ \"code\": %d, \"message\": \"%s\" }",
var->d.as_err->code,
json_escape(json, var->d.as_err->msg));
break;
default:
str = "";
break;
}
return str;
}