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