[696dc9e] | 1 | |
---|
[7a80925] | 2 | /* vim: set et ts=3 sw=3 sts=3 ft=c: |
---|
[696dc9e] | 3 | * |
---|
[7a80925] | 4 | * Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved. |
---|
[696dc9e] | 5 | * https://github.com/udp/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 | |
---|
[7a80925] | 38 | #ifndef json_int_t |
---|
| 39 | #ifndef _MSC_VER |
---|
| 40 | #include <inttypes.h> |
---|
| 41 | #define json_int_t int64_t |
---|
| 42 | #else |
---|
| 43 | #define json_int_t __int64 |
---|
| 44 | #endif |
---|
| 45 | #endif |
---|
| 46 | |
---|
| 47 | #include <stdlib.h> |
---|
| 48 | |
---|
[696dc9e] | 49 | #ifdef __cplusplus |
---|
| 50 | |
---|
| 51 | #include <string.h> |
---|
| 52 | |
---|
[5ebff60] | 53 | extern "C" |
---|
| 54 | { |
---|
[696dc9e] | 55 | |
---|
| 56 | #endif |
---|
| 57 | |
---|
[5ebff60] | 58 | typedef struct { |
---|
| 59 | unsigned long max_memory; |
---|
| 60 | int settings; |
---|
[696dc9e] | 61 | |
---|
[5ebff60] | 62 | /* Custom allocator support (leave null to use malloc/free) |
---|
| 63 | */ |
---|
[7a80925] | 64 | |
---|
[5ebff60] | 65 | void * (*mem_alloc)(size_t, int zero, void * user_data); |
---|
| 66 | void (* mem_free)(void *, void * user_data); |
---|
[7a80925] | 67 | |
---|
[5ebff60] | 68 | void * user_data; /* will be passed to mem_alloc and mem_free */ |
---|
[7a80925] | 69 | |
---|
[696dc9e] | 70 | } json_settings; |
---|
| 71 | |
---|
[7a80925] | 72 | #define json_enable_comments 0x01 |
---|
[696dc9e] | 73 | |
---|
[5ebff60] | 74 | typedef enum { |
---|
| 75 | json_none, |
---|
| 76 | json_object, |
---|
| 77 | json_array, |
---|
| 78 | json_integer, |
---|
| 79 | json_double, |
---|
| 80 | json_string, |
---|
| 81 | json_boolean, |
---|
| 82 | json_null |
---|
[696dc9e] | 83 | |
---|
| 84 | } json_type; |
---|
| 85 | |
---|
| 86 | extern const struct _json_value json_value_none; |
---|
| 87 | |
---|
[5ebff60] | 88 | typedef struct _json_value { |
---|
| 89 | struct _json_value * parent; |
---|
[696dc9e] | 90 | |
---|
[5ebff60] | 91 | json_type type; |
---|
[696dc9e] | 92 | |
---|
[5ebff60] | 93 | union { |
---|
| 94 | int boolean; |
---|
| 95 | json_int_t integer; |
---|
| 96 | double dbl; |
---|
[696dc9e] | 97 | |
---|
[5ebff60] | 98 | struct { |
---|
| 99 | unsigned int length; |
---|
| 100 | json_char * ptr; /* null terminated */ |
---|
[696dc9e] | 101 | |
---|
[5ebff60] | 102 | } string; |
---|
[696dc9e] | 103 | |
---|
[5ebff60] | 104 | struct { |
---|
| 105 | unsigned int length; |
---|
[696dc9e] | 106 | |
---|
[5ebff60] | 107 | struct { |
---|
| 108 | json_char * name; |
---|
| 109 | unsigned int name_length; |
---|
[7a80925] | 110 | |
---|
[5ebff60] | 111 | struct _json_value * value; |
---|
[696dc9e] | 112 | |
---|
[5ebff60] | 113 | } * values; |
---|
[696dc9e] | 114 | |
---|
[5ebff60] | 115 | #if defined(__cplusplus) && __cplusplus >= 201103L |
---|
| 116 | decltype(values) begin() const |
---|
| 117 | { return values; } |
---|
| 118 | decltype(values) end() const |
---|
| 119 | { return values + length; } |
---|
| 120 | #endif |
---|
[7a80925] | 121 | |
---|
[5ebff60] | 122 | } object; |
---|
[696dc9e] | 123 | |
---|
[5ebff60] | 124 | struct { |
---|
| 125 | unsigned int length; |
---|
| 126 | struct _json_value ** values; |
---|
[696dc9e] | 127 | |
---|
[5ebff60] | 128 | #if defined(__cplusplus) && __cplusplus >= 201103L |
---|
| 129 | decltype(values) begin() const |
---|
| 130 | { return values; } |
---|
| 131 | decltype(values) end() const |
---|
| 132 | { return values + length; } |
---|
| 133 | #endif |
---|
[7a80925] | 134 | |
---|
[5ebff60] | 135 | } array; |
---|
[696dc9e] | 136 | |
---|
[5ebff60] | 137 | } u; |
---|
[696dc9e] | 138 | |
---|
[5ebff60] | 139 | union { |
---|
| 140 | struct _json_value * next_alloc; |
---|
| 141 | void * object_mem; |
---|
[696dc9e] | 142 | |
---|
[5ebff60] | 143 | } _reserved; |
---|
[696dc9e] | 144 | |
---|
| 145 | |
---|
[5ebff60] | 146 | /* Some C++ operator sugar */ |
---|
[696dc9e] | 147 | |
---|
| 148 | #ifdef __cplusplus |
---|
| 149 | |
---|
[5ebff60] | 150 | public: |
---|
| 151 | |
---|
| 152 | inline _json_value () |
---|
| 153 | { |
---|
| 154 | memset(this, 0, sizeof(_json_value)); |
---|
| 155 | } |
---|
| 156 | |
---|
| 157 | inline const struct _json_value &operator [](int index) const |
---|
| 158 | { |
---|
| 159 | if (type != json_array || index < 0 |
---|
| 160 | || ((unsigned int)index) >= u.array.length) { |
---|
| 161 | return json_value_none; |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | return *u.array.values [index]; |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | inline const struct _json_value &operator [](const char * index) const |
---|
| 168 | { |
---|
| 169 | if (type != json_object) { |
---|
| 170 | return json_value_none; |
---|
| 171 | } |
---|
| 172 | |
---|
| 173 | for (unsigned int i = 0; i < u.object.length; ++i) { |
---|
| 174 | if (!strcmp(u.object.values [i].name, index)) { |
---|
| 175 | return *u.object.values [i].value; |
---|
| 176 | } |
---|
| 177 | } |
---|
| 178 | |
---|
| 179 | return json_value_none; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | inline operator const char *() const |
---|
| 183 | { |
---|
| 184 | switch (type) { |
---|
| 185 | case json_string: |
---|
| 186 | return u.string.ptr; |
---|
| 187 | |
---|
| 188 | default: |
---|
| 189 | return ""; |
---|
| 190 | } |
---|
| 191 | ; |
---|
| 192 | } |
---|
| 193 | |
---|
| 194 | inline operator json_int_t() const |
---|
| 195 | { |
---|
| 196 | switch (type) { |
---|
| 197 | case json_integer: |
---|
| 198 | return u.integer; |
---|
| 199 | |
---|
| 200 | case json_double: |
---|
| 201 | return (json_int_t)u.dbl; |
---|
| 202 | |
---|
| 203 | default: |
---|
| 204 | return 0; |
---|
| 205 | } |
---|
| 206 | ; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | inline operator bool() const |
---|
| 210 | { |
---|
| 211 | if (type != json_boolean) { |
---|
| 212 | return false; |
---|
| 213 | } |
---|
| 214 | |
---|
| 215 | return u.boolean != 0; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | inline operator double() const |
---|
| 219 | { |
---|
| 220 | switch (type) { |
---|
| 221 | case json_integer: |
---|
| 222 | return (double)u.integer; |
---|
| 223 | |
---|
| 224 | case json_double: |
---|
| 225 | return u.dbl; |
---|
| 226 | |
---|
| 227 | default: |
---|
| 228 | return 0; |
---|
| 229 | } |
---|
| 230 | ; |
---|
| 231 | } |
---|
[696dc9e] | 232 | |
---|
| 233 | #endif |
---|
| 234 | |
---|
| 235 | } json_value; |
---|
| 236 | |
---|
[5ebff60] | 237 | json_value * json_parse(const json_char * json, |
---|
| 238 | size_t length); |
---|
[696dc9e] | 239 | |
---|
[7a80925] | 240 | #define json_error_max 128 |
---|
[5ebff60] | 241 | json_value * json_parse_ex(json_settings * settings, |
---|
| 242 | const json_char * json, |
---|
| 243 | size_t length, |
---|
| 244 | char * error); |
---|
[696dc9e] | 245 | |
---|
[5ebff60] | 246 | void json_value_free(json_value *); |
---|
[696dc9e] | 247 | |
---|
| 248 | |
---|
[7a80925] | 249 | /* Not usually necessary, unless you used a custom mem_alloc and now want to |
---|
| 250 | * use a custom mem_free. |
---|
| 251 | */ |
---|
[5ebff60] | 252 | void json_value_free_ex(json_settings * settings, |
---|
| 253 | json_value *); |
---|
[7a80925] | 254 | |
---|
| 255 | |
---|
[696dc9e] | 256 | #ifdef __cplusplus |
---|
[5ebff60] | 257 | } /* extern "C" */ |
---|
[696dc9e] | 258 | #endif |
---|
| 259 | |
---|
| 260 | #endif |
---|
| 261 | |
---|
| 262 | |
---|