-
Notifications
You must be signed in to change notification settings - Fork 60
/
json.c
373 lines (315 loc) · 7.63 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
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "json.h"
#include "esphap_debug.h"
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define DEBUG_STATE(json) \
DEBUG("State = %d, last JSON output: %s", \
json->state, json->buffer + MAX(0, (long int)json->pos - 20));
void json_init(json_stream *json, uint8_t *buffer, size_t size, json_flush_callback on_flush, void *context) {
json->size = size;
json->pos = 0;
json->buffer = buffer;
json->state = JSON_STATE_START;
json->nesting_idx = 0;
json->on_flush = on_flush;
json->context = context;
}
json_stream *json_new(size_t size, json_flush_callback on_flush, void *context) {
json_stream *json = malloc(sizeof(json_stream) + size);
if (!json) {
return NULL;
}
json_init(json, ((uint8_t*)json) + sizeof(json_stream), size, on_flush, context);
return json;
}
void json_free(json_stream *json) {
free(json);
}
void json_set_context(json_stream *json, void *context) {
json->context = context;
}
void json_reset(json_stream *json) {
json->pos = 0;
json->state = JSON_STATE_START;
json->nesting_idx = 0;
}
void json_flush(json_stream *json) {
if (!json->pos)
return;
if (json->on_flush)
json->on_flush(json->buffer, json->pos, json->context);
json->pos = 0;
}
void json_put(json_stream *json, char c) {
json->buffer[json->pos++] = c;
if (json->pos >= json->size - 1)
json_flush(json);
}
void json_write(json_stream *json, const char *data, size_t size) {
while (size) {
size_t chunk_size = size;
if (size > json->size - json->pos)
chunk_size = json->size - json->pos;
memcpy((char *)json->buffer + json->pos, data, chunk_size);
json->pos += chunk_size;
if (json->pos >= json->size - 1)
json_flush(json);
data += chunk_size;
size -= chunk_size;
}
}
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wimplicit-fallthrough"
void json_object_start(json_stream *json) {
if (json->state == JSON_STATE_ERROR)
return;
switch (json->state) {
case JSON_STATE_ARRAY_ITEM:
json_put(json, ',');
case JSON_STATE_START:
case JSON_STATE_OBJECT_KEY:
case JSON_STATE_ARRAY:
json_put(json, '{');
json->state = JSON_STATE_OBJECT;
json->nesting[json->nesting_idx++] = JSON_NESTING_OBJECT;
break;
default:
ERROR("Unexpected object start");
DEBUG_STATE(json);
json->state = JSON_STATE_ERROR;
}
}
void json_object_end(json_stream *json) {
if (json->state == JSON_STATE_ERROR)
return;
switch (json->state) {
case JSON_STATE_OBJECT:
case JSON_STATE_OBJECT_VALUE:
json_put(json, '}');
json->nesting_idx--;
if (!json->nesting_idx) {
json->state = JSON_STATE_END;
}
else {
switch (json->nesting[json->nesting_idx - 1]) {
case JSON_NESTING_OBJECT:
json->state = JSON_STATE_OBJECT_VALUE;
break;
case JSON_NESTING_ARRAY:
json->state = JSON_STATE_ARRAY_ITEM;
break;
}
}
break;
default:
ERROR("Unexpected object end");
DEBUG_STATE(json);
json->state = JSON_STATE_ERROR;
}
}
void json_array_start(json_stream *json) {
if (json->state == JSON_STATE_ERROR)
return;
switch (json->state) {
case JSON_STATE_ARRAY_ITEM:
json_put(json, ',');
case JSON_STATE_START:
case JSON_STATE_OBJECT_KEY:
case JSON_STATE_ARRAY:
json_put(json, '[');
json->state = JSON_STATE_ARRAY;
json->nesting[json->nesting_idx++] = JSON_NESTING_ARRAY;
break;
default:
ERROR("Unexpected array start");
DEBUG_STATE(json);
json->state = JSON_STATE_ERROR;
}
}
void json_array_end(json_stream *json) {
if (json->state == JSON_STATE_ERROR)
return;
switch (json->state) {
case JSON_STATE_ARRAY:
case JSON_STATE_ARRAY_ITEM:
json_put(json, ']');
json->nesting_idx--;
if (!json->nesting_idx) {
json->state = JSON_STATE_END;
}
else {
switch (json->nesting[json->nesting_idx - 1]) {
case JSON_NESTING_OBJECT:
json->state = JSON_STATE_OBJECT_VALUE;
break;
case JSON_NESTING_ARRAY:
json->state = JSON_STATE_ARRAY_ITEM;
break;
}
}
break;
default:
ERROR("Unexpected array end");
DEBUG_STATE(json);
json->state = JSON_STATE_ERROR;
}
}
void _json_number(json_stream *json, const char *value, size_t len) {
if (json->state == JSON_STATE_ERROR)
return;
void _do_write() {
json_write(json, value, len);
}
switch (json->state) {
case JSON_STATE_START:
_do_write();
json->state = JSON_STATE_END;
break;
case JSON_STATE_ARRAY_ITEM:
json_put(json, ',');
case JSON_STATE_ARRAY:
_do_write();
json->state = JSON_STATE_ARRAY_ITEM;
break;
case JSON_STATE_OBJECT_KEY:
_do_write();
json->state = JSON_STATE_OBJECT_VALUE;
break;
default:
ERROR("Unexpected integer");
DEBUG_STATE(json);
json->state = JSON_STATE_ERROR;
}
}
void json_uint8(json_stream *json, uint8_t x) {
char buffer[4];
size_t len = snprintf(buffer, sizeof(buffer), "%u", x);
_json_number(json, buffer, len);
}
void json_uint16(json_stream *json, uint16_t x) {
char buffer[6];
size_t len = snprintf(buffer, sizeof(buffer), "%u", x);
_json_number(json, buffer, len);
}
void json_uint32(json_stream *json, uint32_t x) {
char buffer[11];
size_t len = snprintf(buffer, sizeof(buffer), "%u", x);
_json_number(json, buffer, len);
}
void json_uint64(json_stream *json, uint64_t x) {
char buffer[21];
buffer[20] = 0;
char *b = &buffer[20];
do {
*(--b) = '0' + (x % 10);
} while (x /= 10);
_json_number(json, b, b - &buffer[20]);
}
void json_integer(json_stream *json, int x) {
char buffer[7];
size_t len = snprintf(buffer, sizeof(buffer), "%d", x);
_json_number(json, buffer, len);
}
void json_float(json_stream *json, float x) {
char buffer[32];
size_t len = snprintf(buffer, sizeof(buffer), "%1.15g", x);
_json_number(json, buffer, len);
}
void json_string(json_stream *json, const char *x) {
if (json->state == JSON_STATE_ERROR)
return;
void _do_write() {
// TODO: escape string
json_put(json, '"');
json_write(json, x, strlen(x));
json_put(json, '"');
}
switch (json->state) {
case JSON_STATE_START:
_do_write();
json->state = JSON_STATE_END;
break;
case JSON_STATE_ARRAY_ITEM:
json_put(json, ',');
case JSON_STATE_ARRAY:
_do_write();
json->state = JSON_STATE_ARRAY_ITEM;
break;
case JSON_STATE_OBJECT_VALUE:
json_put(json, ',');
case JSON_STATE_OBJECT:
_do_write();
json_put(json, ':');
json->state = JSON_STATE_OBJECT_KEY;
break;
case JSON_STATE_OBJECT_KEY:
_do_write();
json->state = JSON_STATE_OBJECT_VALUE;
break;
default:
ERROR("Unexpected string");
DEBUG_STATE(json);
json->state = JSON_STATE_ERROR;
}
}
void json_boolean(json_stream *json, bool x) {
if (json->state == JSON_STATE_ERROR)
return;
void _do_write() {
if (x)
json_write(json, "true", 4);
else
json_write(json, "false", 5);
}
switch (json->state) {
case JSON_STATE_START:
_do_write();
json->state = JSON_STATE_END;
break;
case JSON_STATE_ARRAY_ITEM:
json_put(json, ',');
case JSON_STATE_ARRAY:
_do_write();
json->state = JSON_STATE_ARRAY_ITEM;
break;
case JSON_STATE_OBJECT_KEY:
_do_write();
json->state = JSON_STATE_OBJECT_VALUE;
break;
default:
ERROR("Unexpected boolean");
DEBUG_STATE(json);
json->state = JSON_STATE_ERROR;
}
}
void json_null(json_stream *json) {
if (json->state == JSON_STATE_ERROR)
return;
void _do_write() {
json_write(json, "null", 4);
}
switch (json->state) {
case JSON_STATE_START:
_do_write();
json->state = JSON_STATE_END;
break;
case JSON_STATE_ARRAY_ITEM:
json_put(json, ',');
case JSON_STATE_ARRAY:
_do_write();
json->state = JSON_STATE_ARRAY_ITEM;
break;
case JSON_STATE_OBJECT_KEY:
_do_write();
json->state = JSON_STATE_OBJECT_VALUE;
break;
default:
ERROR("Unexpected null");
DEBUG_STATE(json);
json->state = JSON_STATE_ERROR;
}
}
#pragma GCC diagnostic pop