-
Notifications
You must be signed in to change notification settings - Fork 0
/
mjson_type.c
556 lines (475 loc) · 14 KB
/
mjson_type.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "mjson_type.h"
#include "refp/refp.h"
#include "util/ref_str.h"
#include "util/map.h"
#include "util/vector.h"
#include "mjson_parser.h"
#include "mjson_writer.h"
#define MJSON_INI_FUN_NAME(type) mjson_##type##_ini
#define MJSON_FINI_FUN_NAME(type) mjson_##type##_fini
#define INI_FUN(type_t, TYPE_T) \
mjson_value_t *MJSON_INI_FUN_NAME(type_t)() { \
MJSON_TYPE_NAME(type_t) *v = (MJSON_TYPE_NAME(type_t) *)calloc(1, sizeof (MJSON_TYPE_NAME(type_t)));\
if (v == NULL) { \
return NULL; \
} \
v->h.type = TYPE_T; \
return &v->h; \
}
#define FINI_FUN_BASIC(type_t, TYPE_T) \
void MJSON_FINI_FUN_NAME(type_t)(mjson_value_t *mv) { \
if (mv == NULL || mv->type != TYPE_T) { \
return; \
} \
\
if (mv->text != NULL) { \
if (mv->is_str) { \
free(mv->text); \
} else { \
rs_fini(mv->text); \
} \
mv->text = NULL; \
} \
free(mv); \
}
#define FINI_FUN_STATIC(type_t, TYPE_T) \
void MJSON_FINI_FUN_NAME(type_t)(mjson_value_t *mv) { \
}
static mjson_bool_t true_value = {{MJSON_TRUE, 0, 1, 0, (ref_str_t *)"true"}};
static mjson_bool_t false_value = {{MJSON_FALSE, 0, 1, 0, (ref_str_t *)"false"}};
static mjson_null_t null_value = {{MJSON_NULL, 0, 1, 0, (ref_str_t *)"null"}};
INI_FUN(object, MJSON_OBJECT)
INI_FUN(array, MJSON_ARRAY)
INI_FUN(str, MJSON_STRING)
INI_FUN(int, MJSON_INTEGER)
INI_FUN(double, MJSON_DOUBLE)
mjson_value_t *MJSON_INI_FUN_NAME(true)() {
return &true_value.h;
}
mjson_value_t *MJSON_INI_FUN_NAME(false)() {
return &false_value.h;
}
mjson_value_t *MJSON_INI_FUN_NAME(null)() {
return &null_value.h;
}
void MJSON_FINI_FUN_NAME(object)(mjson_value_t *mv) {
if (mv == NULL || mv->type != MJSON_OBJECT) {
return;
}
if (mv->text != NULL) {
rs_fini(mv->text);
mv->text = NULL;
}
mjson_object_t *mo = (mjson_object_t *)mv;
if (mo->m != NULL) {
map_iter_t it = map_iter_next(mo->m, NULL);
while (it.v != NULL) {
mjson_t *v = (mjson_t *)map_iter_getv(&it);
mj_fini(v);
it = map_iter_next(mo->m, &it);
}
map_fini(mo->m);
mo->m = NULL;
}
free(mo);
}
void MJSON_FINI_FUN_NAME(array)(mjson_value_t *mv) {
if (mv == NULL || mv->type != MJSON_ARRAY) {
return;
}
if (mv->text != NULL) {
rs_fini(mv->text);
mv->text = NULL;
}
mjson_array_t *ma = (mjson_array_t *)mv;
if (ma->v != NULL) {
size_t i = 0;
size_t n = vec_num(ma->v);
for (i = 0; i < n; i++) {
mjson_t *v = (mjson_t *)vec_get(ma->v, i);
mj_fini(v);
}
vec_fini(ma->v);
ma->v = NULL;
}
free(ma);
}
FINI_FUN_BASIC(str, MJSON_STRING)
FINI_FUN_BASIC(int, MJSON_INTEGER)
FINI_FUN_BASIC(double, MJSON_DOUBLE)
FINI_FUN_STATIC(true, MJSON_TRUE)
FINI_FUN_STATIC(false, MJSON_FALSE)
FINI_FUN_STATIC(null, MJSON_NULL)
void set_error(mjson_error_t *pe, int stat) {
if (pe != NULL) {
pe->stat = stat;
}
}
/*
*
* get过程:
* 0. text == NULL: 尚未赋值
* 组合类型:需要生成对应的结构(因为需要返回默认MJSON_NULL)
* 基本类型:默认值可以直接赋值
* 1. text != NULL: 已经赋值
* 组合类型:进行parse即可,最后肯定有正确的内部结构(assert之)
* 基本类型:同上,需要parse
*
* set过程:
* 0. text == NULL: 尚未赋值
* 组合类型:不需要默认text,因为很快会进行赋值,这样成了浪费
* 基本类型:直接赋值即可,本身來进行解决
* 1. text != NULL: 已经赋值
* 组合类型:parse得正确结构,再进行处理
* 基本类型:释放原结构即可
*
* 为了减少浪费,所以要在这里深入json结构内部,而无法用parser隐藏
* 不过set过程其实无法用parser來隐藏,同样是要暴露结构的,所以应该还好
*
*/
mjson_t *MJSON_GET_FUN_NAME(object)(mjson_value_t *mv, const char *key, mjson_error_t *pe) {
if (mv == NULL) {
set_error(pe, MJSONE_NULL);
return NULL;
}
if (mv->type != MJSON_OBJECT) {
set_error(pe, MJSONE_TYPE);
return NULL;
}
mjson_object_t *mo = (mjson_object_t *)mv;
if (mo->h.text == NULL) {
if (mo->m == NULL) {
mo->m = map_ini(0);
if (mo->m == NULL) {
set_error(pe, MJSONE_MEM);
return NULL;
}
/* 进行了修改 */
mo->h.is_dirty = 1;
}
} else if (mjson_parse(mv, 0) < 0) {
set_error(pe, MJSONE_PARSE);
return NULL;
}
assert(mo->m != NULL);
size_t key_len = strlen(key);
mjson_t *v = (mjson_t *)map_get(mo->m, key, key_len);
if (v == NULL) {
v = mj_ini(MJSON_NULL);
if (v == NULL) {
set_error(pe, MJSONE_MEM);
return NULL;
}
map_set(mo->m, key, key_len, v);
mo->h.is_dirty = 1;
}
return v;
}
void MJSON_SET_FUN_NAME(object)(mjson_value_t *mv, const char *key, mjson_t *value, mjson_error_t *pe) {
if (mv == NULL) {
mj_fini(value);
set_error(pe, MJSONE_NULL);
return;
}
if (mv->type != MJSON_OBJECT) {
mj_fini(value);
set_error(pe, MJSONE_TYPE);
return;
}
mjson_object_t *mo = (mjson_object_t *)mv;
if (mo->h.text == NULL) {
if (mo->m == NULL) {
mo->m = map_ini(0);
if (mo->m == NULL) {
mj_fini(value);
set_error(pe, MJSONE_MEM);
return;
}
mo->h.is_dirty = 1;;
}
} else if (mjson_parse(mv, 0) < 0) {
mj_fini(value);
set_error(pe, MJSONE_PARSE);
return;
}
assert(mo->m != NULL);
size_t key_len = strlen(key);
mjson_t *old_v = (mjson_t *)map_get(mo->m, key, key_len);
if (map_set(mo->m, key, key_len, value) < 0) {
mj_fini(value);
set_error(pe, MJSONE_MEM);
return;
}
mj_fini(old_v);
mo->h.is_dirty = 1;
}
size_t mjson_object_size(mjson_value_t *mv) {
if (mv == NULL) {
return 0;
}
if (mv->type != MJSON_OBJECT) {
return 0;
}
mjson_object_t *mo = (mjson_object_t *)mv;
if (mo->h.text != NULL) {
mjson_parse(mv, 0);
}
return map_num(mo->m);
}
void mjson_object_erase(mjson_value_t *mv, const char *key) {
if (mv == NULL || key == NULL) {
return;
}
if (mv->type != MJSON_OBJECT) {
return;
}
mjson_object_t *mo = (mjson_object_t *)mv;
if (mo->h.text != NULL) {
mjson_parse(mv, 0);
}
mjson_t *v = (mjson_t *)map_erase(mo->m, key, strlen(key));
mj_fini(v);
mo->h.is_dirty = 1;
}
mjson_t *MJSON_GET_FUN_NAME(array)(mjson_value_t *mv, size_t index, mjson_error_t *pe) {
if (mv == NULL) {
set_error(pe, MJSONE_NULL);
return NULL;
}
if (mv->type != MJSON_ARRAY) {
set_error(pe, MJSONE_TYPE);
return NULL;
}
mjson_array_t *ma = (mjson_array_t *)mv;
if (ma->h.text == NULL) {
if (ma->v == NULL) {
ma->v = vec_ini(0);
if (ma->v == NULL) {
set_error(pe, MJSONE_MEM);
return NULL;
}
ma->h.is_dirty = 1;
}
} else if (mjson_parse(mv, 0) < 0) {
set_error(pe, MJSONE_PARSE);
return NULL;
}
assert(ma->v != NULL);
if (index >= vec_num(ma->v)) {
set_error(pe, MJSONE_RANGE);
return NULL;
}
mjson_t *v = (mjson_t *)vec_get(ma->v, index);
if (v == NULL) {
v = mj_ini(MJSON_NULL);
if (v == NULL) {
set_error(pe, MJSONE_MEM);
return NULL;
}
vec_set(ma->v, index, v);
ma->h.is_dirty = 1;
}
return v;
}
void MJSON_SET_FUN_NAME(array)(mjson_value_t *mv, size_t index, mjson_t *value, mjson_error_t *pe) {
if (mv == NULL) {
mj_fini(value);
set_error(pe, MJSONE_NULL);
return;
}
if (mv->type != MJSON_ARRAY) {
mj_fini(value);
set_error(pe, MJSONE_TYPE);
return;
}
mjson_array_t *ma = (mjson_array_t *)mv;
if (ma->h.text == NULL) {
if (ma->v == NULL) {
ma->v = vec_ini(0);
if (ma->v == NULL) {
mj_fini(value);
set_error(pe, MJSONE_MEM);
return;
}
ma->h.is_dirty = 1;;
}
} else if (mjson_parse(mv, 0) < 0) {
mj_fini(value);
set_error(pe, MJSONE_PARSE);
return;
}
assert(ma->v != NULL);
mjson_t *old_v = (mjson_t *)vec_get(ma->v, index);
if (vec_set(ma->v, index, value) < 0) {
mj_fini(value);
set_error(pe, MJSONE_MEM);
return;
}
mj_fini(old_v);
ma->h.is_dirty = 1;;
}
size_t mjson_array_size(mjson_value_t *mv) {
if (mv == NULL) {
return 0;
}
if (mv->type != MJSON_ARRAY) {
return 0;
}
mjson_array_t *ma = (mjson_array_t *)mv;
if (ma->h.text != NULL) {
mjson_parse(mv, 0);
}
return vec_num(ma->v);
}
void mjson_array_erase(mjson_value_t *mv, size_t index) {
if (mv == NULL) {
return;
}
if (mv->type != MJSON_ARRAY) {
return;
}
mjson_array_t *ma = (mjson_array_t *)mv;
if (ma->h.text != NULL) {
mjson_parse(mv, 0);
}
mjson_t *v = (mjson_t *)vec_erase(ma->v, index);
mj_fini(v);
ma->h.is_dirty = 1;
}
/*
*
* 基本类型
*
*/
int MJSON_GET_FUN_NAME(int)(mjson_value_t *mv, mjson_error_t *pe) {
if (mv == NULL) {
set_error(pe, MJSONE_NULL);
return 0;
}
if (mv->type != MJSON_INTEGER) {
set_error(pe, MJSONE_TYPE);
return 0;
}
mjson_int_t *mi = (mjson_int_t *)mv;
if (mi->h.text == NULL) {
mi->i = 0;
} else if (mjson_parse(mv, 0) < 0) {
set_error(pe, MJSONE_PARSE);
/* 解析错误,仍然要返回已经解析的部分值 */
}
return mi->i;
}
void MJSON_SET_FUN_NAME(int)(mjson_value_t *mv, int value, mjson_error_t *pe) {
if (mv == NULL) {
set_error(pe, MJSONE_NULL);
return;
}
if (mv->type != MJSON_INTEGER) {
set_error(pe, MJSONE_TYPE);
return;
}
mjson_int_t *mi = (mjson_int_t *)mv;
if (mi->i == value) {
set_error(pe, MJSONE_OK);
return;
}
char *text = mjson_write_int(value);
if (text == NULL) {
set_error(pe, MJSONE_MEM);
return;
}
mi->i = value;
if (mi->h.text != NULL) {
if (mi->h.is_str) {
free((char *)mi->h.text);
} else {
rs_fini(mi->h.text);
}
}
mi->h.text = (ref_str_t *)text;
mi->h.is_str = 1;
mi->h.is_dirty = 1;
set_error(pe, MJSONE_OK);
}
double MJSON_GET_FUN_NAME(double)(mjson_value_t *mv, mjson_error_t *pe) {
if (mv == NULL) {
set_error(pe, MJSONE_NULL);
return 0;
}
if (mv->type != MJSON_DOUBLE) {
set_error(pe, MJSONE_TYPE);
return 0;
}
mjson_double_t *md = (mjson_double_t *)mv;
if (md->h.text == NULL) {
md->d = 0.0;
} else if (mjson_parse(mv, 0) < 0) {
set_error(pe, MJSONE_PARSE);
}
return md->d;
}
void MJSON_SET_FUN_NAME(double)(mjson_value_t *mv, double value, mjson_error_t *pe) {
if (mv == NULL) {
set_error(pe, MJSONE_NULL);
return;
}
if (mv->type != MJSON_DOUBLE) {
set_error(pe, MJSONE_TYPE);
return;
}
mjson_double_t *md = (mjson_double_t *)mv;
if (md->d == value) {
set_error(pe, MJSONE_OK);
return;
}
char *text = mjson_write_double(value);
if (text == NULL) {
set_error(pe, MJSONE_MEM);
return;
}
md->d = value;
if (md->h.text != NULL) {
if (md->h.is_str) {
free((char *)md->h.text);
} else {
rs_fini(md->h.text);
}
}
md->h.text = (ref_str_t *)text;
md->h.is_str = 1;
md->h.is_dirty = 1;
set_error(pe, MJSONE_OK);
}
#define DECLARE_VALUE_INFO(type) \
{MJSON_INI_FUN_NAME(type), MJSON_FINI_FUN_NAME(type)}
typedef mjson_value_t *(*mjson_value_ini_fun)();
typedef void (*mjson_value_fini_fun)(mjson_value_t *mv);
static struct mjson_value_info_t {
mjson_value_ini_fun ini_f;
mjson_value_fini_fun fini_f;
} mjson_value_infos[] = {
DECLARE_VALUE_INFO(object),
DECLARE_VALUE_INFO(array),
DECLARE_VALUE_INFO(str),
DECLARE_VALUE_INFO(int),
DECLARE_VALUE_INFO(double),
DECLARE_VALUE_INFO(true),
DECLARE_VALUE_INFO(false),
DECLARE_VALUE_INFO(null),
};
mjson_value_t *mjson_ini(size_t type) {
if (type < MJSON_OBJECT || type > MJSON_NULL) {
return NULL;
}
return mjson_value_infos[type].ini_f();
}
void mjson_fini(mjson_value_t *mv) {
if (mv == NULL) {
return;
}
mjson_value_infos[mv->type].fini_f(mv);
}