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