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 #ifndef UCX_JSON_H
37 #define UCX_JSON_H
38
39 #include "common.h"
40 #include "allocator.h"
41 #include "string.h"
42 #include "buffer.h"
43 #include "array_list.h"
44 #include "map.h"
45
46 #include <string.h>
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52
53
54
55
56 enum cx_json_token_type {
57
58
59
60 CX_JSON_NO_TOKEN,
61
62
63
64 CX_JSON_TOKEN_ERROR,
65
66
67
68 CX_JSON_TOKEN_BEGIN_ARRAY,
69
70
71
72 CX_JSON_TOKEN_BEGIN_OBJECT,
73
74
75
76 CX_JSON_TOKEN_END_ARRAY,
77
78
79
80 CX_JSON_TOKEN_END_OBJECT,
81
82
83
84 CX_JSON_TOKEN_NAME_SEPARATOR,
85
86
87
88 CX_JSON_TOKEN_VALUE_SEPARATOR,
89
90
91
92 CX_JSON_TOKEN_STRING,
93
94
95
96 CX_JSON_TOKEN_INTEGER,
97
98
99
100 CX_JSON_TOKEN_NUMBER,
101
102
103
104 CX_JSON_TOKEN_LITERAL,
105
106
107
108 CX_JSON_TOKEN_SPACE
109 };
110
111
112
113
114 enum cx_json_value_type {
115
116
117
118 CX_JSON_NOTHING,
119
120
121
122 CX_JSON_OBJECT,
123
124
125
126 CX_JSON_ARRAY,
127
128
129
130 CX_JSON_STRING,
131
132
133
134 CX_JSON_INTEGER,
135
136
137
138 CX_JSON_NUMBER,
139
140
141
142 CX_JSON_LITERAL
143 };
144
145
146
147
148 enum cx_json_literal {
149
150
151
152 CX_JSON_NULL,
153
154
155
156 CX_JSON_TRUE,
157
158
159
160 CX_JSON_FALSE
161 };
162
163
164
165
166 typedef enum cx_json_token_type CxJsonTokenType;
167
168
169
170 typedef enum cx_json_value_type CxJsonValueType;
171
172
173
174
175 typedef struct cx_json_s CxJson;
176
177
178
179
180 typedef struct cx_json_token_s CxJsonToken;
181
182
183
184
185 typedef struct cx_json_value_s CxJsonValue;
186
187
188
189
190 typedef struct cx_json_array_s CxJsonArray;
191
192
193
194
195 typedef CxMap* CxJsonObject;
196
197
198
199 typedef struct cx_mutstr_s CxJsonString;
200
201
202
203 typedef int64_t CxJsonInteger;
204
205
206
207 typedef double CxJsonNumber;
208
209
210
211 typedef enum cx_json_literal CxJsonLiteral;
212
213
214
215
216 struct cx_json_array_s {
217
218
219
220 CX_ARRAY_DECLARE(CxJsonValue*, data);
221 };
222
223
224
225
226 struct cx_json_value_s {
227
228
229
230
231
232 const CxAllocator *allocator;
233
234
235
236
237
238 CxJsonValueType type;
239
240
241
242 union {
243
244
245
246 CxJsonArray array;
247
248
249
250 CxJsonObject object;
251
252
253
254 CxJsonString string;
255
256
257
258 CxJsonInteger integer;
259
260
261
262 CxJsonNumber number;
263
264
265
266 CxJsonLiteral literal;
267 };
268 };
269
270
271
272
273
274
275 struct cx_json_token_s {
276
277
278
279 CxJsonTokenType tokentype;
280
281
282
283 bool allocated;
284
285
286
287
288
289
290 cxmutstr content;
291 };
292
293
294
295
296 struct cx_json_s {
297
298
299
300 const CxAllocator *allocator;
301
302
303
304 CxBuffer buffer;
305
306
307
308
309
310
311 CxJsonToken uncompleted;
312
313
314
315
316
317
318 CxJsonValue *parsed;
319
320
321
322
323
324
325 cxmutstr uncompleted_member_name;
326
327
328
329
330 CX_ARRAY_DECLARE_SIZED(
int, states,
unsigned);
331
332
333
334
335 CX_ARRAY_DECLARE_SIZED(CxJsonValue*, vbuf,
unsigned);
336
337
338
339
340 int states_internal[
8];
341
342
343
344
345 CxJsonValue* vbuf_internal[
8];
346 };
347
348
349
350
351 enum cx_json_status {
352
353
354
355 CX_JSON_NO_ERROR,
356
357
358
359 CX_JSON_NO_DATA,
360
361
362
363
364
365 CX_JSON_INCOMPLETE_DATA,
366
367
368
369
370
371
372
373
374 CX_JSON_OK,
375
376
377
378 CX_JSON_NULL_DATA,
379
380
381
382 CX_JSON_BUFFER_ALLOC_FAILED,
383
384
385
386 CX_JSON_VALUE_ALLOC_FAILED,
387
388
389
390 CX_JSON_FORMAT_ERROR_NUMBER,
391
392
393
394 CX_JSON_FORMAT_ERROR_UNEXPECTED_TOKEN
395 };
396
397
398
399
400 typedef enum cx_json_status CxJsonStatus;
401
402
403
404
405 struct cx_json_writer_s {
406
407
408
409 bool pretty;
410
411
412
413
414
415 uint8_t frac_max_digits;
416
417
418
419
420 bool indent_space;
421
422
423
424
425 uint8_t indent;
426
427
428
429 bool escape_slash;
430 };
431
432
433
434
435 typedef struct cx_json_writer_s CxJsonWriter;
436
437
438
439
440
441
442 cx_attr_nodiscard
443 CX_EXPORT CxJsonWriter cxJsonWriterCompact(
void);
444
445
446
447
448
449
450
451 cx_attr_nodiscard
452 CX_EXPORT CxJsonWriter cxJsonWriterPretty(bool use_spaces);
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472 cx_attr_nonnull_arg(
1,
2,
3)
473 CX_EXPORT int cxJsonWrite(
void* target,
const CxJsonValue* value,
474 cx_write_func wfunc,
const CxJsonWriter* settings);
475
476
477
478
479
480
481
482
483 cx_attr_nonnull_arg(
1)
484 CX_EXPORT void cxJsonInit(CxJson *json,
const CxAllocator *allocator);
485
486
487
488
489
490
491
492 cx_attr_nonnull
493 CX_EXPORT void cxJsonDestroy(CxJson *json);
494
495
496
497
498
499
500
501
502
503 cx_attr_nonnull
504 CX_EXPORT void cxJsonReset(CxJson *json);
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524 cx_attr_nonnull_arg(
1) cx_attr_access_r(
2,
3)
525 CX_EXPORT int cxJsonFilln(CxJson *json,
const char *buf,
size_t len);
526
527
528
529
530
531
532
533
534
535
536 cx_attr_nonnull
537 CX_INLINE int cx_json_fill(CxJson *json, cxstring str) {
538 return cxJsonFilln(json, str.ptr, str.length);
539 }
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558 #define cxJsonFill(json, str) cx_json_fill(json, cx_strcast(str))
559
560
561
562
563
564
565
566
567
568
569 cx_attr_nonnull_arg(
3)
570 CX_EXPORT CxJsonStatus cx_json_from_string(
const CxAllocator *allocator,
571 cxstring str, CxJsonValue **value);
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587 #define cxJsonFromString(allocator, str, value) \
588 cx_json_from_string(allocator, cx_strcast(str), value)
589
590
591
592
593
594
595
596
597
598 cx_attr_nodiscard
599 CX_EXPORT CxJsonValue* cxJsonCreateObj(
const CxAllocator* allocator);
600
601
602
603
604
605
606
607
608
609 cx_attr_nodiscard
610 CX_EXPORT CxJsonValue* cxJsonCreateArr(
const CxAllocator* allocator);
611
612
613
614
615
616
617
618
619
620
621 cx_attr_nodiscard
622 CX_EXPORT CxJsonValue* cxJsonCreateNumber(
const CxAllocator* allocator,
double num);
623
624
625
626
627
628
629
630
631
632
633 cx_attr_nodiscard
634 CX_EXPORT CxJsonValue* cxJsonCreateInteger(
const CxAllocator* allocator,
int64_t num);
635
636
637
638
639
640
641
642
643
644
645
646
647 cx_attr_nodiscard
648 CX_EXPORT CxJsonValue* cx_json_create_string(
const CxAllocator* allocator, cxstring str);
649
650
651
652
653
654
655
656
657
658
659 #define cxJsonCreateString(allocator, str) cx_json_create_string(allocator, cx_strcast(str))
660
661
662
663
664
665
666
667
668
669
670 cx_attr_nodiscard
671 CX_EXPORT CxJsonValue* cxJsonCreateLiteral(
const CxAllocator* allocator, CxJsonLiteral lit);
672
673
674
675
676
677
678
679
680
681
682 cx_attr_nonnull cx_attr_access_r(
2,
3)
683 CX_EXPORT int cxJsonArrAddNumbers(CxJsonValue* arr,
const double* num,
size_t count);
684
685
686
687
688
689
690
691
692
693
694 cx_attr_nonnull cx_attr_access_r(
2,
3)
695 CX_EXPORT int cxJsonArrAddIntegers(CxJsonValue* arr,
const int64_t* num,
size_t count);
696
697
698
699
700
701
702
703
704
705
706
707
708
709 cx_attr_nonnull cx_attr_access_r(
2,
3)
710 CX_EXPORT int cxJsonArrAddStrings(CxJsonValue* arr,
const char*
const* str,
size_t count);
711
712
713
714
715
716
717
718
719
720
721
722
723
724 cx_attr_nonnull cx_attr_access_r(
2,
3)
725 CX_EXPORT int cxJsonArrAddCxStrings(CxJsonValue* arr,
const cxstring* str,
size_t count);
726
727
728
729
730
731
732
733
734
735
736 cx_attr_nonnull cx_attr_access_r(
2,
3)
737 CX_EXPORT int cxJsonArrAddLiterals(CxJsonValue* arr,
const CxJsonLiteral* lit,
size_t count);
738
739
740
741
742
743
744
745
746
747
748
749
750
751 cx_attr_nonnull cx_attr_access_r(
2,
3)
752 CX_EXPORT int cxJsonArrAddValues(CxJsonValue* arr, CxJsonValue*
const* val,
size_t count);
753
754
755
756
757
758
759
760
761
762
763
764
765 cx_attr_nonnull
766 CX_EXPORT int cx_json_obj_put(CxJsonValue* obj, cxstring name, CxJsonValue* child);
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782 #define cxJsonObjPut(obj, name, child) cx_json_obj_put(obj, cx_strcast(name), child)
783
784
785
786
787
788
789
790
791
792
793
794
795 cx_attr_nonnull
796 CX_EXPORT CxJsonValue* cx_json_obj_put_obj(CxJsonValue* obj, cxstring name);
797
798
799
800
801
802
803
804
805
806
807 #define cxJsonObjPutObj(obj, name) cx_json_obj_put_obj(obj, cx_strcast(name))
808
809
810
811
812
813
814
815
816
817
818
819
820 cx_attr_nonnull
821 CX_EXPORT CxJsonValue* cx_json_obj_put_arr(CxJsonValue* obj, cxstring name);
822
823
824
825
826
827
828
829
830
831
832 #define cxJsonObjPutArr(obj, name) cx_json_obj_put_arr(obj, cx_strcast(name))
833
834
835
836
837
838
839
840
841
842
843
844
845
846 cx_attr_nonnull
847 CX_EXPORT CxJsonValue* cx_json_obj_put_number(CxJsonValue* obj, cxstring name,
double num);
848
849
850
851
852
853
854
855
856
857
858
859 #define cxJsonObjPutNumber(obj, name, num) cx_json_obj_put_number(obj, cx_strcast(name), num)
860
861
862
863
864
865
866
867
868
869
870
871
872
873 cx_attr_nonnull
874 CX_EXPORT CxJsonValue* cx_json_obj_put_integer(CxJsonValue* obj, cxstring name,
int64_t num);
875
876
877
878
879
880
881
882
883
884
885
886 #define cxJsonObjPutInteger(obj, name, num) cx_json_obj_put_integer(obj, cx_strcast(name), num)
887
888
889
890
891
892
893
894
895
896
897
898
899
900 cx_attr_nonnull
901 CX_EXPORT CxJsonValue* cx_json_obj_put_string(CxJsonValue* obj, cxstring name, cxstring str);
902
903
904
905
906
907
908
909
910
911
912
913
914
915 #define cxJsonObjPutString(obj, name, str) cx_json_obj_put_string(obj, cx_strcast(name), cx_strcast(str))
916
917
918
919
920
921
922
923
924
925
926
927
928
929 cx_attr_nonnull
930 CX_EXPORT CxJsonValue* cx_json_obj_put_literal(CxJsonValue* obj, cxstring name, CxJsonLiteral lit);
931
932
933
934
935
936
937
938
939
940
941
942 #define cxJsonObjPutLiteral(obj, name, lit) cx_json_obj_put_literal(obj, cx_strcast(name), lit)
943
944
945
946
947
948
949
950
951
952
953
954
955 CX_EXPORT void cxJsonValueFree(CxJsonValue *value);
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979 cx_attr_nonnull cx_attr_access_w(
2)
980 CX_EXPORT CxJsonStatus cxJsonNext(CxJson *json, CxJsonValue **value);
981
982
983
984
985
986
987
988
989 cx_attr_nonnull
990 CX_INLINE bool cxJsonIsObject(
const CxJsonValue *value) {
991 return value->type ==
CX_JSON_OBJECT;
992 }
993
994
995
996
997
998
999
1000
1001 cx_attr_nonnull
1002 CX_INLINE bool cxJsonIsArray(
const CxJsonValue *value) {
1003 return value->type ==
CX_JSON_ARRAY;
1004 }
1005
1006
1007
1008
1009
1010
1011
1012
1013 cx_attr_nonnull
1014 CX_INLINE bool cxJsonIsString(
const CxJsonValue *value) {
1015 return value->type ==
CX_JSON_STRING;
1016 }
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029 cx_attr_nonnull
1030 CX_INLINE bool cxJsonIsNumber(
const CxJsonValue *value) {
1031 return value->type ==
CX_JSON_NUMBER || value->type ==
CX_JSON_INTEGER;
1032 }
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042 cx_attr_nonnull
1043 CX_INLINE bool cxJsonIsInteger(
const CxJsonValue *value) {
1044 return value->type ==
CX_JSON_INTEGER;
1045 }
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059 cx_attr_nonnull
1060 CX_INLINE bool cxJsonIsLiteral(
const CxJsonValue *value) {
1061 return value->type ==
CX_JSON_LITERAL;
1062 }
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073 cx_attr_nonnull
1074 CX_INLINE bool cxJsonIsBool(
const CxJsonValue *value) {
1075 return cxJsonIsLiteral(value) && value->literal !=
CX_JSON_NULL;
1076 }
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090 cx_attr_nonnull
1091 CX_INLINE bool cxJsonIsTrue(
const CxJsonValue *value) {
1092 return cxJsonIsLiteral(value) && value->literal ==
CX_JSON_TRUE;
1093 }
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107 cx_attr_nonnull
1108 CX_INLINE bool cxJsonIsFalse(
const CxJsonValue *value) {
1109 return cxJsonIsLiteral(value) && value->literal ==
CX_JSON_FALSE;
1110 }
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120 cx_attr_nonnull
1121 CX_INLINE bool cxJsonIsNull(
const CxJsonValue *value) {
1122 return cxJsonIsLiteral(value) && value->literal ==
CX_JSON_NULL;
1123 }
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134 cx_attr_nonnull cx_attr_returns_nonnull
1135 CX_EXPORT char *cxJsonAsString(
const CxJsonValue *value);
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146 cx_attr_nonnull
1147 CX_EXPORT cxstring cxJsonAsCxString(
const CxJsonValue *value);
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158 cx_attr_nonnull
1159 CX_EXPORT cxmutstr cxJsonAsCxMutStr(
const CxJsonValue *value);
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170 cx_attr_nonnull
1171 CX_EXPORT double cxJsonAsDouble(
const CxJsonValue *value);
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185 cx_attr_nonnull
1186 CX_EXPORT int64_t cxJsonAsInteger(
const CxJsonValue *value);
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198 cx_attr_nonnull
1199 CX_INLINE bool cxJsonAsBool(
const CxJsonValue *value) {
1200 return value->literal ==
CX_JSON_TRUE;
1201 }
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212 cx_attr_nonnull
1213 CX_INLINE size_t cxJsonArrSize(
const CxJsonValue *value) {
1214 return value->array.data_size;
1215 }
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231 cx_attr_nonnull cx_attr_returns_nonnull
1232 CX_EXPORT CxJsonValue *cxJsonArrGet(
const CxJsonValue *value,
size_t index);
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247 cx_attr_nonnull
1248 CX_EXPORT CxJsonValue *cxJsonArrRemove(CxJsonValue *value,
size_t index);
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261 cx_attr_nonnull cx_attr_nodiscard
1262 CX_EXPORT CxIterator cxJsonArrIter(
const CxJsonValue *value);
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273 cx_attr_nonnull
1274 CX_INLINE size_t cxJsonObjSize(
const CxJsonValue *value) {
1275 return cxCollectionSize(value->object);
1276 }
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290 cx_attr_nonnull cx_attr_nodiscard
1291 CX_EXPORT CxMapIterator cxJsonObjIter(
const CxJsonValue *value);
1292
1293
1294
1295
1296
1297
1298
1299 cx_attr_nonnull cx_attr_returns_nonnull
1300 CX_EXPORT CxJsonValue *cx_json_obj_get(
const CxJsonValue *value, cxstring name);
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316 #define cxJsonObjGet(value, name) cx_json_obj_get(value, cx_strcast(name))
1317
1318
1319
1320
1321
1322
1323
1324 cx_attr_nonnull
1325 CX_EXPORT CxJsonValue *cx_json_obj_remove(CxJsonValue *value, cxstring name);
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340 #define cxJsonObjRemove(value, name) cx_json_obj_remove(value, cx_strcast(name))
1341
1342 #ifdef __cplusplus
1343 }
1344 #endif
1345
1346 #endif
1347
1348