-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjgrandson.h
1414 lines (1248 loc) · 43.7 KB
/
jgrandson.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 © 2021 MetaWord Inc
// Copyright © 2019-2021 William Budd
#pragma once
#if defined(_WIN32) || defined(_WIN64)
#ifdef _MSC_VER // If any version of Visual Studio:
#define _CRT_SECURE_NO_WARNINGS // No whining about decent Posix functions!
#pragma warning(disable: 5105) // Microsoft complaining about its own winbase.h!
#pragma warning(disable: 6011) // Also, Microsoft's...
#pragma warning(disable: 6386) // static analysis game...
#pragma warning(disable: 6387) // has a number of weaknesses.
#endif
#define WIN32_LEAN_AND_MEAN // Cut down on the amount of included cruft
#include <windows.h>
#endif
//##############################################################################
//# C API ####### (See the bottom of this header for the inline C++ API) #######
//##############################################################################
#if defined (__cplusplus)
extern "C" {
#endif
#include <errno.h>
#include <inttypes.h>
#include <limits.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define JG_ELEM_C(array) (sizeof(array) / sizeof((array)[0]))
#define JG_MIN(a, b) ((a) < (b) ? (a) : (b))
#define JG_MAX(a, b) ((a) > (b) ? (a) : (b))
// Enforce an opaque pointer API for the following 5 types by keeping
// struct definitions private (jgrandson_internal.h).
typedef struct jgrandson jg_t;
typedef struct jg_arr const jg_arr_get_t;
typedef struct jg_obj const jg_obj_get_t;
typedef struct jg_val_out jg_arr_set_t;
typedef struct jg_val_out jg_obj_set_t;
enum jg_type { // The JSON types
JG_TYPE_NULL = 0,
JG_TYPE_BOOL = 1,
JG_TYPE_NUM = 2,
JG_TYPE_STR = 3,
JG_TYPE_ARR = 4,
JG_TYPE_OBJ = 5
};
// Every possible Jgrandson error condition corresponds to its own unique enum
// return value. Verbose, but unambiguous; plus it allows for a straightforward
// implementation of jg_get_err_str().
typedef enum {
JG_OK = 0,
JG_E_STATE_NOT_PARSE = 1,
JG_E_STATE_NOT_GET = 2,
JG_E_STATE_NOT_SET = 3,
JG_E_STATE_NOT_GENERATE = 4,
JG_E_SET_ROOT_ALREADY_SET = 5,
JG_E_SET_NOT_ARR = 6,
JG_E_SET_NOT_OBJ = 7,
JG_E_SET_OBJ_DUPLICATE_KEY = 8,
JG_E_MALLOC = 9,
JG_E_CALLOC = 10,
JG_E_REALLOC = 11,
JG_E_VSPRINTF = 12,
JG_E_VSNPRINTF = 13,
JG_E_NEWLOCALE = 14,
JG_E_FREAD = 15,
JG_E_FWRITE = 16,
JG_E_ERRNO_FOPEN = 17,
JG_E_ERRNO_FCLOSE = 18,
JG_E_ERRNO_FSEEKO = 19,
JG_E_ERRNO_FTELLO = 20,
JG_E_PARSE_INVALID_TYPE = 21,
JG_E_PARSE_UNTERM_STR = 22,
JG_E_PARSE_UNTERM_ARR = 23,
JG_E_PARSE_UNTERM_OBJ = 24,
JG_E_PARSE_FALSE = 25,
JG_E_PARSE_TRUE = 26,
JG_E_PARSE_NULL = 27,
JG_E_PARSE_NUM_SIGN = 28,
JG_E_PARSE_NUM_LEAD_ZERO = 29,
JG_E_PARSE_NUM_INVALID = 30,
JG_E_PARSE_NUM_MULTIPLE_POINTS = 31,
JG_E_PARSE_NUM_EXP_HEAD_INVALID = 32,
JG_E_PARSE_NUM_EXP_INVALID = 33,
JG_E_PARSE_NUM_TOO_LARGE = 34,
JG_E_PARSE_STR_UTF8_INVALID = 35,
JG_E_PARSE_STR_UNESC_CONTROL = 36,
JG_E_PARSE_STR_ESC_INVALID = 37,
JG_E_PARSE_STR_UTF16_INVALID = 38,
JG_E_PARSE_STR_UTF16_UNPAIRED_LOW = 39,
JG_E_PARSE_STR_UTF16_UNPAIRED_HIGH = 40,
JG_E_PARSE_STR_TOO_LARGE = 41,
JG_E_PARSE_ARR_INVALID_SEP = 42,
JG_E_PARSE_OBJ_INVALID_KEY = 43,
JG_E_PARSE_OBJ_KEYVAL_INVALID_SEP = 44,
JG_E_PARSE_OBJ_INVALID_SEP = 45,
JG_E_PARSE_OBJ_DUPLICATE_KEY = 46,
JG_E_PARSE_ROOT_SURPLUS = 47,
JG_E_GET_ARG_IS_NULL = 48,
JG_E_GET_NOT_NULL = 49,
JG_E_GET_NOT_BOOL = 50,
JG_E_GET_NOT_NUM = 51,
JG_E_GET_NOT_STR = 52,
JG_E_GET_NOT_ARR = 53,
JG_E_GET_NOT_OBJ = 54,
JG_E_GET_ARR_INDEX_OVER = 55,
JG_E_GET_ARR_TOO_SHORT = 56,
JG_E_GET_ARR_TOO_LONG = 57,
JG_E_GET_OBJ_KEY_NOT_FOUND = 58,
JG_E_GET_OBJ_TOO_SHORT = 59,
JG_E_GET_OBJ_TOO_LONG = 60,
JG_E_GET_STR_BYTE_C_TOO_FEW = 61,
JG_E_GET_STR_BYTE_C_TOO_MANY = 62,
JG_E_GET_STR_CHAR_C_TOO_FEW = 63,
JG_E_GET_STR_CHAR_C_TOO_MANY = 64,
JG_E_GET_NUM_NOT_INTEGER = 65,
JG_E_GET_NUM_NOT_UNSIGNED = 66,
JG_E_GET_NUM_SIGNED_TOO_SMALL = 67,
JG_E_GET_NUM_SIGNED_TOO_LARGE = 68,
JG_E_GET_NUM_UNSIGNED_TOO_SMALL = 69,
JG_E_GET_NUM_UNSIGNED_TOO_LARGE = 70,
JG_E_GET_NUM_NOT_FLO = 71,
JG_E_GET_NUM_FLOAT_OUT_OF_RANGE = 72,
JG_E_GET_NUM_DOUBLE_OUT_OF_RANGE = 73,
JG_E_GET_NUM_LONG_DOUBLE_OUT_OF_RANGE = 74
} jg_ret;
#define JG_GUARD(func_call) do { \
jg_ret ret = (func_call); \
if (ret != JG_OK) { \
return ret; \
} \
} while (0)
#if defined(__GNUC__) && !defined(__clang__) && !defined(__INTEL_COMPILER)
// There's a long-standing bug in GCC where it mistakenly emits a
// -Wmissing-field-initializers in cases where fields are initialized with
// designated initializers: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=84685
// This is annoying given that designated field initializers are a key
// ingredient to Jgrandson's flexible optional argument passing.
// As a workaround, suppress such noise for any file that includes this header:
#pragma GCC diagnostic ignored "-Wmissing-field-initializers"
#endif
//##############################################################################
//## general prototypes (jg_heap.c) ############################################
jg_t * jg_init(
void
);
void jg_free(
jg_t * jg
);
void jg_reinit(
jg_t * jg
);
//##############################################################################
//## general prototypes (jg_error.c) ###########################################
char const * jg_get_err_str(
jg_t * jg,
char const * err_mark_before,
char const * err_mark_after
);
//##############################################################################
//## jg_parse_...() prototypes (jg_parse.c) ####################################
// Copy the JSON text string to a malloc-ed char buffer, then parse.
jg_ret jg_parse_str(
jg_t * jg,
char const * json_text, // null-terminator not required
size_t byte_c // excluding null-terminator
);
// Parse the JSON text string without copying it to a malloc-ed char buffer.
jg_ret jg_parse_callerstr(
jg_t * jg,
char const * json_text, // null-terminator not required
size_t byte_c // excluding null-terminator
);
// Open file, copy contents to a malloc-ed char buffer, close file; then parse.
jg_ret jg_parse_file(
jg_t * jg,
char const * filepath
);
//##############################################################################
//## jg_[root|arr|obj]_get_...() prototypes (jg_get.c) #########################
////////////////////////////////////////////////////////////////////////////////
// jg_[root|arr|obj]_get_json_type() ///////////////////////////////////////////
jg_ret jg_root_get_json_type(
jg_t * jg,
enum jg_type * type
);
jg_ret jg_arr_get_json_type(
jg_t * jg,
jg_arr_get_t * arr,
size_t arr_i,
enum jg_type * type
);
jg_ret jg_obj_get_json_type(
jg_t * jg,
jg_obj_get_t * obj,
char const * key,
enum jg_type * type
);
////////////////////////////////////////////////////////////////////////////////
// jg_[root|arr|obj]_get_bool() ////////////////////////////////////////////////
jg_ret jg_root_get_bool(
jg_t * jg,
bool * v
);
jg_ret jg_arr_get_bool(
jg_t * jg,
jg_arr_get_t * arr,
size_t arr_i,
bool * v
);
// Take defaults directly as parameter, since bool has no other options to take.
jg_ret jg_obj_get_bool(
jg_t * jg,
jg_obj_get_t * obj,
char const * key,
bool const * defa,
bool * v
);
////////////////////////////////////////////////////////////////////////////////
// jg_[root|arr|obj]_get_null() ////////////////////////////////////////////////
// Having a "v" parameter would be pointless in the case of ..._get_null().
jg_ret jg_root_get_null(
jg_t * jg
);
jg_ret jg_arr_get_null(
jg_t * jg,
jg_arr_get_t * arr,
size_t arr_i
);
jg_ret jg_obj_get_null(
jg_t * jg,
jg_obj_get_t * obj,
char const * key
);
////////////////////////////////////////////////////////////////////////////////
// jg_[root|arr|obj]_get_arr() /////////////////////////////////////////////////
struct jg_opt_arr {
char const * min_c_reason;
char const * max_c_reason;
size_t min_c;
size_t max_c;
};
struct jg_opt_arr_defa {
char const * max_c_reason;
size_t max_c;
};
typedef struct jg_opt_arr jg_root_arr;
jg_ret jg_root_get_arr(
jg_t * jg,
jg_root_arr * opt,
jg_arr_get_t * * v,
size_t * elem_c
);
typedef struct jg_opt_arr jg_arr_arr;
jg_ret jg_arr_get_arr(
jg_t * jg,
jg_arr_get_t * arr,
size_t arr_i,
jg_arr_arr * opt,
jg_arr_get_t * * v,
size_t * elem_c
);
typedef struct jg_opt_arr jg_obj_arr;
jg_ret jg_obj_get_arr(
jg_t * jg,
jg_obj_get_t * obj,
char const * key,
jg_obj_arr * opt,
jg_arr_get_t * * v,
size_t * elem_c
);
typedef struct jg_opt_arr_defa jg_obj_arr_defa;
jg_ret jg_obj_get_arr_defa(
jg_t * jg,
jg_obj_get_t * obj,
char const * key,
jg_obj_arr_defa * opt,
jg_arr_get_t * * v,
size_t * elem_c
);
////////////////////////////////////////////////////////////////////////////////
// jg_[root|arr|obj]_get_obj() /////////////////////////////////////////////////
// All remaining getters have the same form of protottpe. Use of the following
// macros macros reduces the size of this header by a few hundred lines.
#define JG_ROOT_GET(_suf, _type) \
jg_ret jg_root_get##_suf( \
jg_t * jg, \
jg_root##_suf * opt, \
_type * v \
)
#define JG_ARR_GET(_suf, _type) \
jg_ret jg_arr_get##_suf( \
jg_t * jg, \
jg_arr_get_t * arr, \
size_t arr_i, \
jg_arr##_suf * opt, \
_type * v \
)
#define JG_OBJ_GET(_suf, _type) \
jg_ret jg_obj_get##_suf( \
jg_t * jg, \
jg_obj_get_t * obj, \
char const * key, \
jg_obj##_suf * opt, \
_type * v \
)
struct jg_opt_obj {
char * * * keys;
size_t * key_c;
char const * min_c_reason;
char const * max_c_reason;
size_t min_c;
size_t max_c;
};
struct jg_opt_obj_defa {
char * * * keys;
size_t * key_c;
char const * max_c_reason;
size_t max_c;
};
typedef struct jg_opt_obj jg_root_obj;
JG_ROOT_GET(_obj, jg_obj_get_t *);
typedef struct jg_opt_obj jg_arr_obj;
JG_ARR_GET(_obj, jg_obj_get_t *);
typedef struct jg_opt_obj jg_obj_obj;
JG_OBJ_GET(_obj, jg_obj_get_t *);
typedef struct jg_opt_obj_defa jg_obj_obj_defa;
JG_OBJ_GET(_obj_defa, jg_obj_get_t *);
////////////////////////////////////////////////////////////////////////////////
// jg_[root|arr|obj]_get_(json)_(caller)str() //////////////////////////////////
// These getters come in 2 * 2 = 4 forms:
//
// * 2 forms with regard to escape sequences (see also rfc8259):
//
// 1) jg_[root|arr|obj]_get_(caller)str():
// All escape sequences as they may appear in the JSON string in the JSON
// text are unescaped prior to copying their contents to the destination;
// except for any escaped null-terminators ("\u0000"), which are discarded.
//
// 2) jg_[root|arr|obj]_get_json_(caller)str():
// All escape sequence contained in the JSON string are left unescaped: the
// JSON string is copied to the destination as-is.
//
// * And 2 forms with regard to the destination buffer "v":
//
// 1) jg_[root|arr|obj]_get_(json)_str():
// The string is copied into a malloc()ed char buffer "v". Jgrandson will
// never free() this buffer: doing so is the responsibility of the caller.
//
// 2) jg_[root|arr|obj]_get_(json)_callerstr():
// The string is copied into a caller-supplied buffer "v". To obtain the size
// needed for this caller-supplied buffer, use the "sprintf(NULL, 0, ...)
// paradigm": call ..._get_caller_str() with a "v" of NULL and a non-NULL
// "byte_c" pointer. (Then allocate a "v" of size "byte_c (+ 1)", and call
// ..._get_callerstr() again with that non-NULL "v".)
#define JG_OPT_STR_COMMON \
size_t * byte_c; \
size_t * codepoint_c; \
char const * min_byte_c_reason; \
char const * max_byte_c_reason; \
char const * min_cp_c_reason; \
char const * max_cp_c_reason; \
size_t min_byte_c; \
size_t max_byte_c; \
size_t min_cp_c; \
size_t max_cp_c; \
bool nullify_empty_str; \
bool omit_null_terminator
struct jg_opt_str {
JG_OPT_STR_COMMON;
};
struct jg_opt_obj_str {
char const * defa;
JG_OPT_STR_COMMON;
};
#undef JG_OPT_STR_COMMON
typedef struct jg_opt_str jg_root_str;
JG_ROOT_GET(_str, char *);
typedef struct jg_opt_str jg_root_callerstr;
JG_ROOT_GET(_callerstr, char);
typedef struct jg_opt_str jg_root_json_str;
JG_ROOT_GET(_json_str, char *);
typedef struct jg_opt_str jg_root_json_callerstr;
JG_ROOT_GET(_json_callerstr, char);
typedef struct jg_opt_str jg_arr_str;
JG_ARR_GET(_str, char *);
typedef struct jg_opt_str jg_arr_callerstr;
JG_ARR_GET(_callerstr, char);
typedef struct jg_opt_str jg_arr_json_str;
JG_ARR_GET(_json_str, char *);
typedef struct jg_opt_str jg_arr_json_callerstr;
JG_ARR_GET(_json_callerstr, char);
typedef struct jg_opt_obj_str jg_obj_str;
JG_OBJ_GET(_str, char *);
typedef struct jg_opt_obj_str jg_obj_callerstr;
JG_OBJ_GET(_callerstr, char);
typedef struct jg_opt_obj_str jg_obj_json_str;
JG_OBJ_GET(_json_str, char *);
typedef struct jg_opt_obj_str jg_obj_json_callerstr;
JG_OBJ_GET(_json_callerstr, char);
////////////////////////////////////////////////////////////////////////////////
// jg_[root|arr|obj]_get_<integer_type>() //////////////////////////////////////
#define JG_GET_INT(_suf, _type) \
struct jg_opt##_suf { \
char const * min_reason; \
char const * max_reason; \
_type * min; \
_type * max; \
}; \
\
struct jg_opt_obj##_suf { \
_type const * defa; \
char const * min_reason; \
char const * max_reason; \
_type * min; \
_type * max; \
}; \
\
typedef struct jg_opt##_suf jg_root##_suf; \
JG_ROOT_GET(_suf, _type); \
\
typedef struct jg_opt##_suf jg_arr##_suf; \
JG_ARR_GET(_suf, _type); \
\
typedef struct jg_opt_obj##_suf jg_obj##_suf; \
JG_OBJ_GET(_suf, _type)
JG_GET_INT(_int8, int8_t);
JG_GET_INT(_char, char);
JG_GET_INT(_signed_char, signed char);
JG_GET_INT(_int16, int16_t);
JG_GET_INT(_short, short);
JG_GET_INT(_int32, int32_t);
JG_GET_INT(_int, int);
JG_GET_INT(_int64, int64_t);
JG_GET_INT(_long, long);
JG_GET_INT(_long_long, long long);
JG_GET_INT(_intmax, intmax_t);
JG_GET_INT(_uint8, uint8_t);
JG_GET_INT(_unsigned_char, unsigned char);
JG_GET_INT(_uint16, uint16_t);
JG_GET_INT(_unsigned_short, unsigned short);
JG_GET_INT(_uint32, uint32_t);
JG_GET_INT(_unsigned, unsigned);
JG_GET_INT(_uint64, uint64_t);
JG_GET_INT(_unsigned_long, unsigned long);
JG_GET_INT(_unsigned_long_long, unsigned long long);
JG_GET_INT(_sizet, size_t);
JG_GET_INT(_uintmax, uintmax_t);
////////////////////////////////////////////////////////////////////////////////
// jg_[root|arr|obj]_get_[float|double|long_double]() //////////////////////////
// Floating point getters may change in the future to take a jg_opt struct
// with .min and .max members, along with .rel_diff, .abs_diff and/or .epsilon,
// but I currently don't have the time/need/motivation to implement that.
// If you'd like to see this: pull requests are very welcome!
#define JG_ROOT_GET_FLO(_suf, _type) \
jg_ret jg_root_get##_suf( \
jg_t * jg, \
_type * v \
)
#define JG_ARR_GET_FLO(_suf, _type) \
jg_ret jg_arr_get##_suf( \
jg_t * jg, \
jg_arr_get_t * arr, \
size_t arr_i, \
_type * v \
)
#define JG_OBJ_GET_FLO(_suf, _type) \
jg_ret jg_obj_get##_suf( \
jg_t * jg, \
jg_obj_get_t * obj, \
char const * key, \
_type const * defa, \
_type * v \
)
#define JG_GET_FLO(_suf, _type) \
JG_ROOT_GET_FLO(_suf, _type); \
JG_ARR_GET_FLO(_suf, _type); \
JG_OBJ_GET_FLO(_suf, _type)
JG_GET_FLO(_float, float);
JG_GET_FLO(_double, double);
JG_GET_FLO(_long_double, long double);
//##############################################################################
//## jg_[root|arr|obj]_set_...() prototypes (jg_set.c) #########################
jg_ret jg_root_set_null(
jg_t * jg
);
jg_ret jg_arr_set_null(
jg_t * jg,
jg_arr_set_t * arr
);
jg_ret jg_obj_set_null(
jg_t * jg,
jg_obj_set_t * obj,
char const * key
);
#define JG_ROOT_SET(_suf, _type) \
jg_ret jg_root_set##_suf( \
jg_t * jg, \
_type v \
)
#define JG_ARR_SET(_suf, _type) \
jg_ret jg_arr_set##_suf( \
jg_t * jg, \
jg_arr_set_t * arr, \
_type v \
)
#define JG_OBJ_SET(_suf, _type) \
jg_ret jg_obj_set##_suf( \
jg_t * jg, \
jg_obj_set_t * obj, \
char const * key, \
_type v \
)
#define JG_SET(_suf, _type) \
JG_ROOT_SET(_suf, _type); \
JG_ARR_SET(_suf, _type); \
JG_OBJ_SET(_suf, _type)
JG_SET(_arr, jg_arr_set_t * *);
JG_SET(_obj, jg_obj_set_t * *);
JG_SET(_str, char const *);
JG_SET(_callerstr, char const *);
JG_SET(_bool, bool);
JG_SET(_int8, int8_t);
JG_SET(_char, char);
JG_SET(_signed_char, signed char);
JG_SET(_int16, int16_t);
JG_SET(_short, short);
JG_SET(_int32, int32_t);
JG_SET(_int, int);
JG_SET(_int64, int64_t);
JG_SET(_long, long);
JG_SET(_long_long, long long);
JG_SET(_intmax, intmax_t);
JG_SET(_uint8, uint8_t);
JG_SET(_unsigned_char, unsigned char);
JG_SET(_uint16, uint16_t);
JG_SET(_unsigned_short, unsigned short);
JG_SET(_uint32, uint32_t);
JG_SET(_unsigned, unsigned);
JG_SET(_uint64, uint64_t);
JG_SET(_unsigned_long, unsigned long);
JG_SET(_unsigned_long_long, unsigned long long);
JG_SET(_sizet, size_t);
JG_SET(_uintmax, uintmax_t);
JG_SET(_float, float);
JG_SET(_double, double);
JG_SET(_long_double, long double);
//##############################################################################
//## jg_generate_...() prototypes (jg_generate.c) ##############################
struct jg_opt_whitespace {
// The number of spaces (or tabs if indent_is_tab is true) to use for each
// indentation level. Ignored if no_whitespace is true.
size_t * indent; // Default: 2
// If true, use tab characters instead of spaces for indentation.
bool indent_is_tab; // Default: false
// If true, prepend a carriage return ('\r') to any line feed ('\n'), for
// Windows-style ("\r\n") newlines instead of Unix-style ("\n") newlines.
bool include_cr; // Default: false
// If true, don't bother including any whitespace at all (and ignore the
// options above). Does not affect the .no_newline_before_eof flag below.
bool no_whitespace; // Default: false
// Only applicable to jg_generate_file(): by default Jgrandson writes a
// final newline when saving the generated JSON as a text file -- as
// is expected of all text files in the Unix world. Set this to true to
// overrule that default.
bool no_newline_before_eof; // jg_generate_file() default: false
};
typedef struct jg_opt_whitespace jg_opt_whitespace;
jg_ret jg_generate_str(
jg_t * jg,
jg_opt_whitespace * opt,
char * * json_text,
size_t * byte_c
);
jg_ret jg_generate_callerstr(
jg_t * jg,
jg_opt_whitespace * opt,
char * json_text,
size_t * byte_c
);
jg_ret jg_generate_file(
jg_t * jg,
jg_opt_whitespace * opt,
char const * filepath
);
#if defined (__cplusplus)
} // End of extern "C"
//##############################################################################
//# C++ API ####################################################################
//##############################################################################
#include <concepts>
#include <filesystem>
#include <functional>
#include <fstream>
#include <memory>
#include <span>
#include <stdexcept>
#include <string>
#include <vector>
using namespace std::placeholders;
#ifdef _MSC_VER // If any version of Visual Studio
#pragma warning(disable: 26812) // Ignore complaints about unscoped enum jg_ret
#endif
namespace jg {
struct Err : public std::runtime_error {
Err(std::string const & what = std::string()) : std::runtime_error(what) {}
};
class ErrMemory : public Err { using Err::Err; };
class ErrState : public Err { using Err::Err; };
class ErrFile : public Err { using Err::Err; };
class ErrParse : public Err { using Err::Err; };
class ErrArg : public Err { using Err::Err; };
class ErrSet : public Err { using Err::Err; };
class ErrGetRange : public Err { using Err::Err; };
class ErrGetKey : public Err { using Err::Err; };
class ErrGetType : public Err { using Err::Err; };
class ErrGetStr : public Err { using Err::Err; };
class ErrGetNum : public Err { using Err::Err; };
struct _Session {
inline _Session() {
jg = jg_init();
if (!jg) {
throw ErrMemory(
"jg_init() returned a NULL pointer: out of memory?");
}
}
inline ~_Session() noexcept {
if (jg) {
jg_free(jg);
}
}
jg_t * jg{};
};
class Base {
protected:
void guard(jg_ret ret) const {
if (ret == JG_OK) {
return;
}
auto str = jg_get_err_str(_s->jg, nullptr, nullptr);
switch (ret) {
case JG_E_STATE_NOT_PARSE: case JG_E_STATE_NOT_GET:
case JG_E_STATE_NOT_SET: case JG_E_STATE_NOT_GENERATE:
throw ErrState(str);
case JG_E_SET_ROOT_ALREADY_SET: case JG_E_SET_NOT_ARR:
case JG_E_SET_NOT_OBJ: case JG_E_SET_OBJ_DUPLICATE_KEY:
throw ErrSet(str);
case JG_E_MALLOC: case JG_E_CALLOC: case JG_E_REALLOC:
case JG_E_VSPRINTF: case JG_E_VSNPRINTF: case JG_E_NEWLOCALE:
throw ErrMemory(str);
case JG_E_FREAD: case JG_E_FWRITE: case JG_E_ERRNO_FOPEN:
case JG_E_ERRNO_FCLOSE: case JG_E_ERRNO_FSEEKO: case JG_E_ERRNO_FTELLO:
throw ErrFile(str);
case JG_E_PARSE_INVALID_TYPE: case JG_E_PARSE_UNTERM_STR:
case JG_E_PARSE_UNTERM_ARR: case JG_E_PARSE_UNTERM_OBJ:
case JG_E_PARSE_FALSE: case JG_E_PARSE_TRUE: case JG_E_PARSE_NULL:
case JG_E_PARSE_NUM_SIGN: case JG_E_PARSE_NUM_LEAD_ZERO:
case JG_E_PARSE_NUM_INVALID: case JG_E_PARSE_NUM_MULTIPLE_POINTS:
case JG_E_PARSE_NUM_EXP_HEAD_INVALID: case JG_E_PARSE_NUM_EXP_INVALID:
case JG_E_PARSE_NUM_TOO_LARGE: case JG_E_PARSE_STR_UTF8_INVALID:
case JG_E_PARSE_STR_UNESC_CONTROL: case JG_E_PARSE_STR_ESC_INVALID:
case JG_E_PARSE_STR_UTF16_INVALID:
case JG_E_PARSE_STR_UTF16_UNPAIRED_LOW:
case JG_E_PARSE_STR_UTF16_UNPAIRED_HIGH: case JG_E_PARSE_STR_TOO_LARGE:
case JG_E_PARSE_ARR_INVALID_SEP: case JG_E_PARSE_OBJ_INVALID_KEY:
case JG_E_PARSE_OBJ_KEYVAL_INVALID_SEP: case JG_E_PARSE_OBJ_INVALID_SEP:
case JG_E_PARSE_OBJ_DUPLICATE_KEY: case JG_E_PARSE_ROOT_SURPLUS:
throw ErrParse(str);
case JG_E_GET_ARG_IS_NULL:
throw ErrArg(str);
case JG_E_GET_NOT_NULL: case JG_E_GET_NOT_BOOL: case JG_E_GET_NOT_NUM:
case JG_E_GET_NOT_STR: case JG_E_GET_NOT_ARR: case JG_E_GET_NOT_OBJ:
throw ErrGetType(str);
case JG_E_GET_ARR_INDEX_OVER: case JG_E_GET_ARR_TOO_SHORT:
case JG_E_GET_ARR_TOO_LONG: case JG_E_GET_OBJ_TOO_SHORT:
case JG_E_GET_OBJ_TOO_LONG:
throw ErrGetRange(str);
case JG_E_GET_OBJ_KEY_NOT_FOUND:
throw ErrGetKey(str);
case JG_E_GET_STR_BYTE_C_TOO_FEW: case JG_E_GET_STR_BYTE_C_TOO_MANY:
case JG_E_GET_STR_CHAR_C_TOO_FEW: case JG_E_GET_STR_CHAR_C_TOO_MANY:
throw ErrGetStr(str);
case JG_E_GET_NUM_NOT_INTEGER: case JG_E_GET_NUM_NOT_UNSIGNED:
case JG_E_GET_NUM_SIGNED_TOO_SMALL: case JG_E_GET_NUM_SIGNED_TOO_LARGE:
case JG_E_GET_NUM_UNSIGNED_TOO_SMALL:
case JG_E_GET_NUM_UNSIGNED_TOO_LARGE: case JG_E_GET_NUM_NOT_FLO:
case JG_E_GET_NUM_FLOAT_OUT_OF_RANGE:
case JG_E_GET_NUM_DOUBLE_OUT_OF_RANGE:
case JG_E_GET_NUM_LONG_DOUBLE_OUT_OF_RANGE:
throw ErrGetNum(str);
default:
throw Err("Unrecognized jg_ret value " + std::to_string(ret) +
": " + str);
}
}
template <typename Functor> inline auto get_arr(
Functor functor,
size_t min_c = 0,
size_t max_c = 0,
std::string const & min_c_reason = std::string(),
std::string const & max_c_reason = std::string()
) const;
template <typename Functor> inline auto get_obj(
Functor functor,
size_t min_c = 0,
size_t max_c = 0,
std::string const & min_c_reason = std::string(),
std::string const & max_c_reason = std::string()
) const;
template <typename Functor, typename Opt> inline auto get_str(
Functor functor,
Opt && opt,
size_t min_byte_c = 0,
size_t max_byte_c = 0,
size_t min_cp_c = 0,
size_t max_cp_c = 0,
// Cannot use std::string_view: null-terminated .c_str() needed
std::string const & min_byte_c_reason = std::string(),
std::string const & max_byte_c_reason = std::string(),
std::string const & min_cp_c_reason = std::string(),
std::string const & max_cp_c_reason = std::string(),
size_t * codepoint_c = nullptr
) const {
size_t byte_c{};
opt.byte_c = &byte_c;
opt.codepoint_c = codepoint_c;
opt.min_byte_c_reason =
min_byte_c_reason.empty() ? nullptr : min_byte_c_reason.c_str();
opt.max_byte_c_reason =
max_byte_c_reason.empty() ? nullptr : max_byte_c_reason.c_str();
opt.min_cp_c_reason =
min_cp_c_reason.empty() ? nullptr : min_cp_c_reason.c_str();
opt.max_cp_c_reason =
max_cp_c_reason.empty() ? nullptr : max_cp_c_reason.c_str();
opt.min_byte_c = min_byte_c;
opt.max_byte_c = max_byte_c;
opt.min_cp_c = min_cp_c;
opt.max_cp_c = max_cp_c;
opt.omit_null_terminator = true;
guard(functor(&opt, nullptr));
std::string str(byte_c, ' ');
guard(functor(&opt, &str[0]));
return str;
}
std::shared_ptr<jg::_Session const> _s{};
};
#undef _
#define _JGP_ARGS(...) __VA_ARGS__
#define _JGP_ARGS_MINMAX_NDEFA(_type, ...) __VA_ARGS__
#define _JGP_ARGS_MINMAX_DEFA(_type, ...) _type defa, __VA_ARGS__
#define _JGP_ARGS_MINMAX(_args, _type, _type_min, _type_max) \
_##_args(_type, \
_type min = _type_min, \
_type max = _type_max, \
/* Cannot use std::string_view: null-terminated .c_str() needed */ \
std::string const & min_reason = std::string(), \
std::string const & max_reason = std::string() \
)
#define _JGP_OPT
#define _JGP_OPT_MINMAX_NDEFA(_opt_t, _defa_ptr, ...) \
_opt_t opt { __VA_ARGS__ }
#define _JGP_OPT_MINMAX_DEFA(_opt_t, _defa_ptr, ...) \
_opt_t opt { _defa_ptr, __VA_ARGS__ }
#define _JGP_OPT_MINMAX(_opt, _opt_t, _defa_ptr, _type, _type_min, _type_max) \
_##_opt(_opt_t, _defa_ptr, \
min_reason.empty() ? nullptr : min_reason.c_str(), \
max_reason.empty() ? nullptr : max_reason.c_str(), \
min == _type_min ? nullptr : &min, \
max == _type_max ? nullptr : &max \
)
#define JGP_GET(_suf, _type, _args, _opt, _func) \
inline auto get##_suf(_##_args) const { \
_##_opt; \
_type v{}; \
guard(_func); \
return v; \
}
#define _JGP_GET_ROOT(_suf, _type) \
JGP_GET(_suf, _type, JGP_ARGS(), JGP_OPT, \
jg_root_get##_suf(_s->jg, &v) \
)
#define _JGP_GET_ARR(_suf, _type) \
JGP_GET(_suf, _type, JGP_ARGS(), JGP_OPT, \
jg_arr_get##_suf(_s->jg, _arr, _i, &v) \
)
#define _JGP_GET_OBJ(_suf, _type) \
JGP_GET(_suf, _type, JGP_ARGS(), JGP_OPT, \
jg_obj_get##_suf(_s->jg, _obj, _k, &v) \
)
#define _JGP_GET_OBJ_NDEFA(_suf, _type) \
JGP_GET(_suf, _type, JGP_ARGS(), JGP_OPT, \
jg_obj_get##_suf(_s->jg, _obj, _k, nullptr, &v) \
)
#define _JGP_GET_OBJ_DEFA(_suf, _type) \
JGP_GET(_suf##_defa, _type, JGP_ARGS(_type defa), JGP_OPT, \
jg_obj_get##_suf(_s->jg, _obj, _k, &defa, &v) \
)
#define _JGP_GET_ROOT_MINMAX(_suf, _type, _type_min, _type_max) \
JGP_GET(_suf, _type, \
JGP_ARGS_MINMAX(JGP_ARGS_MINMAX_NDEFA, _type, _type_min, _type_max), \
JGP_OPT_MINMAX(JGP_OPT_MINMAX_NDEFA, jg_opt##_suf, nullptr, _type, \
_type_min, _type_max), \
jg_root_get##_suf(_s->jg, &opt, &v) \
)
#define _JGP_GET_ARR_MINMAX(_suf, _type, _type_min, _type_max) \
JGP_GET(_suf, _type, \
JGP_ARGS_MINMAX(JGP_ARGS_MINMAX_NDEFA, _type, _type_min, _type_max), \
JGP_OPT_MINMAX(JGP_OPT_MINMAX_NDEFA, jg_opt##_suf, nullptr, _type, \
_type_min, _type_max), \
jg_arr_get##_suf(_s->jg, _arr, _i, &opt, &v) \
)
#define _JGP_GET_OBJ_MINMAX_NDEFA(_suf, _type, _type_min, _type_max) \
JGP_GET(_suf, _type, \
JGP_ARGS_MINMAX(JGP_ARGS_MINMAX_NDEFA, _type, _type_min, _type_max), \
JGP_OPT_MINMAX(JGP_OPT_MINMAX_DEFA, jg_opt_obj##_suf, nullptr, _type, \
_type_min, _type_max), \
jg_obj_get##_suf(_s->jg, _obj, _k, &opt, &v) \
)
#define _JGP_GET_OBJ_MINMAX_DEFA(_suf, _type, _type_min, _type_max) \
JGP_GET(_suf##_defa, _type, \
JGP_ARGS_MINMAX(JGP_ARGS_MINMAX_DEFA, _type, _type_min, _type_max), \
JGP_OPT_MINMAX(JGP_OPT_MINMAX_DEFA, jg_opt_obj##_suf, &defa, _type, \
_type_min, _type_max), \
jg_obj_get##_suf(_s->jg, _obj, _k, &opt, &v) \
)
#define JGP_GET_JSON_TYPE(_get) _##_get(_json_type, jg_type)
#define JGP_GET_BOOL(_get) _##_get(_bool, bool)
#define JGP_GET_INT(_get_minmax) \
_##_get_minmax(_int8, int8_t, INT8_MIN, INT8_MAX); \
_##_get_minmax(_char, char, CHAR_MIN, CHAR_MAX); \
_##_get_minmax(_signed_char, signed char, INT8_MIN, INT8_MAX); \
_##_get_minmax(_int16, int16_t, INT16_MIN, INT16_MAX); \
_##_get_minmax(_short, short, SHRT_MIN, SHRT_MAX); \
_##_get_minmax(_int32, int32_t, INT32_MIN, INT32_MAX); \
_##_get_minmax(_int, int, INT_MIN, INT_MAX); \
_##_get_minmax(_int64, int64_t, INT64_MIN, INT64_MAX); \
_##_get_minmax(_long, long, LONG_MIN, LONG_MAX); \
_##_get_minmax(_long_long, long long, LLONG_MIN, LLONG_MAX); \
_##_get_minmax(_intmax, intmax_t, INTMAX_MIN, INTMAX_MAX); \
_##_get_minmax(_uint8, uint8_t, 0, UINT8_MAX); \
_##_get_minmax(_unsigned_char, unsigned char, 0, UINT8_MAX); \
_##_get_minmax(_uint16, uint16_t, 0, UINT16_MAX); \
_##_get_minmax(_unsigned_short, unsigned short, 0, USHRT_MAX); \
_##_get_minmax(_uint32, uint32_t, 0, UINT32_MAX); \
_##_get_minmax(_unsigned, unsigned, 0, UINT_MAX); \
_##_get_minmax(_uint64, uint64_t, 0, UINT64_MAX); \
_##_get_minmax(_unsigned_long, unsigned long, 0, LONG_MAX); \
_##_get_minmax(_unsigned_long_long, unsigned long long, 0, ULLONG_MAX); \
_##_get_minmax(_sizet, size_t, 0, SIZE_MAX); \
_##_get_minmax(_uintmax, uintmax_t, 0, UINTMAX_MAX)
#define JGP_GET_FLO(_get) \
_##_get(_float, float); \
_##_get(_double, double); \
_##_get(_long_double, long double)
struct Root : protected Base {
inline Root() {
_s = std::make_shared<jg::_Session const>();
}
inline void parse_str(
std::string const & json_text
) const {
guard(jg_parse_str(_s->jg, json_text.c_str(), json_text.size()));
}
template <typename Type>