-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.c
4187 lines (3741 loc) · 119 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
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
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*******************************************
* SPDX-License-Identifier: MIT *
* Copyright (C) 2019-.... Jing Leng *
* Contact: Jing Leng <[email protected]> *
* URL: https://github.com/lengjingzju/json *
*******************************************/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <math.h>
#include "jnum.h"
#include "json.h"
#define type_member jkey.type
#define key_member jkey.str
#define str_member vstr.str
/**************** configuration ****************/
/*
* Algorithm to convert double to string
* 0: ljson dtoa developed by Jing Leng, fastest and best
* 1: sprintf of C standard library, slowest
* 2: grisu2 dtoa developed by Google
* 3: dragonbox dtoa developed by Raffaello Giulietti, faster than grisu2
*/
#ifndef JSON_DTOA_ALGORITHM
#define JSON_DTOA_ALGORITHM 0
#endif
#if JSON_DTOA_ALGORITHM == 1
static inline int json_dtoa(double num, char *buffer)
{
return sprintf(buffer, "%1.16g", num);
}
#elif JSON_DTOA_ALGORITHM == 2
extern int grisu2_dtoa(double num, char *buffer);
#define json_dtoa grisu2_dtoa
#elif JSON_DTOA_ALGORITHM == 3
extern int dragonbox_dtoa(double num, char *buffer);
#define json_dtoa dragonbox_dtoa
#else
#define json_dtoa jnum_dtoa
#endif
/* Whether to allow C-like single-line comments and multi-line comments */
#ifndef JSON_PARSE_SKIP_COMMENT
#define JSON_PARSE_SKIP_COMMENT 0
#endif
/* Whether to allow comma in last element of array or object */
#ifndef JSON_PARSE_LAST_COMMA
#define JSON_PARSE_LAST_COMMA 1
#endif
/* Whether to allow empty key */
#ifndef JSON_PARSE_EMPTY_KEY
#define JSON_PARSE_EMPTY_KEY 0
#endif
/* Whether to allow special characters such as newline in the string */
#ifndef JSON_PARSE_SPECIAL_CHAR
#define JSON_PARSE_SPECIAL_CHAR 1
#endif
/* Whether to allow single quoted string/key and unquoted key */
#ifndef JSON_PARSE_SPECIAL_QUOTES
#define JSON_PARSE_SPECIAL_QUOTES 0
#endif
/* Whether to allow HEX number */
#ifndef JSON_PARSE_HEX_NUM
#define JSON_PARSE_HEX_NUM 1
#endif
/* Whether to allow special number such as starting with '.', '+', '0' */
#ifndef JSON_PARSE_SPECIAL_NUM
#define JSON_PARSE_SPECIAL_NUM 1
#endif
/* Whether to allow special double such as NaN, Infinity, -Infinity */
#ifndef JSON_PARSE_SPECIAL_DOUBLE
#define JSON_PARSE_SPECIAL_DOUBLE 1
#endif
/* Whether to allow json starting with non-array and non-object */
#ifndef JSON_PARSE_SINGLE_VALUE
#define JSON_PARSE_SINGLE_VALUE 1
#endif
/* Whether to allow characters other than spaces after finishing parsing */
#ifndef JSON_PARSE_FINISHED_CHAR
#define JSON_PARSE_FINISHED_CHAR 0
#endif
/**************** debug ****************/
/* error print */
#define JSON_ERROR_PRINT_ENABLE 1
#if JSON_ERROR_PRINT_ENABLE
#define JsonErr(fmt, args...) do { \
printf("[JsonErr][%s:%d] ", __func__, __LINE__); \
printf(fmt, ##args); \
} while(0)
#define JsonPareseErr(s) do { \
if (parse_ptr->str) { \
char ptmp[32] = {0}; \
strncpy(ptmp, parse_ptr->str + parse_ptr->offset, 31); \
JsonErr("====%s====\n%s\n", s, ptmp); \
} else { \
JsonErr("%s\n", s); \
} \
} while(0)
#else
#define JsonErr(fmt, args...) do {} while(0)
#define JsonPareseErr(s) do {} while(0)
#endif
/**************** gcc builtin ****************/
#if defined(__GNUC__) || defined(__clang__)
#define UNUSED_ATTR __attribute__((unused))
#define FALLTHROUGH_ATTR __attribute__((fallthrough))
#define likely(cond) __builtin_expect(!!(cond), 1)
#define unlikely(cond) __builtin_expect(!!(cond), 0)
#else
#define UNUSED_ATTR
#define FALLTHROUGH_ATTR
#define likely(cond) (cond)
#define unlikely(cond) (cond)
#endif
#if JSON_PARSE_SPECIAL_QUOTES
#define UNUSED_END_CH
#else
#define UNUSED_END_CH UNUSED_ATTR
#endif
/**************** definition ****************/
/* head apis */
#define json_malloc malloc
#define json_calloc calloc
#define json_realloc realloc
#define json_strdup strdup
#define json_free free
#define JSON_ITEM_NUM_PLUS_DEF 16
#define JSON_POOL_MEM_SIZE_DEF 8192
/* print choice size */
#define JSON_PRINT_UTF16_SUPPORT 0
#define JSON_PRINT_NUM_INIT_DEF 1024
#define JSON_PRINT_NUM_PLUS_DEF 64
#define JSON_PRINT_DEPTH_DEF 16
#define JSON_PRINT_SIZE_PLUS_DEF 1024
#define JSON_FORMAT_ITEM_SIZE_DEF 32
#define JSON_UNFORMAT_ITEM_SIZE_DEF 24
/* file parse choice size */
#define JSON_PARSE_ERROR_STR "Z"
#define JSON_PARSE_READ_SIZE_DEF 8192
#define JSON_PARSE_NUM_DIV_DEF 8
/**************** json list ****************/
/* args of json_list_entry is different to list_entry of linux */
#define json_list_entry(ptr, type) ((type *)(ptr))
#define json_list_for_each_entry(pos, head, member) \
for (pos = json_list_entry((head)->next, typeof(*pos)); \
&pos->member != (struct json_list *)(head); \
pos = json_list_entry(pos->member.next, typeof(*pos)))
#define json_list_for_each_entry_safe(p, pos, n, head, member) \
for (p = json_list_entry((head), typeof(*pos)), \
pos = json_list_entry((head)->next, typeof(*pos)), \
n = json_list_entry(pos->member.next, typeof(*pos)); \
&pos->member != (struct json_list *)(head); \
p = pos, pos = n, n = json_list_entry(n->member.next, typeof(*n)))
static inline void INIT_JSON_LIST_HEAD(struct json_list_head *head)
{
head->next = (struct json_list *)head;
head->prev = (struct json_list *)head;
}
static inline void json_list_add_head(struct json_list *list, struct json_list_head *head)
{
if (head->next == (struct json_list *)head) {
head->prev = list;
}
list->next = head->next;
head->next = list;
}
static inline void json_list_add_tail(struct json_list *list, struct json_list_head *head)
{
list->next = head->prev->next;
head->prev->next = list;
head->prev = list;
}
static inline void json_list_add(struct json_list *list, struct json_list *prev, struct json_list_head *head)
{
if (prev->next == (struct json_list *)head) {
head->prev = list;
}
list->next = prev->next;
prev->next = list;
}
static inline void json_list_del(struct json_list *list, struct json_list *prev, struct json_list_head *head)
{
if (list->next == (struct json_list *)head) {
head->prev = prev;
}
prev->next = list->next;
list->next = NULL;
}
/**************** json normal apis ****************/
void json_memory_free(void *ptr)
{
if (ptr)
json_free(ptr);
}
static inline int _item_total_get(json_object *json)
{
int cnt = 0;
json_object *item = NULL;
switch (json->type_member) {
case JSON_ARRAY:
case JSON_OBJECT:
json_list_for_each_entry(item, &json->value.head, list) {
cnt += _item_total_get(item);
}
break;
default:
break;
}
++cnt;
return cnt;
}
int json_item_total_get(json_object *json)
{
if (!json)
return 0;
return _item_total_get(json);
}
void json_del_object(json_object *json)
{
json_object *p = NULL, *pos = NULL, *n = NULL;
if (!json)
return;
if (json->key_member)
json_free(json->key_member);
switch (json->type_member) {
case JSON_STRING:
if (json->value.str_member)
json_free(json->value.str_member);
break;
case JSON_ARRAY:
case JSON_OBJECT:
json_list_for_each_entry_safe(p, pos, n, &json->value.head, list) {
json_list_del(&pos->list, &p->list, &json->value.head);
json_del_object(pos);
pos = p;
}
break;
default:
break;
}
json_free(json);
}
json_object *json_new_object(json_type_t type)
{
json_object *json = NULL;
if ((json = json_calloc(1, sizeof(json_object))) == NULL) {
JsonErr("malloc failed!\n");
return NULL;
}
json->type_member = type;
switch (type) {
case JSON_ARRAY:
case JSON_OBJECT:
INIT_JSON_LIST_HEAD(&json->value.head);
break;
default:
break;
}
return json;
}
json_object *json_create_item(json_type_t type, void *value)
{
json_object *json = NULL;
if ((json = json_calloc(1, sizeof(json_object))) == NULL) {
JsonErr("malloc failed!\n");
return NULL;
}
json->type_member = type;
switch (type) {
case JSON_NULL:
break;
case JSON_BOOL: json->value.vnum.vbool = *(bool *)value; break;
case JSON_INT: json->value.vnum.vint = *(int32_t *)value; break;
case JSON_HEX: json->value.vnum.vhex = *(uint32_t *)value; break;
case JSON_LINT: json->value.vnum.vlint = *(int64_t *)value; break;
case JSON_LHEX: json->value.vnum.vlhex = *(uint64_t *)value; break;
case JSON_DOUBLE: json->value.vnum.vdbl = *(double *)value; break;
case JSON_STRING:
if (json_set_string_value(json, (json_string_t *)value) < 0)
return NULL;
break;
case JSON_ARRAY:
case JSON_OBJECT:
INIT_JSON_LIST_HEAD(&json->value.head);
break;
default:
break;
}
return json;
}
json_object *json_create_item_array(json_type_t type, void *values, int count)
{
int i = 0;
void *value = NULL;
json_object *json = NULL, *node = NULL;
if ((json = json_create_array()) == NULL) {
JsonErr("create array failed!\n");
return NULL;
}
for (i = 0; i < count; ++i) {
switch (type) {
case JSON_BOOL: value = ((bool *)values) + i; break;
case JSON_INT: value = ((int32_t *)values) + i; break;
case JSON_HEX: value = ((uint32_t *)values) + i; break;
case JSON_LINT: value = ((int64_t *)values) + i; break;
case JSON_LHEX: value = ((uint64_t *)values) + i; break;
case JSON_DOUBLE: value = ((double *)values) + i; break;
case JSON_STRING: value = ((json_string_t *)values) + i;break;
default: JsonErr("not support json type.\n"); goto err;
}
if ((node = json_create_item(type, value)) == NULL) {
JsonErr("create item failed!\n");
goto err;
}
json_list_add_tail(&node->list, &json->value.head);
}
return json;
err:
json_del_object(json);
return NULL;
}
void json_string_info_update(json_string_t *jstr)
{
const char *str = jstr->str;
int i = 0;
int escaped = 0;
if (jstr->len)
return;
if (str) {
for (i = 0; str[i]; ++i) {
switch (str[i]) {
case '\"': case '\\': case '\b': case '\f': case '\n': case '\r': case '\t': case '\v':
escaped = 1;
break;
default:
#if JSON_PRINT_UTF16_SUPPORT
{
if ((unsigned char)str[i] < ' ')
escaped = 1;
}
#endif
break;
}
}
}
jstr->escaped = escaped;
jstr->len = i;
}
uint32_t json_string_hash_code(json_string_t *jstr)
{
uint32_t i = 0, hash = 0;
json_string_info_update(jstr);
if (!jstr->len)
return 0;
for (i = 0; i < jstr->len; ++i)
hash = (hash << 5) - hash + jstr->str[i];
return hash;
}
int json_string_strdup(json_string_t *src, json_string_t *dst)
{
json_string_info_update(src);
json_string_info_update(dst);
if (src->len) {
if (dst->len < src->len) {
if ((dst->str = json_realloc(dst->str, src->len + 1)) == NULL) {
JsonErr("malloc failed!\n");
dst->len = 0;
return -1;
}
}
dst->escaped = src->escaped;
dst->len = src->len;
memcpy(dst->str, src->str, src->len);
dst->str[src->len] = '\0';
} else {
dst->escaped = 0;
dst->len = 0;
if (dst->str) {
json_free(dst->str);
dst->str = NULL;
}
}
return 0;
}
int json_get_number_value(json_object *json, json_type_t type, void *value)
{
#define _get_number(json, etype, val) do { \
switch (json->type_member) { \
case JSON_BOOL: *(etype *)val = (etype)json->value.vnum.vbool; break; \
case JSON_INT: *(etype *)val = (etype)json->value.vnum.vint; break; \
case JSON_HEX: *(etype *)val = (etype)json->value.vnum.vhex; break; \
case JSON_LINT: *(etype *)val = (etype)json->value.vnum.vlint; break; \
case JSON_LHEX: *(etype *)val = (etype)json->value.vnum.vlhex; break; \
case JSON_DOUBLE: *(etype *)val = (etype)json->value.vnum.vdbl; break; \
default: *(etype *)val = (etype)0; JsonErr("wrong type!\n"); return -1; \
} \
} while(0)
switch (type) {
case JSON_BOOL: _get_number(json, bool, value); break;
case JSON_INT: _get_number(json, int32_t, value); break;
case JSON_HEX: _get_number(json, uint32_t, value); break;
case JSON_LINT: _get_number(json, int64_t, value); break;
case JSON_LHEX: _get_number(json, uint64_t, value); break;
case JSON_DOUBLE: _get_number(json, double, value); break;
default: JsonErr("wrong type!\n"); return -1;
}
return json->type_member == type ? 0 : json->type_member;
}
int json_set_number_value(json_object *json, json_type_t type, void *value)
{
int ret = 0;
switch (json->type_member) {
case JSON_BOOL:
case JSON_INT:
case JSON_HEX:
case JSON_LINT:
case JSON_LHEX:
case JSON_DOUBLE:
break;
default:
JsonErr("wrong type!\n");
return -1;
}
switch (type) {
case JSON_BOOL: json->value.vnum.vbool = *(bool *)value; break;
case JSON_INT: json->value.vnum.vint = *(int32_t *)value; break;
case JSON_HEX: json->value.vnum.vhex = *(uint32_t *)value; break;
case JSON_LINT: json->value.vnum.vlint = *(int64_t *)value; break;
case JSON_LHEX: json->value.vnum.vlhex = *(uint64_t *)value; break;
case JSON_DOUBLE: json->value.vnum.vdbl = *(double *)value; break;
default: return -1;
}
if (json->type_member != type) {
ret = json->type_member;
json->type_member = type;
}
return ret;
}
int json_get_array_size(json_object *json)
{
int count = 0;
json_object *pos = NULL;
if (json->type_member == JSON_ARRAY) {
json_list_for_each_entry(pos, &json->value.head, list) {
++count;
}
}
return count;
}
int json_get_object_size(json_object *json)
{
int count = 0;
json_object *pos = NULL;
if (json->type_member == JSON_OBJECT) {
json_list_for_each_entry(pos, &json->value.head, list) {
++count;
}
}
return count;
}
json_object *json_get_array_item(json_object *json, int seq, json_object **prev)
{
int count = 0;
json_object *p = NULL, *pos = NULL, *n = NULL;
if (json->type_member == JSON_ARRAY) {
json_list_for_each_entry_safe(p, pos, n, &json->value.head, list) {
if (count++ == seq) {
if (prev)
*prev = p;
return pos;
}
}
}
return NULL;
}
json_object *json_get_object_item(json_object *json, const char *key, json_object **prev)
{
json_object *p = NULL, *pos = NULL, *n = NULL;
if (json->type_member == JSON_OBJECT) {
if (key && key[0]) {
json_list_for_each_entry_safe(p, pos, n, &json->value.head, list) {
if (pos->key_member && strcmp(key, pos->key_member) == 0) {
if (prev)
*prev = p;
return pos;
}
}
} else {
json_list_for_each_entry_safe(p, pos, n, &json->value.head, list) {
if (!pos->jkey.len) {
if (prev)
*prev = p;
return pos;
}
}
}
}
return NULL;
}
json_object *json_search_object_item(json_items_t *items, json_string_t *jkey, uint32_t hash)
{
int left = 0, right = 0, middle = 0, i = 0, count = 0;
json_object *json = NULL;
if (!hash)
hash = json_string_hash_code(jkey);
count = items->count;
if (!count)
return NULL;
right = count - 1;
while (left <= right) {
middle = (left + right) >> 1;
if (hash == items->items[middle].hash) {
if (!items->conflicted)
return items->items[middle].json;
if (jkey->len) {
for (i = middle; i < count; ++i) {
if (hash != items->items[i].hash)
break;
json = items->items[i].json;
if (jkey->len == json->jkey.len && memcmp(jkey->str, json->key_member, jkey->len) == 0)
return json;
}
for (i = middle - 1; i >= 0; --i) {
if (hash != items->items[i].hash)
break;
json = items->items[i].json;
if (jkey->len == json->jkey.len && memcmp(jkey->str, json->key_member, jkey->len) == 0)
return json;
}
} else {
for (i = middle; i < count; ++i) {
if (hash != items->items[i].hash)
break;
json = items->items[i].json;
if (!json->jkey.len)
return json;
}
for (i = middle - 1; i >= 0; --i) {
if (hash != items->items[i].hash)
break;
json = items->items[i].json;
if (!json->jkey.len)
return json;
}
}
return NULL;
}
if (hash > items->items[middle].hash)
left = middle + 1;
else
right = middle - 1;
}
return NULL;
}
static int _json_hash_cmp(const void *a, const void *b)
{
uint32_t ha = ((const json_item_t *)a)->hash;
uint32_t hb = ((const json_item_t *)b)->hash;
return (int)(ha - hb);
}
static inline void json_object_items_sort(json_items_t *items)
{
items->conflicted = 0;
if (items->count > 1) {
uint32_t i = 0;
qsort(items->items, items->count, sizeof(json_item_t), _json_hash_cmp);
for (i = 1; i < items->count; ++i) {
if (items->items[i-1].hash == items->items[i].hash) {
items->conflicted = 1;
break;
}
}
}
}
void json_free_items(json_items_t *items)
{
if (items->items)
json_free(items->items);
items->items = NULL;
items->conflicted = 0;
items->total = 0;
items->count = 0;
}
int json_get_items(json_object *json, json_items_t *items)
{
json_object *pos = NULL;
items->count = 0;
if (json->type_member == JSON_ARRAY) {
json_list_for_each_entry(pos, &json->value.head, list) {
if (items->count == items->total) {
items->total += JSON_ITEM_NUM_PLUS_DEF;
if ((items->items = json_realloc(items->items, items->total * sizeof(json_item_t))) == NULL) {
JsonErr("malloc failed!\n");
goto err;
}
}
items->items[items->count++].json = pos;
}
return 0;
}
if (json->type_member == JSON_OBJECT) {
json_list_for_each_entry(pos, &json->value.head, list) {
if (items->count == items->total) {
items->total += JSON_ITEM_NUM_PLUS_DEF;
if ((items->items = json_realloc(items->items, items->total * sizeof(json_item_t))) == NULL) {
JsonErr("malloc failed!\n");
goto err;
}
}
items->items[items->count].hash = json_string_hash_code(&pos->jkey);
items->items[items->count++].json = pos;
}
json_object_items_sort(items);
return 0;
}
return -1;
err:
json_free_items(items);
return -1;
}
int json_add_item_to_array(json_object *array, json_object *item)
{
if (array->type_member == JSON_ARRAY) {
json_list_add_tail(&item->list, &array->value.head);
return 0;
}
return -1;
}
int json_add_item_to_object(json_object *object, json_object *item)
{
if (object->type_member == JSON_OBJECT) {
json_list_add_tail(&item->list, &object->value.head);
return 0;
}
return -1;
}
json_object *json_detach_item_from_array(json_object *json, int seq)
{
json_object *item = NULL, *prev = NULL;
if ((item = json_get_array_item(json, seq, &prev)) == NULL)
return NULL;
json_list_del(&item->list, &prev->list, &json->value.head);
return item;
}
json_object *json_detach_item_from_object(json_object *json, const char *key)
{
json_object *item = NULL, *prev = NULL;
if ((item = json_get_object_item(json, key, &prev)) == NULL)
return NULL;
json_list_del(&item->list, &prev->list, &json->value.head);
return item;
}
int json_del_item_from_array(json_object *json, int seq)
{
json_object *item = NULL;
if ((item = json_detach_item_from_array(json, seq)) == NULL)
return -1;
json_del_object(item);
return 0;
}
int json_del_item_from_object(json_object *json, const char *key)
{
json_object *item = NULL;
if ((item = json_detach_item_from_object(json, key)) == NULL)
return -1;
json_del_object(item);
return 0;
}
int json_replace_item_in_array(json_object *array, int seq, json_object *new_item)
{
json_object *item = NULL, *prev = NULL;
if (array->type_member == JSON_ARRAY) {
if ((item = json_get_array_item(array, seq, &prev)) != NULL) {
json_list_del(&item->list, &prev->list, &array->value.head);
json_del_object(item);
json_list_add(&new_item->list, &prev->list, &array->value.head);
} else {
json_list_add_tail(&new_item->list, &array->value.head);
}
return 0;
}
return -1;
}
int json_replace_item_in_object(json_object *object, json_object *new_item)
{
json_object *item = NULL, *prev = NULL;
if (object->type_member == JSON_OBJECT) {
if ((item = json_get_object_item(object, new_item->key_member, &prev)) != NULL) {
json_list_del(&item->list, &prev->list, &object->value.head);
json_del_object(item);
json_list_add(&new_item->list, &prev->list, &object->value.head);
} else {
json_list_add_tail(&new_item->list, &object->value.head);
}
return 0;
}
return -1;
}
json_object *json_deepcopy(json_object *json)
{
json_object *new_json = NULL;
json_object *item = NULL, *node = NULL;
switch (json->type_member) {
case JSON_NULL: new_json = json_create_null(); break;
case JSON_BOOL: new_json = json_create_bool(json->value.vnum.vbool); break;
case JSON_INT: new_json = json_create_int(json->value.vnum.vint); break;
case JSON_HEX: new_json = json_create_hex(json->value.vnum.vhex); break;
case JSON_LINT: new_json = json_create_lint(json->value.vnum.vlint); break;
case JSON_LHEX: new_json = json_create_lhex(json->value.vnum.vlhex); break;
case JSON_DOUBLE: new_json = json_create_double(json->value.vnum.vdbl); break;
case JSON_STRING: new_json = json_create_string(&json->value.vstr); break;
case JSON_ARRAY: new_json = json_create_array(); break;
case JSON_OBJECT: new_json = json_create_object(); break;
default: break;
}
if (new_json) {
if (json_string_strdup(&json->jkey, &new_json->jkey) < 0) {
JsonErr("add key failed!\n");
json_del_object(new_json);
return NULL;
}
if (json->type_member == JSON_ARRAY || json->type_member == JSON_OBJECT) {
json_list_for_each_entry(item, &json->value.head, list) {
if ((node = json_deepcopy(item)) == NULL) {
JsonErr("copy failed!\n");
json_del_object(new_json);
return NULL;
}
json_list_add_tail(&node->list, &new_json->value.head);
}
}
}
return new_json;
}
int json_copy_item_to_array(json_object *array, json_object *item)
{
json_object *node = NULL;
if (array->type_member == JSON_ARRAY) {
if ((node = json_deepcopy(item)) == NULL) {
JsonErr("copy failed!\n");
return -1;
}
json_list_add_tail(&node->list, &array->value.head);
return 0;
}
return -1;
}
int json_copy_item_to_object(json_object *object, json_object *item)
{
json_object *node = NULL;
if (object->type_member == JSON_OBJECT) {
if ((node = json_deepcopy(item)) == NULL) {
JsonErr("copy failed!\n");
return -1;
}
json_list_add_tail(&node->list, &object->value.head);
return 0;
}
return -1;
}
json_object *json_add_new_item_to_array(json_object *array, json_type_t type, void *value)
{
json_object *item = NULL;
if (array->type_member == JSON_ARRAY) {
switch (type) {
case JSON_NULL:
case JSON_BOOL:
case JSON_INT:
case JSON_HEX:
case JSON_LINT:
case JSON_LHEX:
case JSON_DOUBLE:
case JSON_STRING:
case JSON_ARRAY:
case JSON_OBJECT:
if ((item = json_create_item(type, value)) == NULL) {
JsonErr("create item failed!\n");
return NULL;
}
json_list_add_tail(&item->list, &array->value.head);
return item;
default:
JsonErr("not support json type.\n");
return NULL;
}
}
return NULL;
}
json_object *json_add_new_item_to_object(json_object *object, json_type_t type, json_string_t *jkey, void *value)
{
json_object *item = NULL;
if (object->type_member == JSON_OBJECT) {
switch (type) {
case JSON_NULL:
case JSON_BOOL:
case JSON_INT:
case JSON_HEX:
case JSON_LINT:
case JSON_LHEX:
case JSON_DOUBLE:
case JSON_STRING:
case JSON_ARRAY:
case JSON_OBJECT:
if ((item = json_create_item(type, value)) == NULL) {
JsonErr("create item failed!\n");
return NULL;
}
if (json_set_key(item, jkey) < 0) {
JsonErr("add key failed!\n");
json_del_object(item);
return NULL;
}
json_list_add_tail(&item->list, &object->value.head);
return item;
default:
JsonErr("not support json type.\n");
return NULL;
}
}
return NULL;
}
/**************** json pool memory apis ****************/
static json_mem_node_t s_invalid_json_mem_node;
static json_mem_t s_invalid_json_mem;
static void _json_mem_init(json_mem_mgr_t *mgr)
{
INIT_JSON_LIST_HEAD(&mgr->head);
mgr->cur_node = &s_invalid_json_mem_node;
mgr->mem_size = JSON_POOL_MEM_SIZE_DEF;
}
static void _json_mem_free(json_mem_mgr_t *mgr)
{
json_mem_node_t *p = NULL, *pos = NULL, *n = NULL;
json_list_for_each_entry_safe(p, pos, n, &mgr->head, list) {
json_list_del(&pos->list, &p->list, &mgr->head);
json_free(pos->ptr);
json_free(pos);
pos = p;
}
mgr->cur_node = &s_invalid_json_mem_node;
}
static void _json_mem_refresh(json_mem_mgr_t *mgr)
{
json_mem_node_t *p = NULL, *pos = NULL, *n = NULL;
int i = 0;
json_list_for_each_entry_safe(p, pos, n, &mgr->head, list) {
if (++i == 1) {
pos->cur = pos->ptr;
mgr->cur_node = pos;
} else {
json_list_del(&pos->list, &p->list, &mgr->head);
json_free(pos->ptr);
json_free(pos);
pos = p;
}