-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathjson.c
404 lines (351 loc) · 10 KB
/
json.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// Copyright (C) 2003 Daniel Cowgill
//
// Usage of the works is permitted provided that this
// instrument is retained with the works, so that any entity
// that uses the works is notified of this instrument.
//
// DISCLAIMER: THE WORKS ARE WITHOUT WARRANTY.
#include "json.h"
#include "str.h"
#include "table.h"
#include "utilities.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
struct Json_value {
Json_type type;
union {
char* string; // json_type_string
double number; // json_type_number
int bool; // json_type_bool
Table* table; // json_type_object or json_type_array
} variant;
};
struct Json_iterator {
Json_value* value; // value being iterated over
int pos; // current position in iteration
};
//
// +--------------------+
// | internal functions |
// +--------------------+
//
static Json_value* _json_new_value(Json_type type)
{
Json_value* v = (Json_value*) emalloc(sizeof(Json_value));
v->type = type;
return v;
}
static Json_value* _json_copy_array(const Json_value* v)
{
assert(json_value_has_type(v, json_type_array));
Json_value* new_array =
json_value_new_array(json_value_count_members(v));
Json_iterator* i = json_iterator_create(v);
for (; json_iterator_is_valid(i); json_iterator_advance(i)) {
Json_value* value_copy = json_value_copy(json_iterator_curr_value(i));
json_value_append(new_array, value_copy);
}
json_iterator_destroy(i);
return new_array;
}
static Json_value* _json_copy_object(const Json_value* v)
{
assert(json_value_has_type(v, json_type_object));
Json_value* new_object =
json_value_new_object(json_value_count_members(v));
Json_iterator* i = json_iterator_create(v);
for (; json_iterator_is_valid(i); json_iterator_advance(i)) {
Json_value* value_copy = json_value_copy(json_iterator_curr_value(i));
json_value_set_key(new_object, json_iterator_curr_key(i), value_copy);
}
json_iterator_destroy(i);
return new_object;
}
//
// +------------+
// | Json_value |
// +------------+
//
void json_value_destroy(Json_value* v)
{
if (!v) return;
switch (json_value_get_type(v)) {
case json_type_string:
free(v->variant.string);
break;
case json_type_object:
case json_type_array:
table_destroy(v->variant.table, (void (*)(void*)) json_value_destroy);
break;
default:
// no cleanup necessary for other types
break;
}
free(v);
}
Json_value* json_value_copy(const Json_value* v)
{
switch (json_value_get_type(v)) {
case json_type_null:
return json_value_new_null();
case json_type_string:
return json_value_from_cstr(json_value_get_cstr(v));
case json_type_number:
return json_value_from_double(json_value_get_double(v));
case json_type_bool:
return json_value_from_bool(json_value_get_bool(v));
case json_type_object:
return _json_copy_object(v);
case json_type_array:
return _json_copy_array(v);
}
assert(0); // should not be reached
}
Json_value* json_value_new_null()
{
return _json_new_value(json_type_null);
}
Json_value* json_value_from_cstr(const char* s)
{
Json_value* v = _json_new_value(json_type_string);
v->variant.string = estrdup(s);
return v;
}
Json_value* json_value_from_double(double n)
{
Json_value* v = _json_new_value(json_type_number);
v->variant.number = n;
return v;
}
Json_value* json_value_from_bool(int b)
{
Json_value* v = _json_new_value(json_type_bool);
v->variant.bool = b ? 1 : 0; // coerce to [0,1]
return v;
}
Json_value* json_value_new_array(int size_hint)
{
Json_value* v = _json_new_value(json_type_array);
v->variant.table = table_create(size_hint);
return v;
}
Json_value* json_value_new_object(int size_hint)
{
Json_value* v = _json_new_value(json_type_object);
v->variant.table = table_create(size_hint);
return v;
}
void json_value_append(Json_value* array, Json_value* v)
{
assert(json_value_has_type(array, json_type_array));
table_set_key(&array->variant.table, NULL, v);
}
void json_value_set_key(Json_value* object, const char* k, Json_value* v)
{
assert(json_value_has_type(object, json_type_object));
json_value_destroy(table_set_key(&object->variant.table, k, v));
}
Json_type json_value_get_type(const Json_value* v)
{
assert(v);
return v->type;
}
int json_value_has_type(const Json_value* v, Json_type type)
{
return json_value_get_type(v) == type;
}
const char* json_value_get_cstr(const Json_value* v)
{
assert(json_value_has_type(v, json_type_string));
return v->variant.string;
}
double json_value_get_double(const Json_value* v)
{
assert(json_value_has_type(v, json_type_number));
return v->variant.number;
}
int json_value_get_bool(const Json_value* v)
{
assert(json_value_has_type(v, json_type_bool));
return v->variant.bool;
}
int json_value_count_members(const Json_value* v)
{
assert(json_value_has_type(v, json_type_array) ||
json_value_has_type(v, json_type_object));
return table_get_size(v->variant.table);
}
//
// +---------------+
// | Json_iterator |
// +---------------+
//
Json_iterator* json_iterator_create(const Json_value* v)
{
assert(json_value_has_type(v, json_type_array) ||
json_value_has_type(v, json_type_object));
return (Json_iterator*) table_iterator_create(v->variant.table);
}
void json_iterator_destroy(Json_iterator* iter)
{
table_iterator_destroy((Table_iterator*) iter);
}
void json_iterator_advance(Json_iterator* iter)
{
table_iterator_advance((Table_iterator*) iter);
}
int json_iterator_is_valid(const Json_iterator* iter)
{
return table_iterator_is_valid((Table_iterator*) iter);
}
const char* json_iterator_curr_key(const Json_iterator* iter)
{
return table_iterator_curr_key((Table_iterator*) iter);
}
const Json_value* json_iterator_curr_value(const Json_iterator* iter)
{
return (const Json_value*)
table_iterator_curr_value((Table_iterator*) iter);
}
//
// +-----------------+
// | stringification |
// +-----------------+
//
// Forward declarations are needed because our stringification routines are
// mutually recursive.
static void _json_stringify_array (Str** dst, const Json_value* v);
static void _json_stringify_object(Str** dst, const Json_value* v);
// Escape control characters (and various other characters, such as double
// quotes) in the given string. Returns a newly allocated string.
static char* _escape_string(const char* s)
{
Str* t = str_create(strlen(s));
const char* p;
#define ESC_SEQ(c) { str_append_char(&t, '\\'); str_append_char(&t, c); }
for (p = s; *p != '\0'; p++) {
switch (*p) {
case '\b':
ESC_SEQ('b');
break;
case '\f':
ESC_SEQ('f');
break;
case '\n':
ESC_SEQ('n');
break;
case '\r':
ESC_SEQ('r');
break;
case '\t':
ESC_SEQ('t');
break;
case '"':
ESC_SEQ('"');
break;
default:
str_append_char(&t, *p);
break;
}
}
#undef ESC_SEQ
return str_destroy_and_copy(t);
}
// Converts a double to text, returning a newly allocated string.
static char* _double_to_string(double d)
{
int size = 30; // guess at maximum size
char* p = (char*) emalloc(size);
for (;;) {
int n = snprintf(p, size, "%g", d);
if (n >= 0 && n < size) {
break; // success
}
if (n >= 0) {
size = n+1; // we know the exact size
} else {
size *= 2; // obsolete snprintf, have to guess
}
p = (char*) erealloc(p, size);
}
return p;
}
// Converts a json value to text, appending it to `dst`.
static void _json_stringify(Str** dst, const Json_value* v)
{
char* s;
switch (json_value_get_type(v)) {
case json_type_null:
str_append_cstr(dst, "null");
break;
case json_type_string:
str_append_char(dst, '"');
s = _escape_string(json_value_get_cstr(v));
str_append_cstr(dst, s);
free(s);
str_append_char(dst, '"');
break;
case json_type_number: {
char* s = _double_to_string(json_value_get_double(v));
str_append_cstr(dst, s);
free(s);
break;
}
case json_type_bool:
str_append_cstr(dst, json_value_get_bool(v) ? "true" : "false");
break;
case json_type_object:
_json_stringify_object(dst, v);
break;
case json_type_array:
_json_stringify_array(dst, v);
break;
}
}
// Converts a json array to text, appending it to `dst`.
static void _json_stringify_array(Str** dst, const Json_value* v)
{
str_append_char(dst, '[');
int first = 1;
Json_iterator* i = json_iterator_create(v);
for (; json_iterator_is_valid(i); json_iterator_advance(i)) {
if (first) {
first = 0;
} else {
str_append_cstr(dst, ", ");
}
_json_stringify(dst, json_iterator_curr_value(i));
}
json_iterator_destroy(i);
str_append_char(dst, ']');
}
// Converts a json object to text, appending it to `dst`.
static void _json_stringify_object(Str** dst, const Json_value* v)
{
str_append_char(dst, '{');
Json_iterator* i = json_iterator_create(v);
int first = 1;
for (; json_iterator_is_valid(i); json_iterator_advance(i)) {
if (first) {
first = 0;
} else {
str_append_cstr(dst, ", ");
}
str_append_char(dst, '"');
str_append_cstr(dst, json_iterator_curr_key(i));
str_append_char(dst, '"');
str_append_char(dst, ':');
_json_stringify(dst, json_iterator_curr_value(i));
}
json_iterator_destroy(i);
str_append_char(dst, '}');
}
char* json_stringify(const Json_value* v)
{
Str* s = str_create(100);
_json_stringify(&s, v);
char* answer = estrdup(str_cstr(s));
str_destroy(s);
return answer;
}