-
Notifications
You must be signed in to change notification settings - Fork 0
/
ujson_reader.h
543 lines (488 loc) · 14 KB
/
ujson_reader.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
// SPDX-License-Identifier: LGPL-2.1-or-later
/*
* Copyright (C) 2021-2024 Cyril Hrubis <metan@ucw.cz>
*/
/**
* @file ujson_reader.h
* @brief A recursive descend JSON parser.
*
* All the function that parse JSON return zero on success and non-zero on a
* failure. Once an error has happened all subsequent attempts to parse more
* return with non-zero exit status immediatelly. This is designed so that we
* can parse several values without checking each return value and only check
* if error has happened at the end of the sequence.
*/
#ifndef UJSON_READER_H
#define UJSON_READER_H
#include <stdio.h>
#include <ujson_common.h>
/**
* @brief An ujson_reader initializer with default values.
*
* @param buf A pointer to a buffer with JSON data.
* @param buf_len A JSON data buffer lenght.
* @param rflags enum ujson_reader_flags.
*
* @return An ujson_reader initialized with default values.
*/
#define UJSON_READER_INIT(buf, buf_len, rflags) { \
.max_depth = UJSON_RECURSION_MAX, \
.err_print = UJSON_ERR_PRINT, \
.err_print_priv = UJSON_ERR_PRINT_PRIV, \
.json = buf, \
.len = buf_len, \
.flags = rflags \
}
/** @brief Reader flags. */
enum ujson_reader_flags {
/** @brief If set warnings are treated as errors. */
UJSON_READER_STRICT = 0x01,
};
/**
* @brief A JSON parser internal state.
*/
struct ujson_reader {
/** Pointer to a null terminated JSON string */
const char *json;
/** A length of the JSON string */
size_t len;
/** A current offset into the JSON string */
size_t off;
/** An offset to the start of the last array or object */
size_t sub_off;
/** Recursion depth increased when array/object is entered decreased on leave */
unsigned int depth;
/** Maximal recursion depth */
unsigned int max_depth;
/** Reader flags. */
enum ujson_reader_flags flags;
/** Handler to print errors and warnings */
void (*err_print)(void *err_print_priv, const char *line);
void *err_print_priv;
char err[UJSON_ERR_MAX];
char buf[];
};
/**
* @brief An ujson_val initializer.
*
* @param sbuf A pointer to a buffer used for string values.
* @param sbuf_size A length of the buffer used for string values.
*
* @return An ujson_val initialized with default values.
*/
#define UJSON_VAL_INIT(sbuf, sbuf_size) { \
.buf = sbuf, \
.buf_size = sbuf_size, \
}
/**
* @brief A parsed JSON key value pair.
*/
struct ujson_val {
/**
* @brief A value type
*
* UJSON_VALUE_VOID means that no value was parsed.
*/
enum ujson_type type;
/** An user supplied buffer and size to store a string values to. */
char *buf;
size_t buf_size;
/**
* @brief An index to attribute list.
*
* This is set by the ujson_obj_first_filter() and
* ujson_obj_next_filter() functions.
*/
size_t idx;
/** An union to store the parsed value into. */
union {
/** @brief A boolean value. */
int val_bool;
/** @brief An integer value. */
long long val_int;
/** @brief A string value. */
const char *val_str;
};
/**
* @brief A floating point value.
*
* Since integer values are subset of floating point values val_float
* is always set when val_int was set.
*/
double val_float;
/** @brief An ID for object values */
char id[UJSON_ID_MAX];
char buf__[];
};
/**
* @brief Allocates a JSON value.
*
* @param buf_size A maximal buffer size for a string value, pass 0 for default.
* @return A newly allocated JSON value.
*/
ujson_val *ujson_val_alloc(size_t buf_size);
/**
* @brief Frees a JSON value.
*
* @param self A JSON value previously allocated by ujson_val_alloc().
*/
void ujson_val_free(ujson_val *self);
/**
* @brief Checks is result has valid type.
*
* @param res An ujson value.
* @return Zero if result is not valid, non-zero otherwise.
*/
static inline int ujson_val_valid(struct ujson_val *res)
{
return !!res->type;
}
/**
* @brief Fills the reader error.
*
* Once buffer error is set all parsing functions return immediatelly with type
* set to UJSON_VOID.
*
* @param self An ujson_reader
* @param fmt A printf like format string
* @param ... A printf like parameters
*/
void ujson_err(ujson_reader *self, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
/**
* @brief Prints error stored in the buffer.
*
* The error takes into consideration the current offset in the buffer and
* prints a few preceding lines along with the exact position of the error.
*
* The error is passed to the err_print() handler.
*
* @param self A ujson_reader
*/
void ujson_err_print(ujson_reader *self);
/**
* @brief Prints a warning.
*
* Uses the print handler in the buffer to print a warning along with a few
* lines of context from the JSON at the current position.
*
* @param self A ujson_reader
* @param fmt A printf-like error string.
* @param ... A printf-like parameters.
*/
void ujson_warn(ujson_reader *self, const char *fmt, ...)
__attribute__((format(printf, 2, 3)));
/**
* @brief Returns true if error was encountered.
*
* @param self A ujson_reader
* @return True if error was encountered false otherwise.
*/
static inline int ujson_reader_err(ujson_reader *self)
{
return !!self->err[0];
}
/**
* @brief Returns the type of next element in buffer.
*
* @param self An ujson_reader
* @return A type of next element in the buffer.
*/
enum ujson_type ujson_next_type(ujson_reader *self);
/**
* @brief Returns if first element in JSON is object or array.
*
* @param self A ujson_reader
* @return On success returns UJSON_OBJ or UJSON_ARR. On failure UJSON_VOID.
*/
enum ujson_type ujson_reader_start(ujson_reader *self);
/**
* @brief Starts parsing of a JSON object.
*
* @param self An ujson_reader
* @param res An ujson_val to store the parsed value to.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_obj_first(ujson_reader *self, struct ujson_val *res);
/**
* @brief Parses next value from a JSON object.
*
* If the res->type is UJSON_OBJ or UJSON_ARR it has to be parsed or skipped
* before next call to this function.
*
* @param self An ujson_reader.
* @param res A ujson_val to store the parsed value to.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_obj_next(ujson_reader *self, struct ujson_val *res);
/**
* @brief A loop over a JSON object.
*
* @code
* UJSON_OBJ_FOREACH(reader, val) {
* printf("Got value id '%s' type '%s'", val->id, ujson_type_name(val->type));
* ...
* }
* @endcode
*
* @param self An ujson_reader.
* @param res An ujson_val to store the next parsed value to.
*/
#define UJSON_OBJ_FOREACH(self, res) \
for (ujson_obj_first(self, res); ujson_val_valid(res); ujson_obj_next(self, res))
/**
* @brief Utility function for log(n) lookup in a sorted array.
*
* @param list Analphabetically sorted array.
* @param list_len Array length.
*
* @return An array index or (size_t)-1 if key wasn't found.
*/
size_t ujson_lookup(const void *arr, size_t memb_size, size_t list_len,
const char *key);
/**
* @brief A JSON object attribute description i.e. key and type.
*/
typedef struct ujson_obj_attr {
/** @brief A JSON object key name. */
const char *key;
/**
* @brief A JSON object value type.
*
* Note that because integer numbers are subset of floating point
* numbers if requested type was UJSON_FLOAT it will match if parsed
* type was UJSON_INT and the val_float will be set in addition to
* val_int.
*/
enum ujson_type type;
} ujson_obj_attr;
/** @brief A JSON object description */
typedef struct ujson_obj {
/**
* @brief A list of attributes.
*
* Attributes we are looking for, the parser sets the val->idx for these.
*/
const ujson_obj_attr *attrs;
/** @brief A size of attrs array. */
size_t attr_cnt;
} ujson_obj;
static inline size_t ujson_obj_lookup(const ujson_obj *obj, const char *key)
{
return ujson_lookup(obj->attrs, sizeof(*obj->attrs), obj->attr_cnt, key);
}
/** @brief An ujson_obj_attr initializer. */
#define UJSON_OBJ_ATTR(keyv, typev) \
{.key = keyv, .type = typev}
/** @brief An ujson_obj_attr intializer with an array index. */
#define UJSON_OBJ_ATTR_IDX(key_idx, keyv, typev) \
[key_idx] = {.key = keyv, .type = typev}
/**
* @brief Starts parsing of a JSON object with attribute lists.
*
* @param self An ujson_reader.
* @param res An ujson_val to store the parsed value to.
* @param obj An ujson_obj object description.
* @param ign A list of keys to ignore.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_obj_first_filter(ujson_reader *self, struct ujson_val *res,
const struct ujson_obj *obj, const struct ujson_obj *ign);
/**
* @brief An empty object attribute list.
*
* To be passed to UJSON_OBJ_FOREACH_FITLER() as ignore list.
*/
extern const struct ujson_obj *ujson_empty_obj;
/**
* @brief Parses next value from a JSON object with attribute lists.
*
* If the res->type is UJSON_OBJ or UJSON_ARR it has to be parsed or skipped
* before next call to this function.
*
* @param self An ujson_reader.
* @param res An ujson_val to store the parsed value to.
* @param obj An ujson_obj object description.
* @param ign A list of keys to ignore. If set to NULL all unknown keys are
* ignored, if set to ujson_empty_obj all unknown keys produce warnings.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_obj_next_filter(ujson_reader *self, struct ujson_val *res,
const struct ujson_obj *obj, const struct ujson_obj *ign);
/**
* @brief A loop over a JSON object with a pre-defined list of expected attributes.
*
* @code
* static struct ujson_obj_attr attrs[] = {
* UJSON_OBJ_ATTR("bool", UJSON_BOOL),
* UJSON_OBJ_ATTR("number", UJSON_INT),
* };
*
* static struct ujson_obj obj = {
* .attrs = filter_attrs,
* .attr_cnt = UJSON_ARRAY_SIZE(filter_attrs)
* };
*
* UJSON_OBJ_FOREACH_FILTER(reader, val, &obj, NULL) {
* printf("Got value id '%s' type '%s'",
* attrs[val->idx].id, ujson_type_name(val->type));
* ...
* }
* @endcode
*
* @param self An ujson_reader.
* @param res An ujson_val to store the next parsed value to.
* @param obj An ujson_obj with a description of attributes to parse.
* @param ign An ujson_obj with a description of attributes to ignore.
*/
#define UJSON_OBJ_FOREACH_FILTER(self, res, obj, ign) \
for (ujson_obj_first_filter(self, res, obj, ign); \
ujson_val_valid(res); \
ujson_obj_next_filter(self, res, obj, ign))
/**
* @brief Skips parsing of a JSON object.
*
* @param self An ujson_reader.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_obj_skip(ujson_reader *self);
/**
* @brief Starts parsing of a JSON array.
*
* @param self An ujson_reader.
* @param res An ujson_val to store the parsed value to.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_arr_first(ujson_reader *self, struct ujson_val *res);
/**
* @brief Parses next value from a JSON array.
*
* If the res->type is UJSON_OBJ or UJSON_ARR it has to be parsed or skipped
* before next call to this function.
*
* @param self An ujson_reader.
* @param res An ujson_val to store the parsed value to.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_arr_next(ujson_reader *self, struct ujson_val *res);
/**
* @brief A loop over a JSON array.
*
* @code
* UJSON_ARR_FOREACH(reader, val) {
* printf("Got value type '%s'", ujson_type_name(val->type));
* ...
* }
* @endcode
*
* @param self An ujson_reader.
* @param res An ujson_val to store the next parsed value to.
*/
#define UJSON_ARR_FOREACH(self, res) \
for (ujson_arr_first(self, res); ujson_val_valid(res); ujson_arr_next(self, res))
/**
* @brief Skips parsing of a JSON array.
*
* @param self A ujson_reader.
*
* @return Zero on success, non-zero otherwise.
*/
int ujson_arr_skip(ujson_reader *self);
/**
* @brief A JSON reader state.
*/
typedef struct ujson_reader_state {
size_t off;
unsigned int depth;
} ujson_reader_state;
/**
* @brief Returns a parser state at the start of current object/array.
*
* This function could be used for the parser to return to the start of the
* currently parsed object or array.
*
* @param self A ujson_reader
* @return A state that points to a start of the last object or array.
*/
static inline ujson_reader_state ujson_reader_state_save(ujson_reader *self)
{
struct ujson_reader_state ret = {
.off = self->sub_off,
.depth = self->depth,
};
return ret;
}
/**
* @brief Returns the parser to a saved state.
*
* This function could be used for the parser to return to the start of
* object or array saved by t the ujson_reader_state_get() function.
*
* @param self A ujson_reader
* @param state An parser state as returned by the ujson_reader_state_get().
*/
static inline void ujson_reader_state_load(ujson_reader *self, ujson_reader_state state)
{
if (ujson_reader_err(self))
return;
self->off = state.off;
self->sub_off = state.off;
self->depth = state.depth;
}
/**
* @brief Resets the parser to a start.
*
* @param self A ujson_reader
*/
static inline void ujson_reader_reset(ujson_reader *self)
{
self->off = 0;
self->sub_off = 0;
self->depth = 0;
self->err[0] = 0;
}
/**
* @brief Loads a file into an ujson_reader buffer.
*
* The reader has to be later freed by ujson_reader_free().
*
* @param path A path to a file.
* @return A ujson_reader or NULL in a case of a failure.
*/
ujson_reader *ujson_reader_load(const char *path);
/**
* @brief Frees an ujson_reader buffer.
*
* @param self A ujson_reader allocated by ujson_reader_load() function.
*/
void ujson_reader_free(ujson_reader *self);
/**
* @brief Prints errors and warnings at the end of parsing.
*
* Checks if self->err is set and prints the error with ujson_reader_err()
*
* Checks if there is any text left after the parser has finished with
* ujson_reader_consumed() and prints a warning if there were any non-whitespace
* characters left.
*
* @param self A ujson_reader
*/
void ujson_reader_finish(ujson_reader *self);
/**
* @brief Returns non-zero if whole buffer has been consumed.
*
* @param self A ujson_reader.
* @return Non-zero if whole buffer was consumed.
*/
static inline int ujson_reader_consumed(ujson_reader *self)
{
return self->off >= self->len;
}
#endif /* UJSON_H */