Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epidemian
GitHub Repository: epidemian/gravity
Path: blob/master/src/utils/gravity_json.h
1214 views
1
#include <stddef.h>
2
#include <stdint.h>
3
#include <stdbool.h>
4
5
#ifndef __GRAVITY_JSON_SERIALIZER__
6
#define __GRAVITY_JSON_SERIALIZER__
7
8
// MARK: JSON serializer -
9
10
typedef struct json_t json_t;
11
json_t *json_new (void);
12
void json_begin_object (json_t *json, const char *key);
13
void json_end_object (json_t *json);
14
void json_begin_array (json_t *json, const char *key);
15
void json_end_array (json_t *json);
16
void json_add_cstring (json_t *json, const char *key, const char *value);
17
void json_add_string (json_t *json, const char *key, const char *value, size_t len);
18
void json_add_int (json_t *json, const char *key, int64_t value);
19
void json_add_double (json_t *json, const char *key, double value);
20
void json_add_bool (json_t *json, const char *key, bool value);
21
void json_add_null (json_t *json, const char *key);
22
void json_free (json_t *json);
23
const char *json_buffer (json_t *json, size_t *len);
24
bool json_write_file (json_t *json, const char *path);
25
void json_pop (json_t *json, uint32_t n);
26
27
#endif
28
29
// MARK: - JSON Parser -
30
/* vim: set et ts=3 sw=3 sts=3 ft=c:
31
*
32
* Copyright (C) 2012, 2013, 2014 James McLaughlin et al. All rights reserved.
33
* https://github.com/udp/json-parser
34
*
35
* Redistribution and use in source and binary forms, with or without
36
* modification, are permitted provided that the following conditions
37
* are met:
38
*
39
* 1. Redistributions of source code must retain the above copyright
40
* notice, this list of conditions and the following disclaimer.
41
*
42
* 2. Redistributions in binary form must reproduce the above copyright
43
* notice, this list of conditions and the following disclaimer in the
44
* documentation and/or other materials provided with the distribution.
45
*
46
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56
* SUCH DAMAGE.
57
*/
58
59
#ifndef _JSON_H
60
#define _JSON_H
61
62
#ifndef json_char
63
#define json_char char
64
#endif
65
66
#ifndef json_int_t
67
#ifndef _MSC_VER
68
#include <inttypes.h>
69
#define json_int_t int64_t
70
#else
71
#define json_int_t __int64
72
#endif
73
#endif
74
75
#include <stdlib.h>
76
77
#ifdef __cplusplus
78
79
#include <string.h>
80
81
extern "C"
82
{
83
84
#endif
85
86
typedef struct
87
{
88
unsigned long max_memory;
89
int settings;
90
91
/* Custom allocator support (leave null to use malloc/free)
92
*/
93
94
void * (* memory_alloc) (size_t, int zero, void * user_data);
95
void (* memory_free) (void *, void * user_data);
96
97
void * user_data; /* will be passed to mem_alloc and mem_free */
98
99
size_t value_extra; /* how much extra space to allocate for values? */
100
101
} json_settings;
102
103
#define json_enable_comments 0x01
104
105
typedef enum
106
{
107
json_none,
108
json_object,
109
json_array,
110
json_integer,
111
json_double,
112
json_string,
113
json_boolean,
114
json_null
115
116
} json_type;
117
118
extern const struct _json_value json_value_none;
119
120
typedef struct _json_object_entry
121
{
122
json_char * name;
123
unsigned int name_length;
124
125
struct _json_value * value;
126
127
} json_object_entry;
128
129
typedef struct _json_value
130
{
131
struct _json_value * parent;
132
133
json_type type;
134
135
union
136
{
137
int boolean;
138
json_int_t integer;
139
double dbl;
140
141
struct
142
{
143
unsigned int length;
144
json_char * ptr; /* null terminated */
145
146
} string;
147
148
struct
149
{
150
unsigned int length;
151
152
json_object_entry * values;
153
154
#if defined(__cplusplus) && __cplusplus >= 201103L
155
decltype(values) begin () const
156
{ return values;
157
}
158
decltype(values) end () const
159
{ return values + length;
160
}
161
#endif
162
163
} object;
164
165
struct
166
{
167
unsigned int length;
168
struct _json_value ** values;
169
170
#if defined(__cplusplus) && __cplusplus >= 201103L
171
decltype(values) begin () const
172
{ return values;
173
}
174
decltype(values) end () const
175
{ return values + length;
176
}
177
#endif
178
179
} array;
180
181
} u;
182
183
union
184
{
185
struct _json_value * next_alloc;
186
void * object_mem;
187
188
} _reserved;
189
190
#ifdef JSON_TRACK_SOURCE
191
192
/* Location of the value in the source JSON
193
*/
194
unsigned int line, col;
195
196
#endif
197
198
199
/* Some C++ operator sugar */
200
201
#ifdef __cplusplus
202
203
public:
204
205
inline _json_value ()
206
{ memset (this, 0, sizeof (_json_value));
207
}
208
209
inline const struct _json_value &operator [] (int index) const
210
{
211
if (type != json_array || index < 0
212
|| ((unsigned int) index) >= u.array.length)
213
{
214
return json_value_none;
215
}
216
217
return *u.array.values [index];
218
}
219
220
inline const struct _json_value &operator [] (const char * index) const
221
{
222
if (type != json_object)
223
return json_value_none;
224
225
for (unsigned int i = 0; i < u.object.length; ++ i)
226
if (!strcmp (u.object.values [i].name, index))
227
return *u.object.values [i].value;
228
229
return json_value_none;
230
}
231
232
inline operator const char * () const
233
{
234
switch (type)
235
{
236
case json_string:
237
return u.string.ptr;
238
239
default:
240
return "";
241
};
242
}
243
244
inline operator json_int_t () const
245
{
246
switch (type)
247
{
248
case json_integer:
249
return u.integer;
250
251
case json_double:
252
return (json_int_t) u.dbl;
253
254
default:
255
return 0;
256
};
257
}
258
259
inline operator bool () const
260
{
261
if (type != json_boolean)
262
return false;
263
264
return u.boolean != 0;
265
}
266
267
inline operator double () const
268
{
269
switch (type)
270
{
271
case json_integer:
272
return (double) u.integer;
273
274
case json_double:
275
return u.dbl;
276
277
default:
278
return 0;
279
};
280
}
281
282
#endif
283
284
} json_value;
285
286
#define EMPTY_SETTINGS_STRUCT {0,0,0,0,0,0}
287
#define EMPTY_STATE_STRUCT {0,0,0,EMPTY_SETTINGS_STRUCT,0,0,0,0}
288
289
json_value * json_parse (const json_char * json,
290
size_t length);
291
292
#define json_error_max 128
293
json_value * json_parse_ex (json_settings * settings,
294
const json_char * json,
295
size_t length,
296
char * error);
297
298
void json_value_free (json_value *);
299
300
301
/* Not usually necessary, unless you used a custom mem_alloc and now want to
302
* use a custom mem_free.
303
*/
304
void json_value_free_ex (json_settings * settings,
305
json_value *);
306
307
308
#ifdef __cplusplus
309
} /* extern "C" */
310
#endif
311
312
#endif
313
314
315
316