source: lib/json.h @ 2906268

Last change on this file since 2906268 was 5ebff60, checked in by dequis <dx@…>, at 2015-02-20T22:50:54Z

Reindent everything to K&R style with tabs

Used uncrustify, with the configuration file in ./doc/uncrustify.cfg

Commit author set to "Indent <please@…>" so that it's easier to
skip while doing git blame.

  • Property mode set to 100644
File size: 5.1 KB
Line 
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
56#endif
57
58typedef struct {
59        unsigned long max_memory;
60        int settings;
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
70} json_settings;
71
72#define json_enable_comments  0x01
73
74typedef 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
83
84} json_type;
85
86extern const struct _json_value json_value_none;
87
88typedef struct _json_value {
89        struct _json_value * parent;
90
91        json_type type;
92
93        union {
94                int boolean;
95                json_int_t integer;
96                double dbl;
97
98                struct {
99                        unsigned int length;
100                        json_char * ptr; /* null terminated */
101
102                } string;
103
104                struct {
105                        unsigned int length;
106
107                        struct {
108                                json_char * name;
109                                unsigned int name_length;
110
111                                struct _json_value * value;
112
113                        } * values;
114
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
121
122                } object;
123
124                struct {
125                        unsigned int length;
126                        struct _json_value ** values;
127
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
134
135                } array;
136
137        } u;
138
139        union {
140                struct _json_value * next_alloc;
141                void * object_mem;
142
143        } _reserved;
144
145
146        /* Some C++ operator sugar */
147
148   #ifdef __cplusplus
149
150public:
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        }
232
233   #endif
234
235} json_value;
236
237json_value * json_parse(const json_char * json,
238                        size_t length);
239
240#define json_error_max 128
241json_value * json_parse_ex(json_settings * settings,
242                           const json_char * json,
243                           size_t length,
244                           char * error);
245
246void json_value_free(json_value *);
247
248
249/* Not usually necessary, unless you used a custom mem_alloc and now want to
250 * use a custom mem_free.
251 */
252void json_value_free_ex(json_settings * settings,
253                        json_value *);
254
255
256#ifdef __cplusplus
257}    /* extern "C" */
258#endif
259
260#endif
261
262
Note: See TracBrowser for help on using the repository browser.