-
Notifications
You must be signed in to change notification settings - Fork 5
/
json.h
508 lines (450 loc) · 11.7 KB
/
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
/**
* # JSON Parser
*
* An over-engineered JSON parser and serializer.
*
* ## License
*
* Author: Werner Stoop
* This is free and unencumbered software released into the public domain.
* See http://unlicense.org/ for more details.
*
* ## References
*
* * https://www.json.org/json-en.html
* * https://tools.ietf.org/id/draft-ietf-json-rfc4627bis-09.html
* * Unicode related:
* * https://unicodebook.readthedocs.io/index.html
* * https://en.wikipedia.org/wiki/UTF-8
* * https://en.wikipedia.org/wiki/UTF-16
*
*/
#ifndef JSON_H
#define JSON_H
#ifdef __cplusplus
extern "C"
{
#endif
/**
* ## Types
*
* ### `typedef enum json_type JSON_Type;`
*
* Enumeration for the type a `JSON` entity might have.
*
* Values are `j_string`, `j_number`, `j_object`, `j_array`,
* `j_true`, `j_false` and `j_null`.
*
* See `json_get_type()`.
*/
typedef enum json_type {
j_string,
j_number,
j_object,
j_array,
j_true,
j_false,
j_null
} JSON_Type;
/**
* ### `typedef struct json JSON;`
*
* Structure that encapsulates a JSON entity.
*/
typedef struct json JSON;
/**
* ## Utility Function Pointers
*
* These variables can be changed to point to custom functions to
* change the behaviour of the parser.
*/
/**
* ### `extern int (*json_error)(const char *fmt, ...);`
*
* Pointer to a function that that will be used internally
* to display error messages.
*
* The default just performs a `fprintf()` to `stderr`.
*/
extern int (*json_error)(const char *fmt, ...);
/**
* ### `extern char *(*json_readfile)(const char *fname);`
*
* Pointer to a function that will be called by `json_read()` to read
* the entire contents of a file specified by `fname` into memory.
*
* The return value is expected to be allocated on the heap, and `free()`
* will be called on it.
*
* The default uses `fopen()` and `fread()`
*/
extern char *(*json_readfile)(const char *fname);
/**
* ## API Functions
*/
/**
* ### `JSON *json_read(const char *filename);`
*
* Reads and parses a file specified by `filename`
*/
JSON *json_read(const char *filename);
/**
* ### `JSON *json_parse(const char *text);`
*
* Parses a string `text` into a `JSON` entity.
*/
JSON *json_parse(const char *text);
/**
* ### `JSON *json_retain(JSON *j);`
*
* Increments the reference count of a JSON entity.
*/
JSON *json_retain(JSON *j);
/**
* ### `void json_release(JSON *j)`
*
* Decrements the reference count of a JSON entity. If the
* reference count reaches zero the entity is destroyed and
* its memory reclaimed.
*/
void json_release(JSON *j);
/**
* ### `char *json_serialize(JSON *j);`
*
* Serializes a JSON entity into a heap-allocated string.
*/
char *json_serialize(JSON *j);
/**
* ### `char *json_pretty(JSON *j);`
*
* Serializes a JSON entity into a pretty-printed heap-allocated string.
*/
char *json_pretty(JSON *j);
/**
* ### `JSON *json_new_object()`
*
* Creates a new JSON entity containing an empty object.
*/
JSON *json_new_object();
/**
* ### `JSON *json_new_array()`
*
* Creates a new JSON entity conatining an empty array.
*/
JSON *json_new_array();
/**
* ### `JSON *json_new_string(const char *text)`
*
* Creates a new JSON string entity containing the given text.
*/
JSON *json_new_string(const char *text);
/**
* ### `JSON *json_new_number(double n)`
*
* Creates a new JSON number entity with the specified value.
*/
JSON *json_new_number(double n);
/**
* ### `JSON *json_null()`
*
* Returns a JSON entity representing the JSON `null` value.
*/
JSON *json_null();
/**
* ### `JSON *json_true()`
*
* Returns a JSON entity representing the JSON `true` value.
*/
JSON *json_true();
/**
* ### `JSON *json_false()`
*
* Returns a JSON entity representing the JSON `false` value.
*/
JSON *json_false();
/**
* ### `JSON *json_boolean(int value)`
*
* Returns a JSON entity representing either the JSON `true`
* if `value` is non-zero, or `false` otherwise.
*/
JSON *json_boolean(int value);
/**
* ### `JSON_Type json_get_type(JSON *j)`
*
* Returns the type of a JSON entity.
*/
JSON_Type json_get_type(JSON *j);
/**
* ### `int json_is_null(JSON *j)`
*
* Returns non-zero if `j` represents the JSON `null` value
*/
int json_is_null(JSON *j);
/**
* ### `int json_is_boolean(JSON *j)`
*
* Returns non-zero if `j` and only if represents the JSON
* `true` or `false` values.
*/
int json_is_boolean(JSON *j);
/**
* ### `int json_is_true(JSON *j)`
*
* Returns non-zero if `j` represents the JSON `true` value.
*
* It resembles the JavaScript operation `j === true`. See
* `json_is_truthy()` for the less strict version.
*/
int json_is_true(JSON *j);
/**
* ### `int json_is_false(JSON *j)`
*
* Returns non-zero if `j` represents the JSON `false` value.
*
* It resembles the JavaScript operation `j === false`. See
* `json_is_falsey()` for the less strict version.
*/
int json_is_false(JSON *j);
/**
* ### `int json_is_truthy(JSON *j)`
*
* Returns non-zero if `j` represents a JSON [_truthy_][truthy] value.
*
* [truthy]: https://developer.mozilla.org/en-US/docs/Glossary/Truthy
*/
int json_is_truthy(JSON *j);
/**
* ### `int json_is_falsey(JSON *j)`
*
* Returns non-zero if `j` represents a JSON [_falsey_][falsey] value.
*
* [falsey]: https://developer.mozilla.org/en-US/docs/Glossary/Falsy
*/
int json_is_falsey(JSON *j);
/**
* ### `int json_is_number(JSON *j)`
*
* Returns non-zero if `j` represents a numeric JSON value
*/
int json_is_number(JSON *j);
/**
* ### `int json_is_string(JSON *j)`
*
* Returns non-zero if `j` represents a JSON string value
*/
int json_is_string(JSON *j);
/**
* ### `int json_is_object(JSON *j)`
*
* Returns non-zero if `j` is a JSON object
*/
int json_is_object(JSON *j);
/**
* ### `int json_is_array(JSON *j)`
*
* Returns non-zero if `j` is a JSON array
*/
int json_is_array(JSON *j);
/**
* ### `double json_as_number(JSON *j)`
*
* Returns a numeric representation of the JSON entity `j`
*/
double json_as_number(JSON *j);
/**
* ### `const char *json_as_string(JSON *j)`
*
* Returns the string representation of the JSON entity `j`
*/
const char *json_as_string(JSON *j);
/**
* ### `int json_obj_has(JSON *obj, const char *name)`
*
* Returns non-zero if `obj` is a JSON object containing the key `name`,
* zero otherwise.
*/
int json_obj_has(JSON *obj, const char *name);
/**
* ### `const char *json_obj_next(JSON *j, const char *name);`
*
* Returns the next key in a JSON object following `name`.
*
* If name is `NULL`, it will return the first key in the object.
*
* It will return `NULL` if `name` is the last key in the object.
*
* It kan be used for iterating through all the keys in an object,
* for example:
*
* ```
* const char *name = json_obj_next(obj, NULL);
* while(name) {
* JSON *item = json_obj_get(obj, name);
* // --snip--
* name = json_obj_next(obj, name);
* }
* ```
*/
const char *json_obj_next(JSON *j, const char *name);
/**
* ### `JSON *json_obj_get(JSON *obj, const char *name)`
*
* Retrieves the value associated with `name` in the JSON object `obj`.
*
* It returns NULL if `obj` does not contain `name`.
*/
JSON *json_obj_get(JSON *obj, const char *name);
/**
* ### `double json_obj_get_number(JSON *obj, const char *name)`
*
* Returns the JSON entity associated with `name` in the JSON
* object `obj` as a number.
*
* It returns `0.0` if `obj[name]` is not numeric.
*/
double json_obj_get_number(JSON *obj, const char *name);
/**
* ### `double json_obj_get_number_or(JSON *obj, const char *name, double def)`
*
* Returns the JSON entity associated with `name` in the JSON
* object `obj` as a number, or returns a default value `def` if
* `obj[name]` is not numeric.
*/
double json_obj_get_number_or(JSON *obj, const char *name, double def);
/**
* ### `const char *json_obj_get_string(JSON *obj, const char *name)`
*
* Returns the JSON entity associated with `name` in the JSON
* object `obj` as a string.
*
* It returns `NULL` if `obj[name]` is not a string.
*/
const char *json_obj_get_string(JSON *obj, const char *name);
/**
* ### `const char *json_obj_get_string_or(JSON *j, const char *name, const char *def)`
*
* Returns the JSON entity associated with `name` in the JSON
* object `obj` as a string, or returns a default value `def` if
* `obj[name]` is not a string.
*/
const char *json_obj_get_string_or(JSON *j, const char *name, const char *def);
/**
* ### `int json_obj_get_bool(JSON *j, const char *name)`
*
* Returns the JSON entity associated with `name` in the JSON
* object `obj` as a boolean value (zero = false, non-zero is true).
*
* It returns false if `obj[name]` is not boolean.
*/
int json_obj_get_bool(JSON *j, const char *name);
/**
* ### `int json_obj_get_bool(JSON *j, const char *name)`
*
* Returns the JSON entity associated with `name` in the JSON
* object `obj` as a boolean value (zero = false, non-zero is true).
*
* It returns false if `obj[name]` is not boolean.
*/
int json_obj_get_bool_or(JSON *j, const char *name, int def);
/**
* ### `JSON *json_obj_set(JSON *obj, char *k, JSON *v)`
*
* Sets the value associated with `name` in the JSON object `obj` to `v`.
*
* It returns `obj`.
*/
JSON *json_obj_set(JSON *obj, char *k, JSON *v);
/**
* ### `JSON *json_obj_set_number(JSON *obj, char *k, double n)`
*
* Sets the value associated with `name` in the JSON object `obj` to
* the numeric value `n`.
*
* It returns `obj`.
*/
JSON *json_obj_set_number(JSON *obj, char *k, double n);
/**
* ### `JSON *json_obj_set_string(JSON *obj, char *k, const char *str)`
*
* Sets the value associated with `name` in the JSON object `obj` to
* the string value `str`.
*
* It returns `obj`.
*/
JSON *json_obj_set_string(JSON *obj, char *k, const char *str);
/**
* ### `int json_obj_check_type(JSON *obj, const char *name, JSON_Type type)`
*
* Returns non-zero if the value associated with `name` in the JSON object `obj`
* is of the given type `type`.
*
* Returns zero if it is not of the given type, or `name` is not found in `obj`
*/
int json_obj_check_type(JSON *obj, const char *name, JSON_Type type);
/**
* ### `unsigned int json_array_len(JSON *array)`
*
* Returns the length of the JSON array `array`.
*/
unsigned int json_array_len(JSON *array);
/**
* ### `JSON *json_array_reserve(JSON *array, unsigned int n)`
*
* Reserves space for `n` items in the JSON array `array`.
*/
JSON *json_array_reserve(JSON *array, unsigned int n);
/**
* ### `JSON *json_array_get(JSON *array, int n)`
*
* Retrieves the `n`'th element in the JSON array `array`.
*/
JSON *json_array_get(JSON *array, int n);
/**
* ### `double json_array_get_number(JSON *array, int n)`
*
* Retrieves the `n`'th element in the JSON array `array` as a number.
*/
double json_array_get_number(JSON *array, int n);
/**
* ### `const char *json_array_get_string(JSON *array, int n)`
*
* Retrieves the `n`'th element in the JSON array `array` as a string.
*/
const char *json_array_get_string(JSON *array, int n);
/**
* ### `JSON *json_array_set(JSON *array, int n, JSON *v)`
*
* Sets the `n`'th element in the JSON array `array` to the value `v`.
*
* Returns `array`.
*/
JSON *json_array_set(JSON *array, int n, JSON *v);
/**
* ### `JSON *json_array_add(JSON *array, JSON *value)`
*
* Appends `value` to the JSON array `array`.
*
* Returns `array`.
*/
JSON *json_array_add(JSON *array, JSON *value);
/**
* ### `JSON *json_array_add_number(JSON *array, double number)`
*
* Appends the number `number` to the JSON array `array`.
*
* Returns `array`.
*/
JSON *json_array_add_number(JSON *array, double number);
/**
* ### `JSON *json_array_add_string(JSON *array, const char *str)`
*
* Appends the string `str` to the JSON array `array`.
*
* Returns `array`.
*/
JSON *json_array_add_string(JSON *array, const char *str);
#ifdef __cplusplus
} /* extern "C" */
#endif
#endif /* ifdef */