-
Notifications
You must be signed in to change notification settings - Fork 0
/
pl_json.h
550 lines (462 loc) · 13.8 KB
/
pl_json.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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/*
Copyright (c) 2024, Dominic Szablewski - https://phoboslab.org
SPDX-License-Identifier: MIT
PL_JSON - Yet another single header json parser
-- About
PL_JSON decodes JSON into a structure that is easy to use.
This library does not check the JSON data for conformance. Escape sequences
such as '\n', '\r', or '\\' are handled, but unicode escape sequences (\uffff)
are not - these will be replaced by a single '?' character. So use JSON that
has utf8 strings instead of unicode escape sequences.
The caller is responsible for memory allocation. To facilitate memory efficient
use, the parsing is split into two steps:
1) tokenization into a temporary token buffer
2) parsing of the tokens into the final structure
-- Synopsis:
// Define `PL_JSON_IMPLEMENTATION` in *one* C/C++ file before including this
// library to create the implementation.
#define PL_JSON_IMPLEMENTATION
#include "pl_json.h"
// Read file
FILE *fh = fopen(path, "rb");
fseek(fh, 0, SEEK_END);
int size = ftell(fh);
fseek(fh, 0, SEEK_SET);
char *json_text = malloc(size);
fread(json_text, 1, size, fh);
fclose(fh);
// Tokenize JSON. Allocate some temporary memory that is big enough to hold the
// expected number of tokens. The maximum number of tokens for a given byte size
// can be no larger than 1 + size / 2
// E.g. for the worst case `[0,0,0]` = 7 bytes, 4 tokens
unsigned int tokens_capacity = 1 + size / 2;
json_token_t *tokens = malloc(tokens_capacity * sizeof(json_token_t));
unsigned int size_req;
unsigned int tokens_len = json_tokenize(json_text, size, tokens, tokens_capacity, &size_req);
assert(tokens_len > 0);
// Parse tokens. Allocate memory for the final json object. The size_req
// returned by json_tokenize is always sufficient and close to optimal.
json_t *v = malloc(size_req);
json_parse_tokens(json_text, tokens, tokens_len, v);
// The json_text and tokens are no longer required and can be freed.
free(json_text);
free(tokens);
// Assuming the input `[1, 2, 3, "hello", 4, "world"]`, print the array of
// numbers and strings:
if (v->type == JSON_ARRAY) {
json_t *values = v->data;
for (int i = 0; i < v->len; i++) {
if (values[i].type == JSON_STRING) {
printf("string with length %d: %s\n", values[i].len, values[i].string);
}
else if (values[i].type == JSON_NUMBER) {
printf("number: %f\n", values[i].number);
}
}
}
// Some convenience functions can be used to access the data of json_t; these
// functions are null-safe and check the underlying type. So doing something
// like this is always fine, as long as you check the final return value:
char *str = json_string(json_value_for_key(v, "foobar"));
if (str != null) {
printf("string value for key foobar: %s", str);
}
// See below for the documentation of these functions.
// Finally, when you're done with the parsed json, just free it again.
free(v);
*/
/* -----------------------------------------------------------------------------
Header - Public functions */
#ifndef PL_JSON_H
#define PL_JSON_H
#ifdef __cplusplus
extern "C" {
#endif
typedef enum {
JSON_NULL,
JSON_TRUE,
JSON_FALSE,
JSON_NUMBER,
JSON_STRING,
JSON_ARRAY,
JSON_OBJECT
} json_type_t;
typedef struct {
json_type_t type;
unsigned int pos;
unsigned int len;
} json_token_t;
typedef struct json_t {
json_type_t type;
unsigned int len;
union {
double number;
char *string;
struct json_t *values;
};
} json_t;
typedef enum {
JSON_ERROR_INVALID = -1,
JSON_ERROR_MAX_TOKENS = -2,
JSON_ERROR_MAX_DEPTH = -3,
} json_error_t;
// Tokenize JSON data
// - len denotes the length of the input JSON. If data is a null terminated
// string and the length is not know, it is safe to set len to 0xffffffff.
// - tokens is a pointer to a user allocated array of tokens of tokens_len.
// - parsed_size_req will be set to the number of bytes required for json_parse_tokens
// Returns the number of tokens parsed, or a negative value corresponding to
// the json_error_t enum if the input is invalid or the given tokens_len was not
// sufficient.
int json_tokenize(char *data, unsigned int len, json_token_t *tokens, unsigned int tokens_len, unsigned int *parsed_size_req);
// Parse tokens into json_t
// - data MUST be the same data as for json_tokenize
// - len is the number of tokens to parse (i.e. the return value from json_tokenize)
// - json is a pointer to user allocated memory of sufficient size, ie. the
// parsed_size_req from json_tokenize
void json_parse_tokens(char *data, json_token_t *tokens, unsigned int len, json_t *json);
// The actual number for NUMBER, 1 for TRUE, 0 for all other types.
double json_number(json_t *v);
// 1 for truthy values (TRUE, non-zero NUMBER, non-empty STRING,
// non-empty OBJECT or non-empty ARRAY) or 0 for all others.
int json_bool(json_t *v);
// A pointer to a NULL terminated string for STRING or NULL
char *json_string(json_t *v);
// A pointer to an array of v->len of json_t for ARRAY or OBJECT or NULL
json_t *json_values(json_t *v);
// A pointer to the i-th value of an ARRAY or OBJECT or NULL
json_t *json_value_at(json_t *v, unsigned int i);
// A pointer to an array of v->len of pointers to NULL terminated strings
// for OBJECT or NULL
char **json_keys(json_t *v);
// A NULL terminated string for the i-th key for OBJECT or NULL
char *json_key_at(json_t *v, unsigned int i);
// A pointer to the value for the key for OBJECT or NULL. Uses linear search, so
// be mindful about performance with objects with more than a few hundred keys.
json_t *json_value_for_key(json_t *v, char *key);
#ifdef __cplusplus
}
#endif
#endif /* PL_JSON_H */
/* -----------------------------------------------------------------------------
Implementation */
#ifdef PL_JSON_IMPLEMENTATION
#include <string.h>
#include <stdlib.h>
#define JSON_MAX_DEPTH 256
typedef struct {
json_error_t error;
char *data;
unsigned int data_len;
unsigned int pos;
json_token_t *tokens;
unsigned int tokens_capacity;
unsigned int tokens_len;
unsigned int tokens_pos;
unsigned int parsed_size;
unsigned char *parsed_data;
unsigned int parsed_data_len;
} json_parser_t;
enum {
JSON_C_NULL = (1<<0),
JSON_C_SPACE = (1<<1),
JSON_C_LF = (1<<2),
JSON_C_NUM = (1<<3),
JSON_C_EXP = (1<<5),
JSON_C_PRIM = (1<<6),
JSON_C_OBJ = (1<<7)
};
static const unsigned char json_char_map[256] = {
['\0'] = JSON_C_NULL, ['\t'] = JSON_C_SPACE, ['\n'] = JSON_C_LF,
['\r'] = JSON_C_SPACE, [' '] = JSON_C_SPACE, ['+'] = JSON_C_EXP,
['-'] = JSON_C_NUM, ['.'] = JSON_C_EXP, ['0'] = JSON_C_NUM,
['1'] = JSON_C_NUM, ['2'] = JSON_C_NUM, ['3'] = JSON_C_NUM,
['4'] = JSON_C_NUM, ['5'] = JSON_C_NUM, ['6'] = JSON_C_NUM,
['7'] = JSON_C_NUM, ['8'] = JSON_C_NUM, ['9'] = JSON_C_NUM,
['n'] = JSON_C_PRIM, ['t'] = JSON_C_PRIM, ['f'] = JSON_C_PRIM,
['"'] = JSON_C_OBJ, ['['] = JSON_C_OBJ, ['{'] = JSON_C_OBJ,
['E'] = JSON_C_EXP, ['e'] = JSON_C_EXP,
};
#define json_char_is(C, TYPE) (json_char_map[(unsigned char)(C)] & (TYPE))
static inline char json_next(json_parser_t *parser) {
if (parser->pos >= parser->data_len) {
return '\0';
}
return parser->data[parser->pos++];
}
static inline char json_next_non_whitespace(json_parser_t *parser) {
char c;
do {
c = json_next(parser);
} while (json_char_is(c, JSON_C_SPACE | JSON_C_LF));
return c;
}
static json_token_t *json_tokenize_descent(json_parser_t *parser, unsigned int depth) {
if (depth > JSON_MAX_DEPTH) {
parser->error = JSON_ERROR_MAX_DEPTH;
return NULL;
}
if (parser->tokens_len >= parser->tokens_capacity) {
parser->error = JSON_ERROR_MAX_TOKENS;
return NULL;
}
parser->parsed_size += sizeof(json_t);
char c = json_next_non_whitespace(parser);
json_token_t *token = &parser->tokens[parser->tokens_len++];
token->pos = parser->pos - 1;
token->len = 0;
if (json_char_is(c, JSON_C_OBJ)) {
if (c == '"') {
token->type = JSON_STRING;
token->pos++;
c = json_next(parser);
while (c != '"') {
if (c == '\\') {
c = json_next(parser);
}
else if (json_char_is(c, JSON_C_NULL | JSON_C_LF)) {
return NULL;
}
c = json_next(parser);
}
token->len = parser->pos - token->pos - 1;
parser->parsed_size += token->len + 1;
return token;
}
else if (c == '{') {
token->type = JSON_OBJECT;
c = json_next_non_whitespace(parser);
if (c == '}') {
return token;
}
while (1) {
parser->pos--;
token->len++;
json_token_t *key = json_tokenize_descent(parser, depth+1);
if (!key || key->type != JSON_STRING) {
return NULL;
}
c = json_next_non_whitespace(parser);
if (c != ':') {
return NULL;
}
if (!json_tokenize_descent(parser, depth+1)) {
return NULL;
}
c = json_next_non_whitespace(parser);
if (c == ',') {
c = json_next_non_whitespace(parser);
}
else if (c == '}') {
return token;
}
else {
return NULL;
}
}
}
else /* if (c == '[') */ {
token->type = JSON_ARRAY;
c = json_next_non_whitespace(parser);
if (c == ']') {
return token;
}
while (1) {
parser->pos--;
token->len++;
if (!json_tokenize_descent(parser, depth+1)) {
return NULL;
}
c = json_next_non_whitespace(parser);
if (c == ',') {
c = json_next_non_whitespace(parser);
}
else if (c == ']') {
return token;
}
else {
return NULL;
}
}
}
}
else if (json_char_is(c, JSON_C_PRIM)) {
if (c == 'n') {
token->type = JSON_NULL;
parser->pos += 3;
}
else if (c == 't') {
token->type = JSON_TRUE;
parser->pos += 3;
}
else /* if (c == 'f') */ {
token->type = JSON_FALSE;
parser->pos += 4;
}
return token;
}
else if (json_char_is(c, JSON_C_NUM)) {
token->type = JSON_NUMBER;
do {
c = json_next(parser);
} while (json_char_is(c, JSON_C_NUM | JSON_C_EXP));
parser->pos--;
token->len = parser->pos - token->pos;
if (token->len > 63) {
return NULL;
}
return token;
}
return NULL;
}
static inline void *json_bump_alloc(json_parser_t *parser, unsigned int size) {
void *p = parser->parsed_data + parser->parsed_data_len;
parser->parsed_data_len += size;
return p;
}
static unsigned int json_parse_string(char *dst, char *src, unsigned int len) {
unsigned int di = 0;
for (unsigned int si = 0; si < len; si++, di++) {
char c = src[si];
if (c == '\\') {
c = src[++si];
switch (c) {
case 'r': c = '\r'; break;
case 'n': c = '\n'; break;
case 'b': c = '\b'; break;
case 'f': c = '\f'; break;
case 't': c = '\t'; break;
case 'u': c = '?'; si += 4; break;
}
}
dst[di] = c;
}
dst[di] = '\0';
return di;
}
static void json_parse_descent(json_parser_t *parser, json_t *v) {
json_token_t *t = &parser->tokens[parser->tokens_pos++];
v->type = t->type;
if (v->type == JSON_NULL) {
v->number = 0;
}
else if (v->type == JSON_TRUE) {
v->number = 1;
}
else if (v->type == JSON_FALSE) {
v->number = 0;
}
else if (v->type == JSON_NUMBER) {
char buf[64];
memcpy((void *)buf, &parser->data[t->pos], t->len);
buf[t->len] = '\0';
v->number = atof(buf);
}
else if (v->type == JSON_STRING) {
char *str = json_bump_alloc(parser, t->len + 1);
v->len = json_parse_string(str, &parser->data[t->pos], t->len);
v->string = str;
}
else if (v->type == JSON_OBJECT) {
json_t *values = json_bump_alloc(parser, sizeof(json_t) * t->len);
char **keys = json_bump_alloc(parser, sizeof(char*) * t->len);
v->values = values;
v->len = t->len;
for (unsigned int i = 0; i < t->len; i++) {
json_token_t *key = &parser->tokens[parser->tokens_pos++];
keys[i] = json_bump_alloc(parser, key->len + 1);
json_parse_string(keys[i], &parser->data[key->pos], key->len);
json_parse_descent(parser, &values[i]);
}
}
else if (v->type == JSON_ARRAY) {
json_t *values = json_bump_alloc(parser, sizeof(json_t) * t->len);
v->len = t->len;
v->values = values;
for (unsigned int i = 0; i < t->len; i++) {
json_parse_descent(parser, &values[i]);
}
}
}
// Public API ------------------------------------------------------------------
int json_tokenize(char *data, unsigned int len, json_token_t *tokens, unsigned int tokens_len, unsigned int *parsed_size_req) {
json_parser_t parser = {
.error = JSON_ERROR_INVALID,
.data = data,
.data_len = len,
.tokens = tokens,
.tokens_capacity = tokens_len
};
if (!json_tokenize_descent(&parser, 0)) {
return parser.error;
}
*parsed_size_req = parser.parsed_size;
return parser.tokens_len;
}
void json_parse_tokens(char *data, json_token_t *tokens, unsigned int tokens_len, json_t *json) {
json_parser_t parser = {
.data = data,
.tokens = tokens,
.tokens_len = tokens_len,
.parsed_data = (unsigned char *)json,
};
json_parse_descent(&parser, json_bump_alloc(&parser, sizeof(json_t)));
}
double json_number(json_t *v) {
if (!v || v->type > JSON_NUMBER) {
return 0;
}
return v->number;
}
int json_bool(json_t *v) {
if (!v) {
return 0;
}
if (v->type > JSON_NUMBER) {
return (v->len > 0);
}
return (v->number != 0);
}
char *json_string(json_t *v) {
if (!v || v->type != JSON_STRING) {
return NULL;
}
return v->string;
}
json_t *json_values(json_t *v) {
if (!v || v->len == 0 || v->type < JSON_ARRAY) {
return NULL;
}
return v->values;
}
json_t *json_value_at(json_t *v, unsigned int i) {
if (!v || i >= v->len || v->type < JSON_ARRAY) {
return NULL;
}
return v->values + i;
}
char **json_keys(json_t *v) {
if (!v || v->len == 0 || v->type != JSON_OBJECT) {
return NULL;
}
return (char **)(v->values + v->len);
}
char *json_key_at(json_t *v, unsigned int i) {
if (!v || i >= v->len || v->type != JSON_OBJECT) {
return NULL;
}
return (char *)(((char **)(v->values + v->len)) + i);
}
json_t *json_value_for_key(json_t *v, char *key) {
if (!v || v->type != JSON_OBJECT) {
return NULL;
}
char **keys = json_keys(v);
for (unsigned int i = 0; i < v->len; i++) {
if (strcmp(keys[i], key) == 0) {
return json_value_at(v, i);
}
}
return NULL;
}
#endif /* PL_JSON_IMPLEMENTATION */