-
Notifications
You must be signed in to change notification settings - Fork 1
/
json.c
298 lines (240 loc) · 6.87 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
/* xml2json
*
* Copyright (c) 2018 Partha Susarla <mail@spartha.org>
*/
#include "json.h"
#include "cstring.h"
#include "util.h"
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
/*
* Private Functions
*/
static void parse_json_object(JsonObject *object, cstring *str);
static bool is_json_type_valid(unsigned int type)
{
return (type <= JSON_OBJECT);
}
static void parse_string_object(const char *s, cstring *str)
{
/* TODO: Make this function utf-8 aware */
cstring_addch(str, '"');
cstring_addstr(str, s);
cstring_addch(str, '"');
}
static void parse_num_object(double num, cstring *str)
{
char buf[64];
sprintf(buf, "%.16g", num);
/* TODO: Check if `buf` has a valid number */
cstring_addstr(str, buf);
}
static void parse_array_object(JsonObject *object, cstring *str)
{
JsonObject *element;
cstring_addch(str, '[');
json_foreach(element, object) {
parse_json_object(element, str);
if (element->next != NULL)
cstring_addch(str, ',');
}
cstring_addch(str, ']');
}
static void parse_object(JsonObject *object, cstring *str)
{
JsonObject *member;
cstring_addch(str, '{');
json_foreach(member, object) {
parse_string_object(member->key, str);
cstring_addch(str, ':');
parse_json_object(member, str);
if (member->next != NULL)
cstring_addch(str, ',');
}
cstring_addch(str, '}');
}
static void parse_json_object(JsonObject *object, cstring *str)
{
assert(is_json_type_valid(object->type));
switch (object->type) {
case JSON_NULL:
cstring_addstr(str, "null");
break;
case JSON_BOOL:
cstring_addstr(str, object->bool_ ? "true" : "false");
break;
case JSON_STRING:
parse_string_object(object->str_, str);
break;
case JSON_NUMBER:
parse_num_object(object->num_, str);
break;
case JSON_ARRAY:
parse_array_object(object, str);
break;
case JSON_OBJECT:
parse_object(object, str);
break;
default:
assert(false);
}
}
static char *json_object_to_string(JsonObject *object)
{
cstring jsonstr;
size_t len = 0;
cstring_init(&jsonstr, 0);
parse_json_object(object, &jsonstr);
return cstring_detach(&jsonstr, &len);
}
static JsonObject *json_obj_new(JsonType type)
{
JsonObject *obj = (JsonObject *) xcalloc(1, sizeof(JsonObject));
obj->type = type;
return obj;
}
static void json_remove_from_parent(JsonObject *obj)
{
JsonObject *parent = obj->parent;
if (parent) {
if (obj->prev != NULL)
obj->prev->next = obj->next;
else
parent->children.head = obj->next;
if (obj->next != NULL)
obj->next->prev = obj->prev;
else
obj->children.tail = obj->prev;
free(obj->key);
obj->parent = NULL;
obj->prev = obj->next = NULL;
obj->key = NULL;
}
}
static void json_obj_free(JsonObject *obj)
{
if (obj) {
json_remove_from_parent(obj);
switch(obj->type) {
case JSON_STRING:
free(obj->str_);
break;
case JSON_ARRAY:
case JSON_OBJECT:
{
JsonObject *child, *next;
for (child = obj->children.head; child != NULL; child = next) {
next = child->next;
json_obj_free(child);
}
break;
}
default:
break;
}
free(obj);
obj = NULL;
}
}
static void append_object(JsonObject *parent, JsonObject *child)
{
child->parent = parent;
child->prev = parent->children.tail;
child->next = NULL;
if (parent->children.tail != NULL)
parent->children.tail->next = child;
else
parent->children.head = child;
parent->children.tail = child;
}
static void prepend_object(JsonObject *parent, JsonObject *child)
{
child->parent = parent;
child->prev = NULL;
child->next = parent->children.head;
if (parent->children.head != NULL)
parent->children.head->prev = child;
else
parent->children.tail = child;
parent->children.head = child;
}
/*
* Public Functions
*/
char *json_encode(JsonObject *obj)
{
return json_object_to_string(obj);
}
JsonObject *json_null_obj(void)
{
return json_obj_new(JSON_NULL);
}
JsonObject *json_bool_obj(bool b)
{
JsonObject *obj = json_obj_new(JSON_BOOL);
obj->bool_ = b;
return obj;
}
JsonObject *json_string_obj(const char *str)
{
JsonObject *obj = json_obj_new(JSON_STRING);
obj->str_ = xstrdup(str);
return obj;
}
JsonObject *json_num_obj(double num)
{
JsonObject *obj = json_obj_new(JSON_NUMBER);
obj->num_ = num;
return obj;
}
JsonObject *json_array_obj(void)
{
return json_obj_new(JSON_ARRAY);
}
JsonObject *json_new(void)
{
return json_obj_new(JSON_OBJECT);
}
void json_append_to_array(JsonObject *array, JsonObject *element)
{
assert(array->type == JSON_ARRAY);
assert(element->parent == NULL);
append_object(array, element);
}
void json_prepend_to_array(JsonObject *array, JsonObject *element)
{
assert(array->type == JSON_ARRAY);
assert(element->parent == NULL);
prepend_object(array, element);
}
void json_append_member(JsonObject *object, const char *key, JsonObject *value)
{
assert(object->type == JSON_OBJECT);
assert(value->parent == NULL);
value->key = xstrdup(key);
append_object(object, value);
}
void json_prepend_member(JsonObject *object, const char *key, JsonObject *value)
{
assert(object->type == JSON_OBJECT);
assert(value->parent == NULL);
value->key = xstrdup(key);
prepend_object(object, value);
}
bool json_validate(JsonObject *object)
{
return false;
}
void json_free(JsonObject *obj)
{
json_obj_free(obj);
}
JsonObject *json_first_child(JsonObject *object)
{
if (object != NULL &&
(object->type == JSON_ARRAY || object->type == JSON_OBJECT))
return object->children.head;
return NULL;
}