James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file re_json.h Interface to JavaScript Object Notation (JSON) -- RFC 7159 |
| 3 | * |
| 4 | * Copyright (C) 2010 - 2015 Creytiv.com |
| 5 | */ |
| 6 | |
| 7 | enum json_typ { |
| 8 | JSON_STRING, |
| 9 | JSON_INT, |
| 10 | JSON_DOUBLE, |
| 11 | JSON_BOOL, |
| 12 | JSON_NULL, |
| 13 | }; |
| 14 | |
| 15 | struct json_value { |
| 16 | union { |
| 17 | char *str; |
| 18 | int64_t integer; |
| 19 | double dbl; |
| 20 | bool boolean; |
| 21 | } v; |
| 22 | enum json_typ type; |
| 23 | }; |
| 24 | |
| 25 | struct json_handlers; |
| 26 | |
| 27 | typedef int (json_object_entry_h)(const char *name, |
| 28 | const struct json_value *value, void *arg); |
| 29 | typedef int (json_array_entry_h)(unsigned idx, |
| 30 | const struct json_value *value, void *arg); |
| 31 | typedef int (json_object_h)(const char *name, unsigned idx, |
| 32 | struct json_handlers *h); |
| 33 | typedef int (json_array_h)(const char *name, unsigned idx, |
| 34 | struct json_handlers *h); |
| 35 | |
| 36 | struct json_handlers { |
| 37 | json_object_h *oh; |
| 38 | json_array_h *ah; |
| 39 | json_object_entry_h *oeh; |
| 40 | json_array_entry_h *aeh; |
| 41 | void *arg; |
| 42 | }; |
| 43 | |
| 44 | int json_decode(const char *str, size_t len, unsigned maxdepth, |
| 45 | json_object_h *oh, json_array_h *ah, |
| 46 | json_object_entry_h *oeh, json_array_entry_h *aeh, void *arg); |
| 47 | |
| 48 | int json_decode_odict(struct odict **op, uint32_t hash_size, const char *str, |
| 49 | size_t len, unsigned maxdepth); |
| 50 | int json_encode_odict(struct re_printf *pf, const struct odict *o); |