Repository navigation
Expand file tree
/
Copy pathjson_object.c
More file actions
523 lines (461 loc) · 19.6 KB
/
Copy pathjson_object.c
File metadata and controls
523 lines (461 loc) · 19.6 KB
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
/*
* Copyright (C) 2016-2017 Redis Labs
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "json_object.h"
/* === JSONObjectCtx === */
void resetJSONObjectCtx(JSONObjectCtx *ctx);
/* === Parser === */
static inline void _pushNode(_JsonParserContext *ctx, Node *n) {
ctx->nodes[ctx->nlen] = n;
ctx->nlen++;
}
static inline Node *_popNode(_JsonParserContext *ctx) {
ctx->nlen--;
return ctx->nodes[ctx->nlen];
}
/* Decalre it. */
static int _AllowedEscapes[];
static int _IsAllowedWhitespace(unsigned c);
inline static int errorCallback(jsonsl_t jsn, jsonsl_error_t err, struct jsonsl_state_st *state, char *errat) {
_JsonParserContext *jpctx = (_JsonParserContext *)jsn->data;
jpctx->err = err;
jpctx->errpos = state->pos_cur;
jsonsl_stop(jsn);
return 0;
}
inline static void pushCallback(jsonsl_t jsn, jsonsl_action_t action, struct jsonsl_state_st *state,
const jsonsl_char_t *at) {
_JsonParserContext *jpctx = (_JsonParserContext *)jsn->data;
Node *n = NULL;
// only objects (dictionaries) and lists (arrays) create a container on push
switch (state->type) {
case JSONSL_T_OBJECT:
n = NewDictNode(1);
_pushNode(jpctx, n);
break;
case JSONSL_T_LIST:
n = NewArrayNode(1);
_pushNode(jpctx, n);
break;
default:
break;
}
}
inline static void popCallback(jsonsl_t jsn, jsonsl_action_t action, struct jsonsl_state_st *state,
const jsonsl_char_t *at) {
_JsonParserContext *jpctx = (_JsonParserContext *)jsn->data;
const char *pos = jsn->base + state->pos_begin; // element starting position
size_t len = state->pos_cur - state->pos_begin; // element length
// popping string and key values means addingg them to the node stack
if (JSONSL_T_STRING == state->type || JSONSL_T_HKEY == state->type) {
char *buffer = NULL; // a temporary buffer for unescaped strings
// ignore the quote marks
pos++;
len--;
// deal with escapes
if (state->nescapes) {
jsonsl_error_t err;
size_t newlen;
buffer = RedisModule_Calloc(len, sizeof(char));
newlen = jsonsl_util_unescape(pos, buffer, len, _AllowedEscapes, &err);
if (!newlen) {
RedisModule_Free(buffer);
errorCallback(jsn, err, state, NULL);
return;
}
pos = buffer;
len = newlen;
}
// push it
Node *n;
if (JSONSL_T_STRING == state->type) n = NewStringNode(pos, len);
else n = NewKeyValNode(pos, len, NULL); // NULL is a placeholder for now
_pushNode(jpctx, n);
if (buffer) RedisModule_Free(buffer);
}
// popped special values are also added to the node stack
if (JSONSL_T_SPECIAL == state->type) {
if (state->special_flags & JSONSL_SPECIALf_NUMERIC) {
if (state->special_flags & (JSONSL_SPECIALf_FLOAT | JSONSL_SPECIALf_EXPONENT)) {
// convert to double
double value;
char *eptr;
errno = 0;
value = strtod (pos, &eptr);
// in lieu of "ERR value is not a double or out of range"
if ((errno == ERANGE && (value == HUGE_VAL || value == -HUGE_VAL)) ||
(errno != 0 && value == 0) ||
isnan(value) ||
(eptr != pos + len)) {
errorCallback(jsn, JSONSL_ERROR_INVALID_NUMBER, state, NULL);
return;
}
_pushNode(jpctx, NewDoubleNode(value));
} else {
// convert long long (int64_t)
long long value;
char *eptr;
errno = 0;
value = strtoll(pos, &eptr, 10);
// in lieu of "ERR value is not an integer or out of range"
if ((errno == ERANGE && (value == LLONG_MAX || value == LLONG_MIN)) ||
(errno != 0 && value == 0) ||
(eptr != pos + len)) {
errorCallback(jsn, JSONSL_ERROR_INVALID_NUMBER, state, NULL);
return;
}
_pushNode(jpctx, NewIntNode((int64_t)value));
}
} else if (state->special_flags & JSONSL_SPECIALf_BOOLEAN) {
_pushNode(jpctx, NewBoolNode(state->special_flags & JSONSL_SPECIALf_TRUE));
} else if (state->special_flags & JSONSL_SPECIALf_NULL) {
_pushNode(jpctx, NULL);
}
}
// anything that pops needs to be set in its parent, except the root element and keys
if (jpctx->nlen > 1 && state->type != JSONSL_T_HKEY) {
NodeType p = jpctx->nodes[jpctx->nlen - 2]->type;
Node *n = NULL;
switch (p) {
case N_DICT:
n = _popNode(jpctx);
Node_DictSetKeyVal(jpctx->nodes[jpctx->nlen - 1], n);
break;
case N_ARRAY:
n = _popNode(jpctx);
Node_ArrayAppend(jpctx->nodes[jpctx->nlen - 1], n);
break;
case N_KEYVAL:
n = _popNode(jpctx);
jpctx->nodes[jpctx->nlen - 1]->value.kvval.val = n;
n = _popNode(jpctx);
Node_DictSetKeyVal(jpctx->nodes[jpctx->nlen - 1], n);
break;
default:
break;
}
}
}
int CreateNodeFromJSON(JSONObjectCtx *ctx, const char *buf, size_t len, Node **node, char **err) {
size_t _off = 0, _len = len;
char *_buf = (char *)buf;
int is_scalar = 0;
// munch any leading whitespaces
while (_IsAllowedWhitespace(_buf[_off]) && _off < _len) _off++;
/* Embed scalars in a list (also avoids JSONSL_ERROR_STRING_OUTSIDE_CONTAINER).
* Copying is necc. evil to avoid messing w/ non-standard string implementations (e.g. sds), but
* forgivable because most scalars are supposed to be short-ish.
*/
if ((is_scalar = ('{' != _buf[_off]) && ('[' != _buf[_off]) && _off < _len)) {
_len = _len - _off + 2;
_buf = RedisModule_Calloc(_len, sizeof(char));
_buf[0] = '[';
_buf[_len - 1] = ']';
memcpy(&_buf[1], &buf[_off], len - _off);
}
/* Reset all and feed the lexer. */
resetJSONObjectCtx(ctx);
jsonsl_feed(ctx->parser, _buf, _len);
/* Check for lexer errors. */
sds serr = sdsempty();
if (JSONSL_ERROR_SUCCESS != ctx->pctx->err) {
serr = sdscatprintf(serr, "ERR JSON lexer error %s at position %zd",
jsonsl_strerror(ctx->pctx->err), ctx->pctx->errpos + 1);
goto error;
}
/* Verify that parsing had ended at level 0. */
if (ctx->parser->level) {
serr = sdscatprintf(serr, "ERR JSON value incomplete - %u containers unterminated",
ctx->parser->level);
goto error;
}
/* Verify that an element. */
if (!ctx->parser->stack[0].nelem) {
serr = sdscatprintf(serr, "ERR JSON value not found");
goto error;
}
/* Finalize. */
if (is_scalar) {
// extract the scalar and discard the wrapper array
Node_ArrayItem(ctx->pctx->nodes[0], 0, node);
Node_ArraySet(ctx->pctx->nodes[0], 0, NULL);
Node_Free(_popNode(ctx->pctx));
RedisModule_Free(_buf);
} else {
*node = _popNode(ctx->pctx);
}
sdsfree(serr);
return JSONOBJECT_OK;
error:
// set error string, if one has been passed
if (err) {
*err = rmstrndup(serr, strlen(serr));
}
// free any nodes that are in the stack
while (ctx->pctx->nlen) Node_Free(_popNode(ctx->pctx));
// if this is a scalar, we need to release the temporary buffer
if (is_scalar)
RedisModule_Free(_buf);
sdsfree(serr);
return JSONOBJECT_ERROR;
}
/* === JSON serializer === */
typedef struct {
sds buf; // serialization buffer
int depth; // current tree depth
int indent; // indentation string length
sds indentstr; // indentaion string
sds newlinestr; // newline string
sds spacestr; // space string
sds delimstr; // delimiter string
} _JSONBuilderContext;
#define _JSONSerialize_Indent(b) \
if (b->indent) \
for (int i = 0; i < b->depth; i++) b->buf = sdscatsds(b->buf, b->indentstr);
inline static void _JSONSerialize_StringValue(Node *n, void *ctx) {
_JSONBuilderContext *b = (_JSONBuilderContext *)ctx;
size_t len = n->value.strval.len;
const char *p = n->value.strval.data;
b->buf = sdsMakeRoomFor(b->buf, len + 2); // we'll need at least as much room as the original
b->buf = sdscatlen(b->buf, "\"", 1);
while (len--) {
switch (*p) {
case '"': // quotation mark
case '\\': // reverse solidus
b->buf = sdscatprintf(b->buf, "\\%c", *p);
break;
case '/': // the standard is clear wrt solidus so we're zealous
b->buf = sdscatlen(b->buf, "\\/", 2);
break;
case '\b': // backspace
b->buf = sdscatlen(b->buf, "\\b", 2);
break;
case '\f': // formfeed
b->buf = sdscatlen(b->buf, "\\f", 2);
break;
case '\n': // newline
b->buf = sdscatlen(b->buf, "\\n", 2);
break;
case '\r': // carriage return
b->buf = sdscatlen(b->buf, "\\r", 2);
break;
case '\t': // horizontal tab
b->buf = sdscatlen(b->buf, "\\t", 2);
break;
default:
if ((unsigned char)*p > 31 && isprint(*p))
b->buf = sdscatprintf(b->buf, "%c", *p);
else
b->buf = sdscatprintf(b->buf, "\\u%04x", (unsigned char)*p);
break;
}
p++;
}
b->buf = sdscatlen(b->buf, "\"", 1);
}
inline static void _JSONSerialize_BeginValue(Node *n, void *ctx) {
_JSONBuilderContext *b = (_JSONBuilderContext *)ctx;
if (!n) { // NULL nodes are literal nulls
b->buf = sdscatlen(b->buf, "null", 4);
} else {
switch (n->type) {
case N_BOOLEAN:
if (n->value.boolval) {
b->buf = sdscatlen(b->buf, "true", 4);
} else {
b->buf = sdscatlen(b->buf, "false", 5);
}
break;
case N_INTEGER:
b->buf = sdscatfmt(b->buf, "%I", n->value.intval);
break;
case N_NUMBER:
if (fabs(floor(n->value.numval) - n->value.numval) <= DBL_EPSILON &&
fabs(n->value.numval) < 1.0e60)
b->buf = sdscatprintf(b->buf, "%.0f", n->value.numval);
else if (fabs(n->value.numval) < 1.0e-6 || fabs(n->value.numval) > 1.0e9)
b->buf = sdscatprintf(b->buf, "%e", n->value.numval);
else
b->buf = sdscatprintf(b->buf, "%.17g", n->value.numval);
break;
case N_STRING:
_JSONSerialize_StringValue(n, b);
break;
case N_KEYVAL:
b->buf = sdscatfmt(b->buf, "\"%s\":%s", n->value.kvval.key, b->spacestr);
break;
case N_DICT:
b->buf = sdscatlen(b->buf, "{", 1);
b->depth++;
if (n->value.dictval.len) {
b->buf = sdscatsds(b->buf, b->newlinestr);
_JSONSerialize_Indent(b);
}
break;
case N_ARRAY:
b->buf = sdscatlen(b->buf, "[", 1);
b->depth++;
if (n->value.arrval.len) {
b->buf = sdscatsds(b->buf, b->newlinestr);
_JSONSerialize_Indent(b);
}
break;
case N_NULL: // keeps the compiler from complaining
break;
} // switch(n->type)
}
}
inline static void _JSONSerialize_EndValue(Node *n, void *ctx) {
_JSONBuilderContext *b = (_JSONBuilderContext *)ctx;
if (n) {
switch (n->type) {
case N_DICT:
if (n->value.dictval.len) {
b->buf = sdscatsds(b->buf, b->newlinestr);
}
b->depth--;
_JSONSerialize_Indent(b);
b->buf = sdscatlen(b->buf, "}", 1);
break;
case N_ARRAY:
if (n->value.arrval.len) {
b->buf = sdscatsds(b->buf, b->newlinestr);
}
b->depth--;
_JSONSerialize_Indent(b);
b->buf = sdscatlen(b->buf, "]", 1);
break;
default: // keeps the compiler from complaining
break;
}
}
}
inline static void _JSONSerialize_ContainerDelimiter(void *ctx) {
_JSONBuilderContext *b = (_JSONBuilderContext *)ctx;
b->buf = sdscat(b->buf, b->delimstr);
_JSONSerialize_Indent(b);
}
void SerializeNodeToJSON(const Node *node, const JSONSerializeOpt *opt, sds *json) {
// set up the builder
_JSONBuilderContext *b = RedisModule_Calloc(1, sizeof(_JSONBuilderContext));
b->indentstr = opt->indentstr ? sdsnew(opt->indentstr) : sdsempty();
b->newlinestr = opt->newlinestr ? sdsnew(opt->newlinestr) : sdsempty();
b->spacestr = opt->spacestr ? sdsnew(opt->spacestr) : sdsempty();
b->indent = sdslen(b->indentstr);
b->delimstr = sdsnewlen(",", 1);
b->delimstr = sdscat(b->delimstr, b->newlinestr);
NodeSerializerOpt nso = {.fBegin = _JSONSerialize_BeginValue,
.xBegin = 0xffff,
.fEnd = _JSONSerialize_EndValue,
.xEnd = (N_DICT | N_ARRAY),
.fDelim = _JSONSerialize_ContainerDelimiter,
.xDelim = (N_DICT | N_ARRAY)};
// the real work
b->buf = *json;
Node_Serializer(node, &nso, b);
*json = b->buf;
sdsfree(b->indentstr);
sdsfree(b->newlinestr);
sdsfree(b->spacestr);
sdsfree(b->delimstr);
RedisModule_Free(b);
}
/* JSONObjectContext */
JSONObjectCtx *NewJSONObjectCtx(int levels) {
JSONObjectCtx *ret = RedisModule_Calloc(1, sizeof(JSONObjectCtx));
// Parser setup
if (0 >= levels || JSONSL_MAX_LEVELS < levels) {
// default to maximium if given a negative, 0 or a value greater than maximum
ret->levels = JSONSL_MAX_LEVELS;
} else {
ret->levels = levels;
}
ret->parser = jsonsl_new(ret->levels);
ret->parser->error_callback = errorCallback;
ret->parser->action_callback_POP = popCallback;
ret->parser->action_callback_PUSH = pushCallback;
jsonsl_enable_all_callbacks(ret->parser);
// Parser context setup
ret->pctx = RedisModule_Calloc(1, sizeof(_JsonParserContext));
ret->pctx->nodes = RedisModule_Calloc(ret->levels, sizeof(Node *));
ret->parser->data = ret->pctx;
return ret;
}
void resetJSONObjectCtx(JSONObjectCtx *ctx) {
_JsonParserContext *jpctx = (_JsonParserContext *)ctx->pctx;
jpctx->err = JSONSL_ERROR_SUCCESS;
jpctx->errpos = 0;
jpctx->nlen = 0;
ctx->parser->stack[0].nelem = 0;
jsonsl_reset(ctx->parser);
}
void FreeJSONObjectCtx(JSONObjectCtx *ctx) {
if (ctx) {
RedisModule_Free(ctx->pctx->nodes);
RedisModule_Free(ctx->pctx);
jsonsl_destroy(ctx->parser);
RedisModule_Free(ctx);
}
}
// clang-format off
// from jsonsl.c
/**
* This table contains entries for the allowed whitespace as per RFC 4627
*/
static int _AllowedWhitespace[0x100] = {
/* 0x00 */ 0,0,0,0,0,0,0,0,0, /* 0x08 */
/* 0x09 */ 1 /* <TAB> */, /* 0x09 */
/* 0x0a */ 1 /* <LF> */, /* 0x0a */
/* 0x0b */ 0,0, /* 0x0c */
/* 0x0d */ 1 /* <CR> */, /* 0x0d */
/* 0x0e */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x1f */
/* 0x20 */ 1 /* <SP> */, /* 0x20 */
/* 0x21 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x40 */
/* 0x41 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x60 */
/* 0x61 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x80 */
/* 0x81 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xa0 */
/* 0xa1 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xc0 */
/* 0xc1 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0xe0 */
/* 0xe1 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* 0xfe */
};
// adapted for use with jsonsl_util_unescape_ex
/**
* Allowable two-character 'common' escapes:
*/
static int _AllowedEscapes[0x80] = {
/* 0x00 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x1f */
/* 0x20 */ 0,0, /* 0x21 */
/* 0x22 */ 1 /* <"> */, /* 0x22 */
/* 0x23 */ 0,0,0,0,0,0,0,0,0,0,0,0, /* 0x2e */
/* 0x2f */ 1 /* </> */, /* 0x2f */
/* 0x30 */ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x4f */
/* 0x50 */ 0,0,0,0,0,0,0,0,0,0,0,0, /* 0x5b */
/* 0x5c */ 1 /* <\> */, /* 0x5c */
/* 0x5d */ 0,0,0,0,0, /* 0x61 */
/* 0x62 */ 1 /* <b> */, /* 0x62 */
/* 0x63 */ 0,0,0, /* 0x65 */
/* 0x66 */ 1 /* <f> */, /* 0x66 */
/* 0x67 */ 0,0,0,0,0,0,0, /* 0x6d */
/* 0x6e */ 1 /* <n> */, /* 0x6e */
/* 0x6f */ 0,0,0, /* 0x71 */
/* 0x72 */ 1 /* <r> */, /* 0x72 */
/* 0x73 */ 0, /* 0x73 */
/* 0x74 */ 1 /* <t> */, /* 0x74 */
/* 0x75 */ 1 /* <u> */, /* 0x75 */
/* 0x76 */ 0,0,0,0,0, /* 0x80 */
};
static int _IsAllowedWhitespace(unsigned c) { return c == ' ' || _AllowedWhitespace[c & 0xff]; }
// clang-format on