-
Notifications
You must be signed in to change notification settings - Fork 3
/
json-build.h
579 lines (541 loc) · 17.2 KB
/
json-build.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
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
/*
* Special thanks to Christopher Wellons (aka skeeto) for giving valuable
* feedback that helped improve this lib.
*
* See: https://www.reddit.com/r/C_Programming/comments/sf95m3/comment/huojrjn
*/
#ifndef JSON_BUILD_H
#define JSON_BUILD_H
#include <stddef.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef JSONB_STATIC
#define JSONB_API static
#else
#define JSONB_API extern
#endif
#ifndef JSONB_MAX_DEPTH
/**
* Maximum JSON nesting depth, if default value is unwanted then it should be
* defined before json-build.h is included:
*
* #define JSONB_MAX_DEPTH 256
* #include "json-build.h"
*/
#define JSONB_MAX_DEPTH 128
#endif /* JSONB_MAX_DEPTH */
/** @brief json-builder return codes */
typedef enum jsonbcode {
/** no error, operation was a success */
JSONB_OK = 0,
/** string is complete, expects no more inputs */
JSONB_END,
/** not enough tokens were provided */
JSONB_ERROR_NOMEM = -1,
/** token doesn't match expected value */
JSONB_ERROR_INPUT = -2,
/** operation would lead to out of boundaries access */
JSONB_ERROR_STACK = -3
} jsonbcode;
/** @brief json-builder serializing state */
enum jsonbstate {
JSONB_INIT = 0,
JSONB_ARRAY_OR_OBJECT_OR_VALUE = JSONB_INIT,
JSONB_OBJECT_KEY_OR_CLOSE,
JSONB_OBJECT_VALUE,
JSONB_OBJECT_NEXT_KEY_OR_CLOSE,
JSONB_ARRAY_VALUE_OR_CLOSE,
JSONB_ARRAY_NEXT_VALUE_OR_CLOSE,
JSONB_ERROR,
JSONB_DONE
};
/** @brief Handle for building a JSON string */
typedef struct jsonb {
/** state stack to keep track and enforce next inputs */
enum jsonbstate stack[JSONB_MAX_DEPTH + 1];
/** pointer to stack top */
enum jsonbstate *top;
/** offset in the JSON buffer (current length) */
size_t pos;
} jsonb;
/**
* @brief Reset a jsonb handle buffer's position tracker (for streaming purposes)
* @note Should be used in conjunction with @ref JSONB_ERROR_NOMEM if the
* buffer is meant to be used as a stream
*
* @param builder pointer to the @ref jsonb handle
*/
#define jsonb_reset(builder) ((builder)->pos = 0)
/**
* @brief Initialize a jsonb handle
*
* @param builder the handle to be initialized
*/
JSONB_API void jsonb_init(jsonb *builder);
/**
* @brief Push an object to the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_object(jsonb *builder, char buf[], size_t bufsize);
/**
* @brief Pop an object from the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_object_pop(jsonb *builder,
char buf[],
size_t bufsize);
/**
* @brief Push a key to the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @param key the key to be inserted
* @param len the key length
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_key(
jsonb *builder, char buf[], size_t bufsize, const char key[], size_t len);
/**
* @brief Push an array to the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_array(jsonb *builder, char buf[], size_t bufsize);
/**
* @brief Pop an array from the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_array_pop(jsonb *builder,
char buf[],
size_t bufsize);
/**
* @brief Push a raw JSON token to the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @param token the token to be inserted
* @param len the token length
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_token(jsonb *builder,
char buf[],
size_t bufsize,
const char token[],
size_t len);
/**
* @brief Push a boolean token to the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @param boolean the boolean to be inserted
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_bool(jsonb *builder,
char buf[],
size_t bufsize,
int boolean);
/**
* @brief Push a null token to the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_null(jsonb *builder, char buf[], size_t bufsize);
/**
* @brief Push a string token to the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @param str the string to be inserted
* @param len the string length
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_string(
jsonb *builder, char buf[], size_t bufsize, const char str[], size_t len);
/**
* @brief Push a number token to the builder
*
* @param builder the builder initialized with jsonb_init()
* @param buf the JSON buffer
* @param bufsize the JSON buffer size
* @param number the number to be inserted
* @return @ref jsonbcode value
*/
JSONB_API jsonbcode jsonb_number(jsonb *builder,
char buf[],
size_t bufsize,
double number);
#ifndef JSONB_HEADER
#include <stdio.h>
#ifndef JSONB_DEBUG
#define TRACE(prev, next) next
#define DECORATOR(a)
#else
static const char *
_jsonb_eval_state(enum jsonbstate state)
{
switch (state) {
case JSONB_ARRAY_OR_OBJECT_OR_VALUE: return "array or object or value";
case JSONB_OBJECT_KEY_OR_CLOSE: return "object key or close";
case JSONB_OBJECT_NEXT_KEY_OR_CLOSE: return "object next key or close";
case JSONB_OBJECT_VALUE: return "object value";
case JSONB_ARRAY_VALUE_OR_CLOSE: return "array value or close";
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE: return "array next value or close";
case JSONB_ERROR: return "error";
case JSONB_DONE: return "done";
default: return "unknown";
}
}
#define TRACE(prev, next) \
do { \
enum jsonbstate _prev = prev, _next = next; \
fprintf(stderr, "%s():L%d | %s -> %s\n", __func__, __LINE__, \
_jsonb_eval_state(_prev), _jsonb_eval_state(_next)); \
} while (0)
#define DECORATOR(d) d
#endif /* JSONB_DEBUG */
#define STACK_HEAD(b, state) *(b)->top = (state)
#define STACK_PUSH(b, state) TRACE(*(b)->top, *++(b)->top = (state))
#define STACK_POP(b) TRACE(*(b)->top, DECORATOR(*)--(b)->top)
#define BUFFER_COPY_CHAR(b, c, _pos, buf, bufsize) \
do { \
if ((b)->pos + (_pos) + 1 + 1 > (bufsize)) { \
(buf)[(b)->pos] = '\0'; \
return JSONB_ERROR_NOMEM; \
} \
(buf)[(b)->pos + (_pos)++] = (c); \
(buf)[(b)->pos + (_pos)] = '\0'; \
} while (0)
#define BUFFER_COPY(b, value, len, _pos, buf, bufsize) \
do { \
size_t i; \
if ((b)->pos + (_pos) + (len) + 1 > (bufsize)) { \
(buf)[(b)->pos] = '\0'; \
return JSONB_ERROR_NOMEM; \
} \
for (i = 0; i < (len); ++i) \
(buf)[(b)->pos + (_pos) + i] = (value)[i]; \
(_pos) += (len); \
(buf)[(b)->pos + (_pos)] = '\0'; \
} while (0)
JSONB_API void
jsonb_init(jsonb *b)
{
static jsonb empty_builder;
*b = empty_builder;
b->top = b->stack;
}
JSONB_API jsonbcode
jsonb_object(jsonb *b, char buf[], size_t bufsize)
{
enum jsonbstate new_state;
size_t pos = 0;
if (b->top - b->stack >= JSONB_MAX_DEPTH) return JSONB_ERROR_STACK;
switch (*b->top) {
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE:
BUFFER_COPY_CHAR(b, ',', pos, buf, bufsize);
/* fall-through */
case JSONB_ARRAY_VALUE_OR_CLOSE:
new_state = JSONB_ARRAY_NEXT_VALUE_OR_CLOSE;
break;
case JSONB_OBJECT_VALUE:
new_state = JSONB_OBJECT_NEXT_KEY_OR_CLOSE;
break;
case JSONB_ARRAY_OR_OBJECT_OR_VALUE:
new_state = JSONB_DONE;
break;
default:
STACK_HEAD(b, JSONB_ERROR);
/* fall-through */
case JSONB_DONE:
case JSONB_ERROR:
return JSONB_ERROR_INPUT;
}
BUFFER_COPY_CHAR(b, '{', pos, buf, bufsize);
STACK_HEAD(b, new_state);
STACK_PUSH(b, JSONB_OBJECT_KEY_OR_CLOSE);
b->pos += pos;
return JSONB_OK;
}
JSONB_API jsonbcode
jsonb_object_pop(jsonb *b, char buf[], size_t bufsize)
{
enum jsonbcode code;
size_t pos = 0;
switch (*b->top) {
case JSONB_OBJECT_KEY_OR_CLOSE:
case JSONB_OBJECT_NEXT_KEY_OR_CLOSE:
code = b->stack == b->top - 1 ? JSONB_END : JSONB_OK;
break;
default:
STACK_HEAD(b, JSONB_ERROR);
/* fall-through */
case JSONB_DONE:
case JSONB_ERROR:
return JSONB_ERROR_INPUT;
}
BUFFER_COPY_CHAR(b, '}', pos, buf, bufsize);
STACK_POP(b);
b->pos += pos;
return code;
}
static jsonbcode
_jsonb_escape(
size_t *pos, char buf[], size_t bufsize, const char str[], size_t len)
{
char *esc_tok = NULL, _esc_tok[8] = "\\u00";
char *esc_buf = NULL;
int extra_bytes = 0;
size_t i;
second_iter:
/* 1st iteration, esc_buf is NULL and count extra_bytes needed for escaping
* 2st iteration, esc_buf is not NULL, and does escaping. */
for (i = 0; i < len; ++i) {
unsigned char c = str[i];
esc_tok = NULL;
switch (c) {
case 0x22: esc_tok = "\\\""; break;
case 0x5C: esc_tok = "\\\\"; break;
case '\b': esc_tok = "\\b"; break;
case '\f': esc_tok = "\\f"; break;
case '\n': esc_tok = "\\n"; break;
case '\r': esc_tok = "\\r"; break;
case '\t': esc_tok = "\\t"; break;
default: if (c <= 0x1F) {
static const char tohex[] = "0123456789abcdef";
_esc_tok[4] = tohex[c >> 4];
_esc_tok[5] = tohex[c & 0xF];
_esc_tok[6] = 0;
esc_tok = _esc_tok;
}
}
if (esc_tok) {
int j;
for (j = 0; esc_tok[j]; j++) {
if (!esc_buf) /* count how many extra bytes are needed */
continue;
*esc_buf++ = esc_tok[j];
}
extra_bytes += j - 1;
}
else if (esc_buf) {
*esc_buf++ = c;
}
}
if (*pos + len + extra_bytes > bufsize) {
*buf = '\0';
return JSONB_ERROR_NOMEM;
}
if (esc_buf) {
*pos += len + extra_bytes;
return JSONB_OK;
}
if (!extra_bytes) {
size_t j;
for (j = 0; j < len; ++j)
buf[*pos + j] = str[j];
*pos += len;
return JSONB_OK;
}
esc_buf = buf + *pos;
extra_bytes = 0;
goto second_iter;
}
JSONB_API jsonbcode
jsonb_key(jsonb *b, char buf[], size_t bufsize, const char key[], size_t len)
{
size_t pos = 0;
switch (*b->top) {
case JSONB_OBJECT_NEXT_KEY_OR_CLOSE:
BUFFER_COPY_CHAR(b, ',', pos, buf, bufsize);
/* fall-through */
case JSONB_OBJECT_KEY_OR_CLOSE: {
enum jsonbcode ret;
BUFFER_COPY_CHAR(b, '"', pos, buf, bufsize);
ret = _jsonb_escape(&pos, buf + b->pos, bufsize - b->pos, key, len);
if (ret != JSONB_OK) return ret;
BUFFER_COPY(b, "\":", 2, pos, buf, bufsize);
STACK_HEAD(b, JSONB_OBJECT_VALUE);
} break;
default:
STACK_HEAD(b, JSONB_ERROR);
/* fall-through */
case JSONB_DONE:
return JSONB_ERROR_INPUT;
}
b->pos += pos;
return JSONB_OK;
}
JSONB_API jsonbcode
jsonb_array(jsonb *b, char buf[], size_t bufsize)
{
enum jsonbstate new_state;
size_t pos = 0;
if (b->top - b->stack >= JSONB_MAX_DEPTH) return JSONB_ERROR_STACK;
switch (*b->top) {
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE:
BUFFER_COPY_CHAR(b, ',', pos, buf, bufsize);
/* fall-through */
case JSONB_ARRAY_VALUE_OR_CLOSE:
new_state = JSONB_ARRAY_NEXT_VALUE_OR_CLOSE;
break;
case JSONB_OBJECT_VALUE:
new_state = JSONB_OBJECT_NEXT_KEY_OR_CLOSE;
break;
case JSONB_ARRAY_OR_OBJECT_OR_VALUE:
new_state = JSONB_DONE;
break;
default:
STACK_HEAD(b, JSONB_ERROR);
/* fall-through */
case JSONB_DONE:
case JSONB_ERROR:
return JSONB_ERROR_INPUT;
}
BUFFER_COPY_CHAR(b, '[', pos, buf, bufsize);
STACK_HEAD(b, new_state);
STACK_PUSH(b, JSONB_ARRAY_VALUE_OR_CLOSE);
b->pos += pos;
return JSONB_OK;
}
JSONB_API jsonbcode
jsonb_array_pop(jsonb *b, char buf[], size_t bufsize)
{
enum jsonbcode code;
size_t pos = 0;
switch (*b->top) {
case JSONB_ARRAY_VALUE_OR_CLOSE:
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE:
code = b->stack == b->top - 1 ? JSONB_END : JSONB_OK;
break;
default:
STACK_HEAD(b, JSONB_ERROR);
/* fall-through */
case JSONB_DONE:
case JSONB_ERROR:
return JSONB_ERROR_INPUT;
}
BUFFER_COPY_CHAR(b, ']', pos, buf, bufsize);
STACK_POP(b);
b->pos += pos;
return code;
}
JSONB_API jsonbcode
jsonb_token(
jsonb *b, char buf[], size_t bufsize, const char token[], size_t len)
{
enum jsonbstate next_state;
enum jsonbcode code;
size_t pos = 0;
switch (*b->top) {
case JSONB_ARRAY_OR_OBJECT_OR_VALUE:
next_state = JSONB_DONE;
code = JSONB_END;
break;
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE:
BUFFER_COPY_CHAR(b, ',', pos, buf, bufsize);
/* fall-through */
case JSONB_ARRAY_VALUE_OR_CLOSE:
next_state = JSONB_ARRAY_NEXT_VALUE_OR_CLOSE;
code = JSONB_OK;
break;
case JSONB_OBJECT_VALUE:
next_state = JSONB_OBJECT_NEXT_KEY_OR_CLOSE;
code = JSONB_OK;
break;
default:
STACK_HEAD(b, JSONB_ERROR);
/* fall-through */
case JSONB_DONE:
case JSONB_ERROR:
return JSONB_ERROR_INPUT;
}
BUFFER_COPY(b, token, len, pos, buf, bufsize);
STACK_HEAD(b, next_state);
b->pos += pos;
return code;
}
JSONB_API jsonbcode
jsonb_bool(jsonb *b, char buf[], size_t bufsize, int boolean)
{
if (boolean) return jsonb_token(b, buf, bufsize, "true", 4);
return jsonb_token(b, buf, bufsize, "false", 5);
}
JSONB_API jsonbcode
jsonb_null(jsonb *b, char buf[], size_t bufsize)
{
return jsonb_token(b, buf, bufsize, "null", 4);
}
JSONB_API jsonbcode
jsonb_string(
jsonb *b, char buf[], size_t bufsize, const char str[], size_t len)
{
enum jsonbstate next_state;
enum jsonbcode code, ret;
size_t pos = 0;
switch (*b->top) {
case JSONB_ARRAY_OR_OBJECT_OR_VALUE:
next_state = JSONB_DONE;
code = JSONB_END;
break;
case JSONB_ARRAY_NEXT_VALUE_OR_CLOSE:
BUFFER_COPY_CHAR(b, ',', pos, buf, bufsize);
/* fall-through */
case JSONB_ARRAY_VALUE_OR_CLOSE:
next_state = JSONB_ARRAY_NEXT_VALUE_OR_CLOSE;
code = JSONB_OK;
break;
case JSONB_OBJECT_VALUE:
next_state = JSONB_OBJECT_NEXT_KEY_OR_CLOSE;
code = JSONB_OK;
break;
default:
STACK_HEAD(b, JSONB_ERROR);
/* fall-through */
case JSONB_DONE:
case JSONB_ERROR:
return JSONB_ERROR_INPUT;
}
BUFFER_COPY_CHAR(b, '"', pos, buf, bufsize);
ret = _jsonb_escape(&pos, buf + b->pos, bufsize - b->pos, str, len);
if (ret != JSONB_OK) return ret;
BUFFER_COPY_CHAR(b, '"', pos, buf, bufsize);
STACK_HEAD(b, next_state);
b->pos += pos;
return code;
}
JSONB_API jsonbcode
jsonb_number(jsonb *b, char buf[], size_t bufsize, double number)
{
char token[32];
long len = sprintf(token, "%.17G", number);
if (len < 0) return JSONB_ERROR_INPUT;
return jsonb_token(b, buf, bufsize, token, len);
}
#endif /* JSONB_HEADER */
#ifdef __cplusplus
}
#endif
#endif /* JSON_BUILD_H */