This repository has been archived by the owner on Nov 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.c
398 lines (358 loc) · 9.29 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
/*******************************************************************************
* Carona Comunitária USP está licenciado com uma Licença Creative Commons
* Atribuição-NãoComercial-CompartilhaIgual 4.0 Internacional (CC BY-NC-SA 4.0).
*
* Carona Comunitária USP is licensed under a Creative Commons
* Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0).
*
* Biblioteca para análise de pacotes JSON
*******************************************************************************/
#include "json.h"
static int cmp_json_pair(const void *p1, const void *p2) { // ponteiros para json_pair
return strcmp( ( (json_pair const *) p1)->name, ( (json_pair const *) p2)->name);
}
static int search_object(json_parser *json) {
for (; *json->cur; json->cur++) {
switch (*json->cur) {
case '{':
json->cur++;
return 0;
case '\t':
case '\r':
case '\n':
case ' ':
break;
default:
return JSON_INVALID;
}
}
return JSON_INCOMPLETE;
}
static int is_empty(json_parser *json) {
for (; *json->cur; json->cur++) {
switch (*json->cur) {
case '}':
return 1;
case '\t':
case '\r':
case '\n':
case ' ':
break;
default:
return 0;
}
}
return JSON_INCOMPLETE;
}
static char *json_getstr(json_parser *json) {
char *end, *p;
for (; *(json->cur); json->cur++) {
switch (*json->cur) {
case '\"':
if ((end = strchr(json->cur + 1, '\"')) == NULL)
return NULL;
*end = 0;
p = json->cur + 1;
json->cur = end + 1;
return p;
case '\t':
case '\r':
case '\n':
case ' ':
break;
default:
return NULL;
}
}
return NULL;
}
static int json_next(json_parser *json) { //1 = há mais dados antes do fim do objeto
for (; *json->cur; json->cur++) {
switch (*json->cur) {
case '}':
*json->cur = 0; // Ver json_get_int()
return 0;
case '\t':
case '\r':
case '\n':
case ' ':
break;
case ',':
*json->cur = 0; // Ver json_get_int()
json->cur++;
return 1;
default:
return JSON_INVALID;
}
}
return JSON_INVALID;
}
static int json_get_value_start(json_parser *json) {
for (; *json->cur; json->cur++) {
switch (*json->cur) {
case ':':
json->cur++;
return JSON_OK;
case '\t':
case '\r':
case '\n':
case ' ':
break;
default:
return JSON_INVALID;
}
}
return JSON_INVALID;
}
static int json_parse_int(json_parser *json) {
for (; *json->cur; json->cur++) {
switch (*json->cur) {
case '\t':
case '\r':
case '\n':
case ' ':
*json->cur = 0; // Fim do número, coloca caractere nulo
json->cur++; // Próxima função começará acessos do próximo byte
// '}' e ',' serão usadas por json_next() para encontrar o fim do objeto/item,
// então não vamos colocá-los como 0 aqui:
///@TODO: Isso é um pouco confuso (ler aqui, colocar o caractere nulo em outra função),
/// melhor mudar isso alguma hora (mas funciona bem, então vou deixar assim por enquanto)
case '}':
case ',':
return 0; // Sucesso!
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
break;
default:
return JSON_INVALID;
}
}
// Atingiu final do pacote sem achar fim do objeto:
return JSON_INVALID;
}
static int json_get_type(json_parser *json) {
json_get_value_start(json);
for (; *json->cur; json->cur++) {
switch (*json->cur) {
case '\"':
return JSON_STRING;
case 't':
if (!strncmp(json->cur, "true", 4)) {
json->cur += 4;
return JSON_TRUE;
}
return JSON_INVALID;
case 'f':
if (!strncmp(json->cur, "false", 5)) {
json->cur += 5;
return JSON_FALSE;
}
return JSON_INVALID;
case 'n':
if (!strncmp(json->cur, "null", 4)) {
json->cur += 4;
return JSON_NULL;
}
return JSON_INVALID;
case '{':
return JSON_OBJECT;
case '[':
return JSON_ARRAY;
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9':
return JSON_NUMBER;
case '\t':
case '\r':
case '\n':
case ' ':
break;
default:
return JSON_INVALID;
}
}
return JSON_INVALID;
}
/*
* Busca fim de uma array
*/
int json_array_end(json_parser *json) {
int n_aberturas = 0; // número de colchetes ('[') que foram abertos
for (; *json->cur; json->cur++) {
switch (*json->cur) {
case '[':
n_aberturas++;
break;
case ']':
n_aberturas--;
if (n_aberturas == 0) {
*json->cur = 0;
json->cur++;
return 0;
}
}
}
// Atingiu final do pacote sem achar fim da array:
return JSON_INVALID;
}
void bp() {} //para depuração
#define try(cmd) do {int __ret = (cmd); if (__ret < 0) return __ret;} while(0)
#define try0(cmd) do {if ((cmd) == NULL) return JSON_INVALID;} while(0)
int json_all_parse(json_parser *json) {
printf("Info: JSON parse\n%s\n", json->start);
json->cur = json->start;
try(search_object(json));
int obj_n = 0;
int has_ended = is_empty(json);
if (has_ended == JSON_INCOMPLETE)
return JSON_INCOMPLETE;
if (!has_ended) {
int has_next;
do {
if (obj_n >= json->n_pairs)
return JSON_NOMEM; // Faltam entradas em json->pairs
char *str;
try0(str = json_getstr(json));
int type;
switch (type = json_get_type(json)) {
// Seria melhor e mais organizado ter uma array para cada tipo
case JSON_STRING:
try0(json->pairs[obj_n].value = json_getstr(json));
break;
case JSON_NUMBER:
printf("Recebido número, assumindo como inteiro\n");
json->pairs[obj_n].value = json->cur;
try(json_parse_int(json));
break;
case JSON_TRUE:
case JSON_FALSE:
case JSON_NULL:
break;
case JSON_OBJECT:
///@TODO: suportar objetos (não é importante agora)
printf("JSON: Objetos não suportados!\n");
return JSON_INVALID;
case JSON_ARRAY:
json->pairs[obj_n].value = json->cur + 1;
try(json_array_end(json));
break;
default:
printf("JSON: json_get_type: tipo %d desconhecido\n", type);
}
json->pairs[obj_n].name = str;
json->pairs[obj_n].type = type;
obj_n++;
has_next = json_next(json);
if (has_next == JSON_INVALID)
return JSON_INVALID;
} while (has_next == 1 && obj_n < json->n_pairs);
}
bp();
qsort(json->pairs, obj_n, sizeof(json_pair), cmp_json_pair);
printf("Sorted:\n");
int i;
for (i=0; i < obj_n; i++) {
printf("\"%s\" = \t", json->pairs[i].name);
switch (json->pairs[i].type) {
case JSON_STRING:
printf("\"%s\"\n", json->pairs[i].value);
break;
case JSON_INT:
printf("%s\n", json->pairs[i].value);
break;
case JSON_TRUE:
printf("true\n");
break;
case JSON_FALSE:
printf("false\n");
break;
case JSON_NULL:
printf("null\n");
break;
case JSON_ARRAY:
printf("array [%s]\n", json->pairs[i].value);
break;
default:
printf("Tipo desconhecido %d\n", json->pairs[i].type);
}
}
json->n_pairs_read = obj_n;
return 0;
}
json_pair *json_get(json_parser *json, const char *search, int type) {
json_pair search_pair, *result;
search_pair.name = search;
result = bsearch(&search_pair, json->pairs, json->n_pairs_read, sizeof(json_pair), cmp_json_pair);
if (result == NULL)
return NULL;
if (result->type == type)
return result;
return NULL;
}
char *json_get_str(json_parser *json, const char *search) {
json_pair *result;
if ((result =json_get(json, search, JSON_STRING)) == NULL)
return NULL;
return result->value;
}
int json_get_int(json_parser *json, const char *search) {
json_pair *result = json_get(json, search, JSON_INT);
int n;
if (result == NULL)
return JSON_INVALID; ///@TODO: Isso precisa ser arrumado (-1 não poderá ser lido pois retornará o mesmo que JSON_INVALID),
/// mas não é importante agora
sscanf(result->value, "%d", &n);
return n;
}
int json_get_bool(json_parser *json, const char *search) {
///@TODO: melhorar a interface (sem duas buscas aqui)
/// talvez definir um tipo JSON_BOOL?
if (json_get(json, search, JSON_TRUE) != NULL)
return 1;
if (json_get(json, search, JSON_FALSE) != NULL)
return 0;
return JSON_INVALID;
}
int json_get_null(json_parser *json, const char *search) {
if (json_get(json, search, JSON_NULL) != NULL)
return 1;
return JSON_INVALID;
}
const char *json_get_array(json_parser *json, const char *search) {
json_pair *result;
if ((result = json_get(json, search, JSON_ARRAY)) == NULL)
return NULL;
return result->value;
}
/*
* Pega item n da array
*/
///@TODO: função bem simples, falha em arrays ou objetos dentro de arrays.
/// Mas usamos apenas arrays de inteiros no servidor, então não faz diferença
int json_array_i(const char *array, int n) {
int i, ret;
for (i = 0; i < n; i++) {
if ((array = strchr(array, ',')) == NULL)
return JSON_INVALID; ///@TODO: Isso precisa ser arrumado (-1 não poderá ser lido pois retornará o mesmo que JSON_INVALID),
/// mas não é importante agora
array++;
}
for (; *array; array++) {
switch (*array) {
case '0': case '1': case '2': case '3': case '4': case '5':
case '6': case '7': case '8': case '9': case '-':
sscanf(array, "%d", &ret);
return ret;
case '\t':
case '\r':
case '\n':
case ' ':
break;
default:
return JSON_INVALID;
}
}
return JSON_INVALID;
}
//int json_array_next(char *array) {
// Uma função que retorne o próximo item da array
//}