| 1 | |
| 2 | /* vim: set et ts=3 sw=3 sts=3 ft=c: |
| 3 | * |
| 4 | * Copyright (C) 2012-2021 the json-parser authors All rights reserved. |
| 5 | * https://github.com/json-parser/json-parser |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following conditions |
| 9 | * are met: |
| 10 | * |
| 11 | * 1. Redistributions of source code must retain the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer. |
| 13 | * |
| 14 | * 2. Redistributions in binary form must reproduce the above copyright |
| 15 | * notice, this list of conditions and the following disclaimer in the |
| 16 | * documentation and/or other materials provided with the distribution. |
| 17 | * |
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND |
| 19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE |
| 22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
| 26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
| 27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 28 | * SUCH DAMAGE. |
| 29 | */ |
| 30 | |
| 31 | #ifndef _JSON_H |
| 32 | #define _JSON_H |
| 33 | |
| 34 | #ifndef json_char |
| 35 | #define json_char char |
| 36 | #endif |
| 37 | |
| 38 | #ifndef json_int_t |
| 39 | #undef JSON_INT_T_OVERRIDDEN |
| 40 | #if defined(_MSC_VER) |
| 41 | #define json_int_t __int64 |
| 42 | #elif (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__cplusplus) && __cplusplus >= 201103L) |
| 43 | /* C99 and C++11 */ |
| 44 | #include <stdint.h> |
| 45 | #define json_int_t int_fast64_t |
| 46 | #else |
| 47 | /* C89 */ |
| 48 | #define json_int_t long |
| 49 | #endif |
| 50 | #else |
| 51 | #define JSON_INT_T_OVERRIDDEN 1 |
| 52 | #endif |
| 53 | |
| 54 | #include <stddef.h> |
| 55 | |
| 56 | #ifdef __cplusplus |
| 57 | |
| 58 | #include <string.h> |
| 59 | |
| 60 | extern "C" |
| 61 | { |
| 62 | |
| 63 | #endif |
| 64 | |
| 65 | typedef struct |
| 66 | { |
| 67 | unsigned long max_memory; /* should be size_t, but would modify the API */ |
| 68 | int settings; |
| 69 | |
| 70 | /* Custom allocator support (leave null to use malloc/free) |
| 71 | */ |
| 72 | |
| 73 | void * (* mem_alloc) (size_t, int zero, void * user_data); |
| 74 | void (* mem_free) (void *, void * user_data); |
| 75 | |
| 76 | void * user_data; /* will be passed to mem_alloc and mem_free */ |
| 77 | |
| 78 | size_t ; /* how much extra space to allocate for values? */ |
| 79 | |
| 80 | } json_settings; |
| 81 | |
| 82 | #define 0x01 |
| 83 | |
| 84 | typedef enum |
| 85 | { |
| 86 | json_none, |
| 87 | json_object, |
| 88 | json_array, |
| 89 | json_integer, |
| 90 | json_double, |
| 91 | json_string, |
| 92 | json_boolean, |
| 93 | json_null |
| 94 | |
| 95 | } json_type; |
| 96 | |
| 97 | extern const struct _json_value json_value_none; |
| 98 | |
| 99 | typedef struct _json_object_entry |
| 100 | { |
| 101 | json_char * name; |
| 102 | unsigned int name_length; |
| 103 | |
| 104 | struct _json_value * value; |
| 105 | |
| 106 | } json_object_entry; |
| 107 | |
| 108 | typedef struct _json_value |
| 109 | { |
| 110 | struct _json_value * parent; |
| 111 | |
| 112 | json_type type; |
| 113 | |
| 114 | union |
| 115 | { |
| 116 | int boolean; |
| 117 | json_int_t integer; |
| 118 | double dbl; |
| 119 | |
| 120 | struct |
| 121 | { |
| 122 | unsigned int length; |
| 123 | json_char * ptr; /* null terminated */ |
| 124 | |
| 125 | } string; |
| 126 | |
| 127 | struct |
| 128 | { |
| 129 | unsigned int length; |
| 130 | |
| 131 | json_object_entry * values; |
| 132 | |
| 133 | #if defined(__cplusplus) |
| 134 | json_object_entry * begin () const |
| 135 | { return values; |
| 136 | } |
| 137 | json_object_entry * end () const |
| 138 | { return values + length; |
| 139 | } |
| 140 | #endif |
| 141 | |
| 142 | } object; |
| 143 | |
| 144 | struct |
| 145 | { |
| 146 | unsigned int length; |
| 147 | struct _json_value ** values; |
| 148 | |
| 149 | #if defined(__cplusplus) |
| 150 | _json_value ** begin () const |
| 151 | { return values; |
| 152 | } |
| 153 | _json_value ** end () const |
| 154 | { return values + length; |
| 155 | } |
| 156 | #endif |
| 157 | |
| 158 | } array; |
| 159 | |
| 160 | } u; |
| 161 | |
| 162 | union |
| 163 | { |
| 164 | struct _json_value * next_alloc; |
| 165 | void * object_mem; |
| 166 | |
| 167 | } _reserved; |
| 168 | |
| 169 | #ifdef JSON_TRACK_SOURCE |
| 170 | |
| 171 | /* Location of the value in the source JSON |
| 172 | */ |
| 173 | unsigned int line, col; |
| 174 | |
| 175 | #endif |
| 176 | |
| 177 | |
| 178 | /* Some C++ operator sugar */ |
| 179 | |
| 180 | #ifdef __cplusplus |
| 181 | |
| 182 | public: |
| 183 | |
| 184 | inline _json_value () |
| 185 | { memset (s: this, c: 0, n: sizeof (_json_value)); |
| 186 | } |
| 187 | |
| 188 | inline const struct _json_value &operator [] (int index) const |
| 189 | { |
| 190 | if (type != json_array || index < 0 |
| 191 | || ((unsigned int) index) >= u.array.length) |
| 192 | { |
| 193 | return json_value_none; |
| 194 | } |
| 195 | |
| 196 | return *u.array.values [index]; |
| 197 | } |
| 198 | |
| 199 | inline const struct _json_value &operator [] (const char * index) const |
| 200 | { |
| 201 | if (type != json_object) |
| 202 | return json_value_none; |
| 203 | |
| 204 | for (unsigned int i = 0; i < u.object.length; ++ i) |
| 205 | if (!strcmp (s1: u.object.values [i].name, s2: index)) |
| 206 | return *u.object.values [i].value; |
| 207 | |
| 208 | return json_value_none; |
| 209 | } |
| 210 | |
| 211 | inline operator const char * () const |
| 212 | { |
| 213 | switch (type) |
| 214 | { |
| 215 | case json_string: |
| 216 | return u.string.ptr; |
| 217 | |
| 218 | default: |
| 219 | return "" ; |
| 220 | }; |
| 221 | } |
| 222 | |
| 223 | inline operator json_int_t () const |
| 224 | { |
| 225 | switch (type) |
| 226 | { |
| 227 | case json_integer: |
| 228 | return u.integer; |
| 229 | |
| 230 | case json_double: |
| 231 | return (json_int_t) u.dbl; |
| 232 | |
| 233 | default: |
| 234 | return 0; |
| 235 | }; |
| 236 | } |
| 237 | |
| 238 | inline operator bool () const |
| 239 | { |
| 240 | if (type != json_boolean) |
| 241 | return false; |
| 242 | |
| 243 | return u.boolean != 0; |
| 244 | } |
| 245 | |
| 246 | inline operator double () const |
| 247 | { |
| 248 | switch (type) |
| 249 | { |
| 250 | case json_integer: |
| 251 | return (double) u.integer; |
| 252 | |
| 253 | case json_double: |
| 254 | return u.dbl; |
| 255 | |
| 256 | default: |
| 257 | return 0; |
| 258 | }; |
| 259 | } |
| 260 | |
| 261 | #endif |
| 262 | |
| 263 | } json_value; |
| 264 | |
| 265 | json_value * json_parse (const json_char * json, |
| 266 | size_t length); |
| 267 | |
| 268 | #define json_error_max 128 |
| 269 | json_value * json_parse_ex (json_settings * settings, |
| 270 | const json_char * json, |
| 271 | size_t length, |
| 272 | char * error); |
| 273 | |
| 274 | void json_value_free (json_value *); |
| 275 | |
| 276 | |
| 277 | /* Not usually necessary, unless you used a custom mem_alloc and now want to |
| 278 | * use a custom mem_free. |
| 279 | */ |
| 280 | void json_value_free_ex (json_settings * settings, |
| 281 | json_value *); |
| 282 | |
| 283 | |
| 284 | #ifdef __cplusplus |
| 285 | } /* extern "C" */ |
| 286 | #endif |
| 287 | |
| 288 | #endif |
| 289 | |