-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson.h
1562 lines (1358 loc) · 52.3 KB
/
json.h
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 *
*******************************************/
#pragma once
#include "jnum.h"
#define JSON_VERSION 0x020102
#define JSON_SAX_APIS_SUPPORT 1
/**************** json object structure ****************/
/*
* struct json_list - the value of json list
* @next: the next list
* @description: LJSON uses it to manage json objects and memory blocks.
*/
struct json_list {
struct json_list *next;
};
/*
* struct json_list_head - the head of json list
* @next: the next list
* @prev: the last list
* @description: LJSON uses it to manage json objects and memory blocks.
*/
struct json_list_head {
struct json_list *next, *prev;
};
/*
* json_type_t - the json object type
* @description: LJSON supports not only standard types, but also extended types (JSON HEX...).
*/
typedef enum {
JSON_NULL = 0, /* It doesn't has value variable: null */
JSON_BOOL, /* Its value variable is vbool: true, false */
JSON_INT, /* Its value variable is vint */
JSON_HEX, /* Its value variable is vhex */
JSON_LINT, /* Its value variable is vlint */
JSON_LHEX, /* Its value variable is vlhex */
JSON_DOUBLE, /* Its value variable is vdbl */
JSON_STRING, /* Its value variable is vstr */
JSON_ARRAY, /* Its value variable is head */
JSON_OBJECT /* Its value variable is head */
} json_type_t;
/*
* json_string_t - the json string object value or type-key value
* @type: the json object type (json_type_t), it is only valid when used as a key
* @escaped: whether the string contains characters that need to be escaped
* @alloced: whether the string is alloced, it is only valid for SAX APIs
* @len: the length of string
* @str: the value of string
* @description: LJSON uses string with information to accelerate printing.
*/
typedef struct {
uint32_t type:4;
uint32_t escaped:1;
uint32_t alloced:1;
uint32_t reserved:2;
uint32_t len:24;
char *str;
} json_string_t;
/*
* json_number_t - the json number object value
* @description: LJSON supports decimal and hexadecimal, integer and long long integer.
*/
typedef jnum_value_t json_number_t;
#if JSON_SAX_APIS_SUPPORT
/*
* json_sax_cmd_t - the beginning and end of JSON_ARRAY or JSON_OBJECT object
* @description: We know that parentheses have two sides, `JSON_SAX_START` indicates left side,
* and `JSON_SAX_FINISH` indicates right side.
*/
typedef enum {
JSON_SAX_START = 0,
JSON_SAX_FINISH
} json_sax_cmd_t;
#endif
/*
* json_value_t - the json object value
* @vnum: the numerical value
* @vstr: the string value
* @vcmd: the SAX array or object value, only for SAX APIs
* @head: the DOM array or object value, LJSON manages child json objects through the list head
* @description: LJSON uses union to manage the value of different objects to save memory.
*/
typedef union {
json_number_t vnum;
json_string_t vstr;
#if JSON_SAX_APIS_SUPPORT
json_sax_cmd_t vcmd;
#endif
struct json_list_head head;
} json_value_t;
/*
* json_object - the json object
* @list: the list value, LJSON associates `list` to the `head` of parent json object
* or the `list` of brother json objects
* @jkey: the json object type and key, only the child objects of JSON_OBJECT have key
* @value: the json object value
* @description: LJSON uses union to manage the value of different objects to save memory.
*/
typedef struct {
struct json_list list;
json_string_t jkey;
json_value_t value;
} json_object;
/*
* json_item_t - the json item which contains json object and hash code
* @hash: the hash code of key, only child json objects of JSON_OBJECT has the value
* @json: the json object
* @description: LJSON uses hash code to accelerate access to member of JSON_OBJECT.
*/
typedef struct {
uint32_t hash;
json_object *json;
} json_item_t;
/*
* json_items_t - the json items array
* @conflicted: whether the key hashes are conflicted in items, only for JSON_OBJECT parent
* @total: the total size of items
* @count: the total number of child json objects of JSON_ARRAY or JSON_OBJECT
* @json: the array to store child json objects
* @description: LJSON uses it to store all members of JSON_ARRAY or JSON_OBJECT.
*/
typedef struct {
uint32_t conflicted:1;
uint32_t reserved:31;
uint32_t total;
uint32_t count;
json_item_t *items;
} json_items_t;
/**************** json classic editor ****************/
/*
* json_memory_free - Free the ptr alloced by LJSON
* @ptr: IN, the alloced ptr
* @description: the alloced ptr may be:
* 1. the returned string by json_print_common or json_sax_print_finish when printing to string
* 2. the LJSON style string alloced by LJSON classic(not pool) APIS
*/
void json_memory_free(void *ptr);
/*
* json_item_total_get - Get the total number of json tree (recursive)
* @json: IN, the json object
* @return: the total number
*/
int json_item_total_get(json_object *json);
/*
* json_del_object - Delete the json object, includes the child objects (recursive)
* @json: IN, the json object
* @return: None
*/
void json_del_object(json_object *json);
/*
* json_new_object - Create a json object without value
* @type: IN, the json object type
* @return: NULL on failure, a pointer on success
*/
json_object *json_new_object(json_type_t type);
/*
* json_create_item - Create a json object with value
* @type: IN, the json object type
* @value: IN, the json object value
* @return: NULL on failure, a pointer on success
*/
json_object *json_create_item(json_type_t type, void *value);
static inline json_object *json_create_null(void)
{
return json_new_object(JSON_NULL);
}
static inline json_object *json_create_bool(bool value)
{
return json_create_item(JSON_BOOL, &value);
}
static inline json_object *json_create_int(int32_t value)
{
return json_create_item(JSON_INT, &value);
}
static inline json_object *json_create_hex(uint32_t value)
{
return json_create_item(JSON_HEX, &value);
}
static inline json_object *json_create_lint(int64_t value)
{
return json_create_item(JSON_LINT, &value);
}
static inline json_object *json_create_lhex(uint64_t value)
{
return json_create_item(JSON_LHEX, &value);
}
static inline json_object *json_create_double(double value)
{
return json_create_item(JSON_DOUBLE, &value);
}
static inline json_object *json_create_string(json_string_t *value)
{
return json_create_item(JSON_STRING, value);
}
static inline json_object *json_create_array(void)
{
return json_new_object(JSON_ARRAY);
}
static inline json_object *json_create_object(void)
{
return json_new_object(JSON_OBJECT);
}
/*
* json_create_item_array - Create a JSON_ARRAY object with a group of child json objects
* @type: IN, the type of child json objects
* @values: IN, the values of child json objects
* @count: IN, the total number of child json objects
* @return: NULL on failure, a pointer on success
*/
json_object *json_create_item_array(json_type_t type, void *values, int count);
static inline json_object *json_create_bool_array(bool *values, int count)
{
return json_create_item_array(JSON_BOOL, values, count);
}
static inline json_object *json_create_int_array(int32_t *values, int count)
{
return json_create_item_array(JSON_INT, values, count);
}
static inline json_object *json_create_hex_array(uint32_t *values, int count)
{
return json_create_item_array(JSON_HEX, values, count);
}
static inline json_object *json_create_lint_array(int64_t *values, int count)
{
return json_create_item_array(JSON_LINT, values, count);
}
static inline json_object *json_create_lhex_array(uint64_t *values, int count)
{
return json_create_item_array(JSON_LHEX, values, count);
}
static inline json_object *json_create_double_array(double *values, int count)
{
return json_create_item_array(JSON_DOUBLE, values, count);
}
static inline json_object *json_create_string_array(json_string_t *values, int count)
{
return json_create_item_array(JSON_STRING, values, count);
}
/*
* json_string_info_update - Update the parameters for json string
* @jstr: INOUT, the LJSON string
* @return: None
* @description: If jstr->len is not equal to 0, the parameters will not be updated.
*/
void json_string_info_update(json_string_t *jstr);
/*
* json_string_hash_code - Calculate the hash code of json string
* @jstr: IN, the LJSON string
* @return: the hash value
*/
uint32_t json_string_hash_code(json_string_t *jstr);
/*
* json_string_strdup - Strdup the LJSON string src to dst
* @src: IN, the source string
* @dst: OUT, the destination string
* @return: -1 on failure, 0 on success
*/
int json_string_strdup(json_string_t *src, json_string_t *dst);
/*
* json_set_key - Set the key of json object
* @json: IN, the json object to be set
* @jkey: IN, the LJSON string key, allow length not to be set first by json_string_info_update
* @return: -1 on failure, 0 on success
*/
static inline int json_set_key(json_object *json, json_string_t *jkey)
{
return json_string_strdup(jkey, &json->jkey);
}
/*
* json_set_string_value - Set the string of JSON_STRING object
* @json: IN, the json object to be set
* @jstr: IN, the LJSON string value, allow length not to be set first by json_string_info_update
* @return: -1 on failure, 0 on success
*/
static inline int json_set_string_value(json_object *json, json_string_t *jstr)
{
if (json->jkey.type == JSON_STRING) {
return json_string_strdup(jstr, &json->value.vstr);
}
return -1;
}
/*
* json_get_number_value - Get the value of numerical object
* @json: IN, the json object
* @type: IN, the json object type
* @value: OUT, the json object value
* @return: -1 on failure, 0 on success, >0(original type) on success with type cast
*/
int json_get_number_value(json_object *json, json_type_t type, void *value);
static inline bool json_get_bool_value(json_object *json)
{
bool value = 0;
json_get_number_value(json, JSON_BOOL, &value);
return value;
}
static inline int32_t json_get_int_value(json_object *json)
{
int32_t value = 0;
json_get_number_value(json, JSON_INT, &value);
return value;
}
static inline uint32_t json_get_hex_value(json_object *json)
{
uint32_t value = 0;
json_get_number_value(json, JSON_HEX, &value);
return value;
}
static inline int64_t json_get_lint_value(json_object *json)
{
int64_t value = 0;
json_get_number_value(json, JSON_LINT, &value);
return value;
}
static inline uint64_t json_get_lhex_value(json_object *json)
{
uint64_t value = 0;
json_get_number_value(json, JSON_LHEX, &value);
return value;
}
static inline double json_get_double_value(json_object *json)
{
double value = 0;
json_get_number_value(json, JSON_DOUBLE, &value);
return value;
}
/*
* json_set_number_value - Set the value of numerical object
* @json: IN, the json object
* @type: IN, the json object type
* @value: IN, the json object value
* @return: -1 on failure, 0 on success, >0(original type) on success with type cast
*/
int json_set_number_value(json_object *json, json_type_t type, void *value);
static inline int json_set_bool_value(json_object *json, bool value)
{
return json_set_number_value(json, JSON_BOOL, &value);
}
static inline int json_set_int_value(json_object *json, int32_t value)
{
return json_set_number_value(json, JSON_INT, &value);
}
static inline int json_set_hex_value(json_object *json, uint32_t value)
{
return json_set_number_value(json, JSON_HEX, &value);
}
static inline int json_set_lint_value(json_object *json, int64_t value)
{
return json_set_number_value(json, JSON_LINT, &value);
}
static inline int json_set_lhex_value(json_object *json, uint64_t value)
{
return json_set_number_value(json, JSON_LHEX, &value);
}
static inline int json_set_double_value(json_object *json, double value)
{
return json_set_number_value(json, JSON_DOUBLE, &value);
}
/*
* json_get_array_size - Get the total child objects of JSON_ARRAY object (not recursive)
* @json: IN, the JSON_ARRAY object
* @return: the total child objects
*/
int json_get_array_size(json_object *json);
/*
* json_get_object_size - Get the total child objects of JSON_OBJECT object (not recursive)
* @json: IN, the JSON_OBJECT object
* @return: the total child objects
*/
int json_get_object_size(json_object *json);
/*
* json_get_array_item - Get the specified object in JSON_ARRAY object
* @json: IN, the JSON_ARRAY object
* @seq: IN, the sequence number
* &prev: OUT, to store the previous JSON object, it can be NULL
* @return: NULL on failure, a pointer on success
*/
json_object *json_get_array_item(json_object *json, int seq, json_object **prev);
/*
* json_get_object_item - Get the specified object in JSON_OBJECT object
* @json: IN, the JSON_OBJECT object
* @key: IN, the specified key
* &prev: OUT, to store the previous JSON object, it can be NULL
* @return: NULL on failure, a pointer on success
*/
json_object *json_get_object_item(json_object *json, const char *key, json_object **prev);
/*
* json_search_object_item - Search the specified object in chiild items of JSON_OBJECT object
* @items: IN, all child json objects of JSON_OBJECT
* @jkey: IN, the searched LJSON string key, allow length not to be set first by json_string_info_update
* @hash: IN, the hash value for searched key, if it is zero, the func will calculate actual hash
* @return: NULL on failure, a pointer on success
* @description: LJSON uses dichotomy to search the specific json object.
*/
json_object *json_search_object_item(json_items_t *items, json_string_t *jkey, uint32_t hash);
/*
* json_free_items - Free alloced memory in json_items_t
* @items: INOUT, the json items
* @return: none
*/
void json_free_items(json_items_t *items);
/*
* json_get_items - Get all child json objects of JSON_ARRAY or JSON_OBJECT
* @json: IN, the JSON_ARRAY or JSON_OBJECT object to get
* @items: INOUT, the json items
* @return: -1 on failure, 0 on success
* @description: LJSON uses it to accelerate access to member of JSON_ARRAY or JSON_OBJECT
*/
int json_get_items(json_object *json, json_items_t *items);
/*
* json_add_item_to_array - Add the specified object to the JSON_ARRAY object
* @array: IN, the JSON_ARRAY object
* @item: IN, the json object to add
* @return: -1 on failure, 0 on success
* @description: The item will be added to the end, once successfully added,
* it is no longer necessary to manually delete item.
*/
int json_add_item_to_array(json_object *array, json_object *item);
/*
* json_add_item_to_object - Add the specified object to the JSON_OBJECT object
* @object: IN, the JSON_OBJECT object
* @item: IN, the json object to add, users should set the key of item first
* @return: -1 on failure, 0 on success
* @description: The item will be added to the end, once successfully added,
* it is no longer necessary to manually delete item.
*/
int json_add_item_to_object(json_object *object, json_object *item);
/*
* json_detach_item_from_array - Detach the specified object in JSON_ARRAY object
* @json: IN, the JSON_ARRAY object
* @seq: IN, the sequence number
* @return: NULL on failure, a pointer (the detached object) on success
* @description: After use, users need to delete the returned object in classic jsons,
* don't delete it in pool jsons.
*/
json_object *json_detach_item_from_array(json_object *json, int seq);
/*
* json_detach_item_from_object - Detach the specified object in JSON_OBJECT object
* @json: IN, the JSON_ARRAY object
* @key: IN, the specified key
* @return: NULL on failure, a pointer (the detached object) on success
* @description: After use, users need to delete the returned object in classic jsons,
* don't delete it in pool jsons.
*/
json_object *json_detach_item_from_object(json_object *json, const char *key);
/*
* json_del_item_from_array - Delete the specified object in JSON_ARRAY object
* @json: IN, the JSON_ARRAY object
* @seq: IN, the sequence number
* @return: -1 on not found, 0 on found and deleted
*/
int json_del_item_from_array(json_object *json, int seq);
/*
* json_del_item_from_object - Delete the specified object in JSON_OBJECT object
* @json: IN, the JSON_OBJECT object
* @key: IN, the specified key
* @return: -1 on not found, 0 on found and deleted
*/
int json_del_item_from_object(json_object *json, const char *key);
/*
* json_replace_item_in_array - Replace the specified object in JSON_ARRAY object with new json object
* @array: IN, the JSON_ARRAY object
* @seq: IN, the sequence number
* @new_item: IN, the new json object
* @return: -1 on failure, 0 on success
* @description: If seq is not satisfied, new_item will be added to the end.
*/
int json_replace_item_in_array(json_object *array, int seq, json_object *new_item);
/*
* json_replace_item_in_object - Replace the specified object in JSON_OBJECT object with new json object
* @object: IN, the JSON_OBJECT object
* @new_item: IN, the new json object, users should set the key of new_item first
* @return: -1 on failure, 0 on success
* @description: If key is not satisfied, new_item will be added to the end.
*/
int json_replace_item_in_object(json_object *object, json_object *new_item);
/*
* json_deepcopy - Deep copy the json object (recursive)
* @json: IN, the source json object
* @return: NULL on failure, a pointer on success
*/
json_object *json_deepcopy(json_object *json);
/*
* json_copy_item_to_array - Copy the specified object, the add it to the JSON_ARRAY object
* @array: IN, the JSON_ARRAY object
* @item: IN, the json object to copy and add
* @return: -1 on failure, 0 on success
* @description: The item will be added to the end, once successfully added,
* it is also necessary to manually delete item.
*/
int json_copy_item_to_array(json_object *array, json_object *item);
/*
* json_copy_item_to_object - Copy the specified object, the add it to the JSON_OBJECT object
* @object: IN, the JSON_OBJECT object
* @item: IN, the json object to copy and add, users should set the key of item first
* @return: -1 on failure, 0 on success
* @description: The item will be added to the end, once successfully added,
* it is also necessary to manually delete item.
*/
int json_copy_item_to_object(json_object *object, json_object *item);
/*
* json_add_new_item_to_array - Create a new object, the add it to the JSON_ARRAY object
* @array: IN, the JSON_ARRAY object
* @type: the json object type
* @value: IN, the json object value
* @return: NULL on failure, a pointer on success
* @description: the pointer points to the created json object
*/
json_object *json_add_new_item_to_array(json_object *array, json_type_t type, void* value);
static inline json_object *json_add_null_to_array(json_object *array)
{
return json_add_new_item_to_array(array, JSON_NULL, NULL);
}
static inline json_object *json_add_bool_to_array(json_object *array, bool value)
{
return json_add_new_item_to_array(array, JSON_BOOL, &value);
}
static inline json_object *json_add_int_to_array(json_object *array, int32_t value)
{
return json_add_new_item_to_array(array, JSON_INT, &value);
}
static inline json_object *json_add_hex_to_array(json_object *array, uint32_t value)
{
return json_add_new_item_to_array(array, JSON_HEX, &value);
}
static inline json_object *json_add_lint_to_array(json_object *array, int64_t value)
{
return json_add_new_item_to_array(array, JSON_LINT, &value);
}
static inline json_object *json_add_lhex_to_array(json_object *array, uint64_t value)
{
return json_add_new_item_to_array(array, JSON_LHEX, &value);
}
static inline json_object *json_add_double_to_array(json_object *array, double value)
{
return json_add_new_item_to_array(array, JSON_DOUBLE, &value);
}
static inline json_object *json_add_string_to_array(json_object *array, json_string_t *value)
{
return json_add_new_item_to_array(array, JSON_STRING, &value);
}
static inline json_object *json_add_array_to_array(json_object *array)
{
return json_add_new_item_to_array(array, JSON_ARRAY, NULL);
}
static inline json_object *json_add_object_to_array(json_object *array)
{
return json_add_new_item_to_array(array, JSON_OBJECT, NULL);
}
/*
* json_add_new_item_to_object - Create a new object, the add it to the JSON_OBJECT object
* @object: IN, the JSON_OBJECT object
* @type: the json object type
* @jkey: IN, the LJSON string key, allow length not to be set first by json_string_info_update
* @value: IN, the json object value
* @return: NULL on failure, a pointer on success
* @description: the pointer points to the created json object
*/
json_object *json_add_new_item_to_object(json_object *object, json_type_t type, json_string_t *jkey, void* value);
static inline json_object *json_add_null_to_object(json_object *object, json_string_t *jkey)
{
return json_add_new_item_to_object(object, JSON_NULL, jkey, NULL);
}
static inline json_object *json_add_bool_to_object(json_object *object, json_string_t *jkey, bool value)
{
return json_add_new_item_to_object(object, JSON_BOOL, jkey, &value);
}
static inline json_object *json_add_int_to_object(json_object *object, json_string_t *jkey, int32_t value)
{
return json_add_new_item_to_object(object, JSON_INT, jkey, &value);
}
static inline json_object *json_add_hex_to_object(json_object *object, json_string_t *jkey, uint32_t value)
{
return json_add_new_item_to_object(object, JSON_HEX, jkey, &value);
}
static inline json_object *json_add_lint_to_object(json_object *object, json_string_t *jkey, int64_t value)
{
return json_add_new_item_to_object(object, JSON_LINT, jkey, &value);
}
static inline json_object *json_add_lhex_to_object(json_object *object, json_string_t *jkey, uint64_t value)
{
return json_add_new_item_to_object(object, JSON_LHEX, jkey, &value);
}
static inline json_object *json_add_double_to_object(json_object *object, json_string_t *jkey, double value)
{
return json_add_new_item_to_object(object, JSON_DOUBLE, jkey, &value);
}
static inline json_object *json_add_string_to_object(json_object *object, json_string_t *jkey, json_string_t *value)
{
return json_add_new_item_to_object(object, JSON_STRING, jkey, &value);
}
static inline json_object *json_add_array_to_object(json_object *object, json_string_t *jkey)
{
return json_add_new_item_to_object(object, JSON_ARRAY, jkey, NULL);
}
static inline json_object *json_add_object_to_object(json_object *object, json_string_t *jkey)
{
return json_add_new_item_to_object(object, JSON_OBJECT, jkey, NULL);
}
/*
* The below APIs are also available to pool json:
* json_item_total_get
* json_string_info_update
* json_get_number_value / ...
* json_set_number_value / ...
* json_get_array_size
* json_get_object_size
* json_get_array_item
* json_get_object_item
* json_search_object_item
* json_free_items
* json_get_items
* json_add_item_to_array
* json_add_item_to_object
* json_detach_item_from_array
* json_detach_item_from_object
*/
/**************** json pool editor ****************/
/*
* json_mem_node_t - the block memory node
* @list: the list value, LJSON associates `list` to the `head` of json_mem_mgr_t
* or the `list` of brother json_mem_node_t
* @size: the memory size
* @ptr: the memory pointer
* @cur: the current memory pointer
* @description: LJSON can use the block memory to accelerate memory allocation and save memory space.
*/
typedef struct {
struct json_list list;
size_t size;
char *ptr;
char *cur;
} json_mem_node_t;
/*
* json_mem_mgr_t - the node to manage block memory node
* @head: the list head, LJSON manages block memory nodes through the list head
* @mem_size: the default memory size to allocate, its default value is JSON_POOL_MEM_SIZE_DEF(8096)
* @cur_node: the current block memory node
* @description: the manage node manages a group of block memory nodes.
*/
typedef struct {
struct json_list_head head;
size_t mem_size;
json_mem_node_t *cur_node;
} json_mem_mgr_t;
/*
* json_mem_t - the head to manage all types of block memory
* @obj_mgr: the node to manage json_object
* @key_mgr: the node to manage the value of key
* @str_mgr: the node to manage the value of JSON_STRING object
* @description: The reason for dividing into multiple management nodes is that
* there is a memory address alignment requirement for json_object.
*/
typedef struct {
json_mem_mgr_t obj_mgr;
json_mem_mgr_t key_mgr;
json_mem_mgr_t str_mgr;
} json_mem_t;
/*
* pjson_memory_free - Free all block memory
* @mem: IN, the block memory manager
* @description: Users need to call it to delete all json objects, not one.
*/
void pjson_memory_free(json_mem_t *mem);
/*
* pjson_memory_init - Initializate the block memory manager
* @mem: IN, the block memory manager to be initializated
* @return: None
* @description: Users need to call it before using block memory apis.
* User can re-set `mem_size` after calling it.
*/
void pjson_memory_init(json_mem_t *mem);
/*
* pjson_memory_statistics - Count the alloced memory of the memory blocks
* @mgr: IN, the block memory manager node
* @return: the alloced memory size
*/
int pjson_memory_statistics(json_mem_mgr_t *mgr);
/*
* pjson_new_object - Create a pool json object without value
* @type: IN, the json object type
* @mem: IN, the block memory manager
* @return: NULL on failure, a pointer on success
*/
json_object *pjson_new_object(json_type_t type, json_mem_t *mem);
/*
* pjson_create_item - Create a pool json object with value
* @type: IN, the json object type
* @value: IN, the json object value
* @mem: IN, the block memory manager
* @return: NULL on failure, a pointer on success
*/
json_object *pjson_create_item(json_type_t type, void *value, json_mem_t *mem);
static inline json_object *pjson_create_null(json_mem_t *mem)
{
return pjson_new_object(JSON_NULL, mem);
}
static inline json_object *pjson_create_bool(bool value, json_mem_t *mem)
{
return pjson_create_item(JSON_BOOL, &value, mem);
}
static inline json_object *pjson_create_int(int32_t value, json_mem_t *mem)
{
return pjson_create_item(JSON_INT, &value, mem);
}
static inline json_object *pjson_create_hex(uint32_t value, json_mem_t *mem)
{
return pjson_create_item(JSON_HEX, &value, mem);
}
static inline json_object *pjson_create_lint(int64_t value, json_mem_t *mem)
{
return pjson_create_item(JSON_LINT, &value, mem);
}
static inline json_object *pjson_create_lhex(uint64_t value, json_mem_t *mem)
{
return pjson_create_item(JSON_LHEX, &value, mem);
}
static inline json_object *pjson_create_double(double value, json_mem_t *mem)
{
return pjson_create_item(JSON_DOUBLE, &value, mem);
}
static inline json_object *pjson_create_string(json_string_t *value, json_mem_t *mem)
{
return pjson_create_item(JSON_STRING, &value, mem);
}
static inline json_object *pjson_create_array(json_mem_t *mem)
{
return pjson_new_object(JSON_ARRAY, mem);
}
static inline json_object *pjson_create_object(json_mem_t *mem)
{
return pjson_new_object(JSON_OBJECT, mem);
}
/*
* pjson_create_item_array - Create a pool JSON_ARRAY object with a group of child json objects
* @type: IN, the type of child json objects
* @values: IN, the values of child json objects
* @count: IN, the total number of child json objects
* @mem: IN, the block memory manager
* @return: NULL on failure, a pointer on success
*/
json_object *pjson_create_item_array(json_type_t item_type, void *values, int count, json_mem_t *mem);
static inline json_object *pjson_create_bool_array(bool *values, int count, json_mem_t *mem)
{
return pjson_create_item_array(JSON_BOOL, values, count, mem);
}
static inline json_object *pjson_create_int_array(int32_t *values, int count, json_mem_t *mem)
{
return pjson_create_item_array(JSON_INT, values, count, mem);
}
static inline json_object *pjson_create_hex_array(uint32_t *values, int count, json_mem_t *mem)
{
return pjson_create_item_array(JSON_HEX, values, count, mem);
}
static inline json_object *pjson_create_lint_array(int64_t *values, int count, json_mem_t *mem)
{
return pjson_create_item_array(JSON_LINT, values, count, mem);
}
static inline json_object *pjson_create_lhex_array(uint64_t *values, int count, json_mem_t *mem)
{
return pjson_create_item_array(JSON_LHEX, values, count, mem);
}
static inline json_object *pjson_create_double_array(double *values, int count, json_mem_t *mem)
{
return pjson_create_item_array(JSON_DOUBLE, values, count, mem);
}
static inline json_object *pjson_create_string_array(json_string_t *values, int count, json_mem_t *mem)
{
return pjson_create_item_array(JSON_STRING, values, count, mem);
}
/*
* pjson_string_strdup - Strdup the LJSON string src to dst
* @src: IN, the source string
* @dst: OUT, the destination string
* @return: -1 on failure, 0 on success
*/
int pjson_string_strdup(json_string_t *src, json_string_t *dst, json_mem_mgr_t *mgr);
/*
* pjson_set_key - Set the key of json object
* @json: IN, the json object to be set
* @jkey: IN, the LJSON string key, allow length not to be set first by json_string_info_update
* @mem: IN, the block memory manager
* @return: -1 on failure, 0 on success
*/
static inline int pjson_set_key(json_object *json, json_string_t *jkey, json_mem_t *mem)
{
return pjson_string_strdup(jkey, &json->jkey, &mem->key_mgr);
}
/*
* pjson_set_string_value - Set the string of JSON_STRING object
* @json: IN, the json object to be set
* @jstr: IN, the LJSON string value, allow length not to be set first by json_string_info_update
* @mem: IN, the block memory manager
* @return: -1 on failure, 0 on success
*/
static inline int pjson_set_string_value(json_object *json, json_string_t *jstr, json_mem_t *mem)
{
if (json->jkey.type == JSON_STRING) {
return pjson_string_strdup(jstr, &json->value.vstr, &mem->str_mgr);
}
return -1;
}
/*
* pjson_replace_item_in_array - Replace the specified object in JSON_ARRAY object with new json object
* @array: IN, the JSON_ARRAY object
* @seq: IN, the sequence number
* @new_item: IN, the new json object
* @return: -1 on failure, 0 on success
* @description: If seq is not satisfied, new_item will be added to the end.
*/
int pjson_replace_item_in_array(json_object *array, int seq, json_object *new_item);
/*
* pjson_replace_item_in_object - Replace the specified object in JSON_OBJECT object with new json object
* @object: IN, the JSON_OBJECT object
* @new_item: IN, the new json object, users should set the key of new_item first
* @return: -1 on failure, 0 on success
* @description: If key is not satisfied, new_item will be added to the end.
*/
int pjson_replace_item_in_object(json_object *object, json_object *new_item);
/*
* pjson_deepcopy - Deep copy the json object (recursive)
* @json: IN, the source json object
* @mem: IN, the block memory manager
* @return: NULL on failure, a pointer on success
*/
json_object *pjson_deepcopy(json_object *json, json_mem_t *mem);
/*
* pjson_copy_item_to_array - Copy the specified object, the add it to the JSON_ARRAY object
* @array: IN, the JSON_ARRAY object
* @item: IN, the json object to copy and add
* @mem: IN, the block memory manager
* @return: -1 on failure, 0 on success
* @description: The item will be added to the end.
*/
int pjson_copy_item_to_array(json_object *array, json_object *item, json_mem_t *mem);
/*
* pjson_copy_item_to_object - Copy the specified object, the add it to the JSON_OBJECT object
* @object: IN, the JSON_OBJECT object
* @item: IN, the json object to copy and add, users should set the key of item first
* @mem: IN, the block memory manager
* @return: -1 on failure, 0 on success
* @description: The item will be added to the end.
*/
int pjson_copy_item_to_object(json_object *object, json_object *item, json_mem_t *mem);
/*
* pjson_add_new_item_to_array - Create a new object, the add it to the pool JSON_ARRAY object
* @object: IN, the JSON_ARRAY object
* @type: the json object type
* @value: IN, the json object value
* @mem: IN, the block memory manager
* @return: NULL on failure, a pointer on success
* @description: the pointer points to the created json object
*/
json_object *pjson_add_new_item_to_array(json_object *array, json_type_t type, void *value, json_mem_t *mem);
static inline json_object *pjson_add_null_to_array(json_object *array, json_mem_t *mem)
{
return pjson_add_new_item_to_array(array, JSON_NULL, NULL, mem);
}
static inline json_object *pjson_add_bool_to_array(json_object *array, bool value, json_mem_t *mem)
{
return pjson_add_new_item_to_array(array, JSON_BOOL, &value, mem);
}
static inline json_object *pjson_add_int_to_array(json_object *array, int32_t value, json_mem_t *mem)