2 /* vim: set et ts=3 sw=3 ft=c:
4 * Copyright (C) 2012 James McLaughlin et al. All rights reserved.
5 * https://github.com/udp/json-parser
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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.
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
35 #define json_char char
40 #define json_int_t int64_t
54 unsigned long max_memory;
59 #define json_relaxed_commas 1
74 extern const struct _json_value json_value_none;
76 typedef struct _json_value
78 struct _json_value * parent;
91 json_char * ptr; /* null terminated */
102 struct _json_value * value;
111 struct _json_value ** values;
119 struct _json_value * next_alloc;
125 /* Some C++ operator sugar */
131 inline _json_value ()
132 { memset (this, 0, sizeof (_json_value));
135 inline const struct _json_value &operator [] (int index) const
137 if (type != json_array || index < 0
138 || ((unsigned int) index) >= u.array.length)
140 return json_value_none;
143 return *u.array.values [index];
146 inline const struct _json_value &operator [] (const char * index) const
148 if (type != json_object)
149 return json_value_none;
151 for (unsigned int i = 0; i < u.object.length; ++ i)
152 if (!strcmp (u.object.values [i].name, index))
153 return *u.object.values [i].value;
155 return json_value_none;
158 inline operator const char * () const
170 inline operator json_int_t () const
178 return (json_int_t) u.dbl;
185 inline operator bool () const
187 if (type != json_boolean)
190 return u.boolean != 0;
193 inline operator double () const
198 return (double) u.integer;
212 json_value * json_parse
213 (const json_char * json);
215 json_value * json_parse_ex
216 (json_settings * settings, const json_char * json, char * error);
218 void json_value_free (json_value *);