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