-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjsml.h
505 lines (473 loc) · 12.8 KB
/
jsml.h
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
#ifndef JSML_H
#define JSML_H
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef enum json_type {
JSON_NULL,
JSON_OBJECT,
JSON_ARRAY,
JSON_STRING,
JSON_INTEGER,
JSON_DOUBLE,
JSON_BOOL
} json_type;
typedef struct json {
json_type type; // type of json node, based on json_type enum
const char *key; // key of the property; applicable only for OBJECT
const char *json_string; // value of STRING node
long long json_integer; // value of INTEGER node
int json_bool; // value of BOOL node
double json_double; // value of DOUBLE node
int length; // number of children of OBJECT or ARRAY
struct json *child; // points to first child
struct json *next; // points to next child
struct json *last_child;
} json;
typedef int (*json_unicode_encoder)(unsigned int codepoint, char *p,
char **endp);
const json *json_parse(char *text, json_unicode_encoder encoder);
const json *json_parse_file(char *file_path, json_unicode_encoder encoder);
const json *json_parse_utf8(char *text);
const json *json_parse_file_utf8(char *file_path);
const json *json_get(const json *json,
const char *key); // get object's property by key
const json *json_item(const json *json, int idx); // get array element by index
const json *json_get_nested(const json *parsed_json,
const char *key); // get nested object/array
void json_print(const json *parsed_json);
void json_recursive_print(const json *parsed_json, int depth);
void json_free(const json *js);
char *unescape_json_string_literal(const char *str);
char *read_file(char *filename);
// redefine JSON_CALLOC & JSON_FREE to use custom allocator
#ifndef JSON_CALLOC
#define JSON_CALLOC() calloc(1, sizeof(json))
#define JSON_FREE(json) free((void *)(json))
#endif
// redefine JSON_REPORT_ERROR to use custom error reporting
#ifndef JSON_REPORT_ERROR
#define JSON_REPORT_ERROR(msg, p) \
fprintf(stderr, "JSML PARSE ERROR (%d): " msg " at %s\n", __LINE__, p)
#endif
#define IS_WHITESPACE(c) ((unsigned char)(c) <= (unsigned char)' ')
static const json dummy = {JSON_NULL};
static json *create_json(json_type type, const char *key, json *parent) {
json *js = (json *)JSON_CALLOC();
assert(js);
js->type = type;
js->key = key;
if (!parent->last_child) {
parent->child = parent->last_child = js;
} else {
parent->last_child->next = js;
parent->last_child = js;
}
parent->length++;
return js;
}
inline void json_free(const json *js) {
json *p = js->child;
json *p1;
while (p) {
p1 = p->next;
json_free(p);
p = p1;
}
JSON_FREE(js);
}
static int unicode_to_utf8(unsigned int codepoint, char *p, char **endp) {
// code from http://stackoverflow.com/a/4609989/697313
if (codepoint < 0x80)
*p++ = codepoint;
else if (codepoint < 0x800)
*p++ = 192 + codepoint / 64, *p++ = 128 + codepoint % 64;
else if (codepoint - 0xd800u < 0x800)
return 0; // surrogate must have been treated earlier
else if (codepoint < 0x10000)
*p++ = 224 + codepoint / 4096, *p++ = 128 + codepoint / 64 % 64,
*p++ = 128 + codepoint % 64;
else if (codepoint < 0x110000)
*p++ = 240 + codepoint / 262144, *p++ = 128 + codepoint / 4096 % 64,
*p++ = 128 + codepoint / 64 % 64, *p++ = 128 + codepoint % 64;
else
return 0; // error
*endp = p;
return 1;
}
static inline int hex_val(char c) {
if (c >= '0' && c <= '9')
return c - '0';
if (c >= 'a' && c <= 'f')
return c - 'a' + 10;
if (c >= 'A' && c <= 'F')
return c - 'A' + 10;
return -1;
}
static char *unescape_string(char *s, char **end,
json_unicode_encoder encoder) {
char *p = s;
char *d = s;
char c;
while ((c = *p++)) {
if (c == '"') {
*d = '\0';
*end = p;
return s;
} else if (c == '\\') {
switch (*p) {
case '\\':
case '/':
case '"':
*d++ = *p++;
break;
case 'b':
*d++ = '\b';
p++;
break;
case 'f':
*d++ = '\f';
p++;
break;
case 'n':
*d++ = '\n';
p++;
break;
case 'r':
*d++ = '\r';
p++;
break;
case 't':
*d++ = '\t';
p++;
break;
case 'u': // unicode
{
if (!encoder) {
// leave untouched
*d++ = c;
break;
}
char *ps = p - 1;
int h1, h2, h3, h4;
if ((h1 = hex_val(p[1])) < 0 || (h2 = hex_val(p[2])) < 0 ||
(h3 = hex_val(p[3])) < 0 || (h4 = hex_val(p[4])) < 0) {
JSON_REPORT_ERROR("invalid unicode escape", p - 1);
return 0;
}
unsigned int codepoint = h1 << 12 | h2 << 8 | h3 << 4 | h4;
if ((codepoint & 0xfc00) ==
0xd800) { // high surrogate; need one more unicode to succeed
p += 6;
if (p[-1] != '\\' || *p != 'u' || (h1 = hex_val(p[1])) < 0 ||
(h2 = hex_val(p[2])) < 0 || (h3 = hex_val(p[3])) < 0 ||
(h4 = hex_val(p[4])) < 0) {
JSON_REPORT_ERROR("invalid unicode surrogate", ps);
return 0;
}
unsigned int codepoint2 = h1 << 12 | h2 << 8 | h3 << 4 | h4;
if ((codepoint2 & 0xfc00) != 0xdc00) {
JSON_REPORT_ERROR("invalid unicode surrogate", ps);
return 0;
}
codepoint =
0x10000 + ((codepoint - 0xd800) << 10) + (codepoint2 - 0xdc00);
}
if (!encoder(codepoint, d, &d)) {
JSON_REPORT_ERROR("invalid codepoint", ps);
return 0;
}
p += 5;
break;
}
default:
// leave untouched
*d++ = c;
break;
}
} else {
*d++ = c;
}
}
JSON_REPORT_ERROR("no closing quote for string", s);
return 0;
}
static char *parse_key(const char **key, char *p,
json_unicode_encoder encoder) {
// on '}' return with *p=='}'
char c;
while ((c = *p++)) {
if (c == '"') {
*key = unescape_string(p, &p, encoder);
if (!*key)
return 0; // propagate error
while (*p && IS_WHITESPACE(*p))
p++;
if (*p == ':')
return p + 1;
JSON_REPORT_ERROR("unexpected chars", p);
return 0;
} else if (IS_WHITESPACE(c) || c == ',') {
// continue
} else if (c == '}') {
return p - 1;
} else {
JSON_REPORT_ERROR("unexpected chars", p - 1);
return 0; // error
}
}
JSON_REPORT_ERROR("unexpected chars", p - 1);
return 0; // error
}
static char *parse_value(json *parent, const char *key, char *p,
json_unicode_encoder encoder) {
json *js;
while (1) {
switch (*p) {
case '\0':
JSON_REPORT_ERROR("unexpected end of text", p);
return 0; // error
case ' ':
case '\t':
case '\n':
case '\r':
case ',':
// skip
p++;
break;
case '{':
js = create_json(JSON_OBJECT, key, parent);
p++;
while (1) {
const char *new_key;
p = parse_key(&new_key, p, encoder);
if (!p)
return 0; // error
if (*p == '}')
return p + 1; // end of object
p = parse_value(js, new_key, p, encoder);
if (!p)
return 0; // error
}
case '[':
js = create_json(JSON_ARRAY, key, parent);
p++;
while (1) {
p = parse_value(js, 0, p, encoder);
if (!p)
return 0; // error
if (*p == ']')
return p + 1; // end of array
}
case ']':
return p;
case '"':
p++;
js = create_json(JSON_STRING, key, parent);
js->json_string = unescape_string(p, &p, encoder);
if (!js->json_string)
return 0; // propagate error
return p;
case '-':
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9': {
js = create_json(JSON_INTEGER, key, parent);
char *pe;
errno = 0;
js->json_integer = strtoll(p, &pe, 0);
if (pe == p || errno == ERANGE) {
JSON_REPORT_ERROR("invalid number", p);
return 0; // error
}
if (*pe == '.' || *pe == 'e' || *pe == 'E') { // double value
js->type = JSON_DOUBLE;
errno = 0;
js->json_double = strtod(p, &pe);
if (pe == p || errno == ERANGE) {
JSON_REPORT_ERROR("invalid number", p);
return 0; // error
}
} else {
js->json_double = js->json_integer;
}
return pe;
}
case 't':
if (!strncmp(p, "true", 4)) {
js = create_json(JSON_BOOL, key, parent);
js->json_bool = 1;
return p + 4;
}
JSON_REPORT_ERROR("unexpected chars", p);
return 0; // error
case 'f':
if (!strncmp(p, "false", 5)) {
js = create_json(JSON_BOOL, key, parent);
js->json_bool = 0;
return p + 5;
}
JSON_REPORT_ERROR("unexpected chars", p);
return 0; // error
case 'n':
if (!strncmp(p, "null", 4)) {
create_json(JSON_NULL, key, parent);
return p + 4;
}
JSON_REPORT_ERROR("unexpected chars", p);
return 0; // error
default:
JSON_REPORT_ERROR("unexpected chars", p);
return 0; // error
}
}
}
inline char *unescape_json_string_literal(const char *str) {
// replace \" with "
char *unescaped = (char *)malloc(strlen(str) + 1);
char *p = unescaped;
while (*str) {
if (*str == '\\' && *(str + 1) == '\"') {
*p++ = '"';
str += 2;
} else {
*p++ = *str++;
}
}
*p = '\0';
return unescaped;
}
inline char *read_file(char *filename) {
FILE *f = fopen(filename, "rt");
assert(f);
fseek(f, 0, SEEK_END);
long length = ftell(f);
fseek(f, 0, SEEK_SET);
char *buffer = (char *)malloc(length + 1);
buffer[length] = '\0';
fread(buffer, 1, length, f);
fclose(f);
return buffer;
}
inline void json_print(const json *parsed_json) {
json_recursive_print(parsed_json, 0);
}
inline void json_recursive_print(const json *parsed_json, int depth) {
json *js;
for (js = parsed_json->child; js; js = js->next) {
if (depth == 0) {
printf("┼──");
} else {
for (int i = 0; i < depth + 1; i++) {
printf("┼──");
}
}
int type = (int)js->type;
if (js->key) {
printf(" %s: ", js->key);
} else {
printf(" ");
}
switch (type) {
case JSON_NULL:
printf("NULL\n");
break;
case JSON_OBJECT:
printf("OBJECT\n");
json_recursive_print(js, depth + 1);
break;
case JSON_ARRAY:
printf("ARRAY\n");
json_recursive_print(js, depth + 1);
break;
case JSON_STRING:
printf("%s (string)\n", js->json_string);
break;
case JSON_INTEGER:
printf("%lld (int)\n", js->json_integer);
break;
case JSON_DOUBLE:
printf("%lf (double)\n", js->json_double);
break;
case JSON_BOOL:
printf("%s (bool)\n", js->json_bool ? "true" : "false");
break;
default:
printf("?\n");
break;
}
}
}
inline const json *json_parse_utf8(char *text) {
return json_parse(text, unicode_to_utf8);
}
inline const json *json_parse(char *text, json_unicode_encoder encoder) {
json js = {};
if (!parse_value(&js, 0, text, encoder)) {
if (js.child)
json_free(js.child);
return 0;
}
return js.child;
}
inline const json *json_parse_file_utf8(char *file_path) {
return json_parse_file(file_path, unicode_to_utf8);
}
inline const json *json_parse_file(char *file_path,
json_unicode_encoder encoder) {
char *text = read_file(file_path);
json js = {};
if (!parse_value(&js, 0, text, encoder)) {
if (js.child)
json_free(js.child);
return 0;
}
return js.child;
}
inline const json *json_get(const json *parsed_json, const char *key) {
if (!parsed_json || !key)
return &dummy; // never return null
json *js;
for (js = parsed_json->child; js; js = js->next) {
if (js->key && !strcmp(js->key, key))
return js;
}
return &dummy; // never return null
}
inline const json *json_get_nested(const json *parsed_json, const char *key) {
if (!parsed_json || !key)
return &dummy; // never return null
// split key by '.'
char *key_copy = strdup(key);
char *token = strtok(key_copy, ".");
const json *js = parsed_json;
while (token) {
js = json_get(js, token);
if (js == &dummy) {
return &dummy;
}
token = strtok(NULL, ".");
}
free(key_copy);
return js;
}
inline const json *json_item(const json *parsed_json, int idx) {
if (!parsed_json)
return &dummy; // never return null
json *js;
for (js = parsed_json->child; js; js = js->next) {
if (!idx--)
return js;
}
return &dummy; // never return null
}
#endif // JSML_H