-
Notifications
You must be signed in to change notification settings - Fork 3
/
json_util.c
3202 lines (2906 loc) · 85.7 KB
/
json_util.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* json_util - general JSON parser utility support functions
*
* "Because specs w/o version numbers are forced to commit to their original design flaws." :-)
*
* This JSON parser was co-developed in 2022 by:
*
* @xexyl
* https://xexyl.net Cody Boone Ferguson
* https://ioccc.xexyl.net
* and:
* chongo (Landon Curt Noll, http://www.isthe.com/chongo/index.html) /\oo/\
*
* "Because sometimes even the IOCCC Judges need some help." :-)
*
* "Share and Enjoy!"
* -- Sirius Cybernetics Corporation Complaints Division, JSON spec department. :-)
*/
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <math.h>
#include <stdarg.h>
#include <stdint.h>
#include <limits.h>
#include <ctype.h>
#include <unistd.h> /* for chdir(2) */
#include <fcntl.h> /* for open(2) */
/*
* json_parse - JSON parser support code
*/
#include "json_parse.h"
/*
* json_util - general JSON parser utility support functions
*/
#include "json_util.h"
/*
* global variables
*/
int json_verbosity_level = JSON_DBG_NONE; /* json debug level set by -J in jparse */
/*
* hexval - convert ASCII character to hex value
*
* NOTE: -1 means the ASCII character is not a valid hex character
*/
int const hexval[JSON_BYTE_VALUES] = {
/* \x00 - \x0f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \x10 - \x1f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \x20 - \x2f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \x30 - \x3f */
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,
/* \x40 - \x4f */
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \x50 - \x5f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \x60 - \x6f */
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \x70 - \x7f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \x80 - \x8f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \x90 - \x9f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \xa0 - \xaf */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \xb0 - \xbf */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \xc0 - \xcf */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \xd0 - \xdf */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \xe0 - \xef */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
/* \xf0 - \xff */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
};
/*
* static declarations
*/
static void fprnumber(FILE *stream, char *prestr, struct json_number *item, char *midstr, char *poststr);
static void fprstring(FILE *stream, struct json_string *item);
static void fprboolean(FILE *stream, struct json_boolean *item);
static void fprnull(FILE *stream, struct json_null *item);
static void fprmember(FILE *stream, struct json_member *item);
static void fprobject(FILE *stream, struct json_object *item);
static void fprarray(FILE *stream, struct json_array *item);
static void fprelements(FILE *stream, struct json_elements *item);
/*
* json_dbg_allowed - determine if verbosity level allows for JSON debug messages are allowed
*
* given:
* json_dbg_lvl write message if >= verbosity level
*
* returns:
* true ==> allowed, false ==> disabled
*/
bool
json_dbg_allowed(int json_dbg_lvl)
{
/*
* determine if verbosity level allows for JSON debug messages
*/
if (dbg_output_allowed == true &&
(json_dbg_lvl == JSON_DBG_FORCED || json_dbg_lvl <= json_verbosity_level)) {
return true;
}
return false;
}
/*
* json_warn_allowed - determine if a JSON warning message is allowed
*
* returns:
* true ==> allowed, false ==> disabled
*/
bool
json_warn_allowed(void)
{
/*
* determine if a JSON warning message is allowed
*/
if (warn_output_allowed == false || (msg_warn_silent == true && json_verbosity_level <= 0)) {
return false;
}
return true;
}
/*
* json_err_allowed - determine if fatal JSON error messages are allowed
*
* returns:
* true ==> allowed, false ==> disabled
*/
bool
json_err_allowed(void)
{
/*
* determine if fatal error messages are allowed
*/
if (err_output_allowed == false) {
return false;
}
return true;
}
/*
* json_dbg - print JSON debug message if we are verbose enough
*
* This function behaves like dbg() in determining if a JSON debug message
* is printed. To print a JSON debug message, dbg_output_allowed == true AND
* json_dbg_lvl <= json_verbosity_level or json_dbg_lvl == JSON_DBG_FORCED.
* When dbg_output_allowed == false, no debug output will be printed,
* even when json_dbg_lvl == JSON_DBG_FORCED.
*
* See also: json_dbg_tree_print().
*
* given:
* json_dbg_lvl print message if JSON_DBG_FORCED OR if <= json_verbosity_level
* name function name
* fmt printf format
* ...
*
* Example:
*
* json_dbg(1, __func__, "foobar information: %d", value);
*
* NOTE: We warn with extra newlines to help internal fault messages stand out.
* Normally one should NOT include newlines in warn messages.
*
* This function returns true if debug output is allowed; else it returns false.
*/
void
json_dbg(int json_dbg_lvl, char const *name, char const *fmt, ...)
{
va_list ap; /* variable argument list */
int saved_errno = 0; /* errno at function start */
bool allowed = false; /* assume debug output is not allowed */
/*
* determine if JSON debug messages are allowed
*/
allowed = json_dbg_allowed(json_dbg_lvl);
if (allowed == false) {
return;
}
/*
* save errno so we can restore it before returning
*/
saved_errno = errno;
/*
* stdarg variable argument list setup
*/
va_start(ap, fmt);
/*
* firewall
*/
if (name == NULL) {
name = "((NULL name))";
warn(__func__, "NULL name, forcing use of: %s", name);
}
if (fmt == NULL) {
fmt = "((NULL fmt))";
warn(__func__, " NULL fmt, forcing use of: %s", fmt);
}
/*
* print the debug message if allowed and allowed by the verbosity level
*/
json_vdbg(json_dbg_lvl, name, fmt, ap);
/*
* stdarg variable argument list clean up
*/
va_end(ap);
/*
* restore previous errno value
*/
errno = saved_errno;
return;
}
/*
* json_vdbg - print debug message if we are verbose enough
*
* This function behaves like vdbg() in determining if a JSON debug message
* is printed. To print a JSON debug message, dbg_output_allowed == true AND
* json_dbg_lvl <= json_verbosity_level or json_dbg_lvl == JSON_DBG_FORCED.
* When dbg_output_allowed == false, no debug output will be printed,
* even when json_dbg_lvl == JSON_DBG_FORCED.
*
* See also: json_dbg_tree_print().
*
* given:
* json_dbg_lvl print message if JSON_DBG_FORCED OR if <= json_verbosity_level
* name function name
* ap variable argument list
*
* Example:
*
* json_vdbg(1, __func__, "foobar information: %d", ap);
*
* NOTE: We warn with extra newlines to help internal fault messages stand out.
* Normally one should NOT include newlines in warn messages.
*
* This function returns true if debug output is allowed; else it returns false.
*/
void
json_vdbg(int json_dbg_lvl, char const *name, char const *fmt, va_list ap)
{
int saved_errno = 0; /* errno at function start */
bool allowed = false; /* assume debug message not allowed */
int ret; /* libc function return code */
/*
* determine if JSON debug messages are allowed
*/
allowed = json_dbg_allowed(json_dbg_lvl);
if (allowed == false) {
return;
}
/*
* save errno so we can restore it before returning
*/
saved_errno = errno;
/*
* firewall
*/
if (name == NULL) {
name = "((NULL name))";
warn(__func__, "NULL name, forcing use of: %s", name);
}
if (fmt == NULL) {
fmt = "((NULL fmt))";
warn(__func__, "NULL fmt, forcing use of: %s", fmt);
}
/*
* print the debug message
*/
errno = 0;
ret = fprintf(stderr, "in %s(): JSON debug[%d]: ", name, json_dbg_lvl);
if (chk_stdio_printf_err(stderr, ret)) {
warn(__func__, "fprintf returned errno: %d: (%s)", errno, strerror(errno));
}
errno = 0;
ret = vfprintf(stderr, fmt, ap);
if (chk_stdio_printf_err(stderr, ret)) {
warn(__func__, "vfprintf returned errno: %d: (%s)", errno, strerror(errno));
}
errno = 0;
ret = fputc('\n', stderr);
if (ret != '\n') {
warn(__func__, "fputc returned errno: %d: (%s)", errno, strerror(errno));
}
errno = 0;
ret = fflush(stderr);
if (ret < 0) {
warn(__func__, "fflush returned errno: %d: (%s)", errno, strerror(errno));
}
/*
* restore previous errno value
*/
errno = saved_errno;
return;
}
/*
* json_putc - print a UTF-8 character with JSON encoding
*
* given:
* stream - string to print on
* c - UTF-8 character to encode
*
* returns:
* true ==> stream print was OK,
* false ==> error printing to stream
*/
bool
json_putc(uint8_t const c, FILE *stream)
{
int ret; /* libc function return */
/*
* firewall
*/
if (stream == NULL) {
warn(__func__, "called with NULL stream");
return false;
}
/*
* write JSON encoding to stream
*
* XXX - This is NOT the canonical way to encode Unicode characters! - XXX
* XXX - Valid Unicode symbols when encoded as UTF-8 bytes should be - XXX
* XXX - encoded as 1 or more consecutive \\u[0-9A-Fa-f]{4} strings! - XXX
*/
errno = 0; /* pre-clear errno for warnp */
ret = fprintf(stream, "%s", byte2asciistr[c].enc);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf #1 error");
return false;
}
return true;
}
/*
* json_fprintf_str - print a JSON string
*
* Print on stream:
*
* If str == NULL:
*
* null
*
* else str != NULL:
*
* str with JSON string encoding surrounded by '"'s
*
* See:
*
* https://www.json.org/json-en.html
*
* given:
* stream - open file stream to print on
* str - the string to JSON encode or NULL
*
* returns:
* true ==> stream print was OK,
* false ==> error printing to stream
*
* This function does not return on error.
*/
bool
json_fprintf_str(FILE *stream, char const *str)
{
int ret; /* libc function return */
char const *p;
/*
* firewall
*/
if (stream == NULL) {
warn(__func__, "called with NULL stream");
return false;
}
/*
* case: NULL
*/
if (str == NULL) {
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "null");
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf #0 error for null");
return false;
}
return true;
}
/*
* print leading double-quote
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fputc('"', stream);
if (ret == EOF) {
warnp(__func__, "fputc #0 error for leading double-quote");
return false;
}
/*
* print name, JSON encoded
*/
for (p=str; *p != '\0'; ++p) {
if (json_putc((uint8_t const)*p, stream) != true) {
warn(__func__, "json_putc #0 error");
return false;
}
}
/*
* print trailing double-quote
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fputc('"', stream);
if (ret == EOF) {
warnp(__func__, "fputc #1 error for trailing double-quote");
return false;
}
return true;
}
/*
* json_fprintf_value_string - print name value (as a string) pair
*
* On a stream, we will print:
*
* lead "name_encoded " middle "value_encoded" tail
*
* given:
* stream - open file stream to print on
* lead - leading whitespace string to print
* name - name string to JSON encode or NULL
* middle - middle string (often " : " )l
* value - value string to JSON encode or NULL
* tail - tailing string to print (often ",\n")
*
* returns:
* true
*
* returns:
* true ==> stream print was OK,
* false ==> error printing to stream
*
* This function does not return on error.
*/
bool
json_fprintf_value_string(FILE *stream, char const *lead, char const *name, char const *middle, char const *value,
char const *tail)
{
int ret; /* libc function return */
/*
* firewall
*/
if (stream == NULL || lead == NULL || middle == NULL || tail == NULL) {
warn(__func__, "called with NULL arg(s)");
return false;
}
/*
* print leading string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", lead);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing lead");
return false;
}
/*
* print name as a JSON encoded string
*/
if (json_fprintf_str(stream, name) != true) {
warn(__func__, "json_fprintf_str error printing name");
return false;
}
/*
* print middle string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", middle);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing middle");
return false;
}
/*
* print value as a JSON encoded string
*/
if (json_fprintf_str(stream, value) != true) {
warn(__func__, "json_fprintf_str for value as a string");
return false;
}
/*
* print trailing string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", tail);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing end of line");
return false;
}
return true;
}
/*
* json_fprintf_value_long - print name value (as a long integer) pair
*
* On a stream, we will print:
*
* lead "name_encoded" middle long_value tail
*
* given:
* stream - open file stream to print on
* lead - leading whitespace string to print
* name - name string to JSON encode or NULL
* middle - middle string (often " : " )l
* value - value as long
* tail - tailing string to print (often ",\n")
*
* returns:
* true
*
* returns:
* true ==> stream print was OK,
* false ==> error printing to stream
*
* This function does not return on error.
*/
bool
json_fprintf_value_long(FILE *stream, char const *lead, char const *name, char const *middle, long value,
char const *tail)
{
int ret; /* libc function return */
/*
* firewall
*/
if (stream == NULL || lead == NULL || middle == NULL || tail == NULL) {
warn(__func__, "called with NULL arg(s)");
return false;
}
/*
* print leading string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", lead);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing lead");
return false;
}
/*
* print name as a JSON encoded string
*/
if (json_fprintf_str(stream, name) != true) {
warnp(__func__, "json_fprintf_str error printing name");
return false;
}
/*
* print middle string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", middle);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing middle");
return false;
}
/*
* print value as a JSON long
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%ld", value);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf for value as a long");
return false;
}
/*
* print trailing string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", tail);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing end of line");
return false;
}
return true;
}
/*
* json_fprintf_value_time_t - print name value (as a time_t integer) pair
*
* On a stream, we will print:
*
* lead "name_encoded" middle time_t_value tail
*
* given:
* stream - open file stream to print on
* lead - leading whitespace string to print
* name - name string to JSON encode or NULL
* middle - middle string (often " : " )l
* value - value as time_t
* tail - tailing string to print (often ",\n")
*
* returns:
* true
*
* returns:
* true ==> stream print was OK,
* false ==> error printing to stream
*
* This function does not return on error.
*/
bool
json_fprintf_value_time_t(FILE *stream, char const *lead, char const *name, char const *middle, time_t value,
char const *tail)
{
int ret; /* libc function return */
/*
* firewall
*/
if (stream == NULL || lead == NULL || middle == NULL || tail == NULL) {
warn(__func__, "called with NULL arg(s)");
return false;
}
/*
* print leading string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", lead);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing lead");
return false;
}
/*
* print name as a JSON encoded string
*/
if (json_fprintf_str(stream, name) != true) {
warnp(__func__, "json_fprintf_str error printing name");
return false;
}
/*
* print middle string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", middle);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing middle");
return false;
}
/*
* print value as a JSON time_t
*/
errno = 0; /* pre-clear errno for warnp() */
if ((time_t)-1 > 0) {
/* case: unsigned time_t */
ret = fprintf(stream, "%ju", (uintmax_t)value);
} else {
/* case: signed time_t */
ret = fprintf(stream, "%jd", (intmax_t)value);
}
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf for value as a time_t");
return false;
}
/*
* print trailing string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", tail);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing end of line");
return false;
}
return true;
}
/*
* json_fprintf_value_bool - print name value (as a boolean) pair
*
* On a stream, we will print:
*
* lead "name_encoded" middle true tail
* or:
* lead "name_encoded" middle false tail
*
* given:
* stream - open file stream to print on
* lead - leading whitespace string to print
* name - name string to JSON encode or NULL
* middle - middle string (often " : " )l
* value - value as boolean
* tail - tailing string to print (often ",\n")
*
* returns:
* true ==> stream print was OK,
* false ==> error printing to stream
*/
bool
json_fprintf_value_bool(FILE *stream, char const *lead, char const *name, char const *middle, bool value,
char const *tail)
{
int ret; /* libc function return */
/*
* firewall
*/
if (stream == NULL || lead == NULL || middle == NULL || tail == NULL) {
warn(__func__, "called with NULL arg(s)");
return false;
}
/*
* print leading string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", lead);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing lead");
return false;
}
/*
* print name as a JSON encoded string
*/
if (json_fprintf_str(stream, name) != true) {
warn(__func__, "json_fprintf_str error printing name");
return false;
}
/*
* print middle string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", middle);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing middle");
return false;
}
/*
* print value as a JSON boolean
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", booltostr(value));
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf for value as a boolean");
return false;
}
/*
* print trailing string
*/
errno = 0; /* pre-clear errno for warnp() */
ret = fprintf(stream, "%s", tail);
if (chk_stdio_printf_err(stream, ret)) {
warnp(__func__, "fprintf printing end of line");
return false;
}
return true;
}
/*
* json_type_name - return a struct json item union type name
*
* given:
* type one of the values of the JTYPE_ enum
*
* returns:
* A constant (read-only) string that names the JTYPE_ enum.
*
* NOTE: This string returned is read only: It's not allocated on the stack.
*/
char const *
json_type_name(enum item_type type)
{
char const *name = "JTYPE_UNSET";
/*
* determine type name based on type
*/
switch (type) {
case JTYPE_UNSET:
name = "JTYPE_UNSET";
break;
case JTYPE_NUMBER:
name = "JTYPE_NUMBER";
break;
case JTYPE_STRING:
name = "JTYPE_STRING";
break;
case JTYPE_BOOL:
name = "JTYPE_BOOL";
break;
case JTYPE_NULL:
name = "JTYPE_NULL";
break;
case JTYPE_MEMBER:
name = "JTYPE_MEMBER";
break;
case JTYPE_OBJECT:
name = "JTYPE_OBJECT";
break;
case JTYPE_ARRAY:
name = "JTYPE_ARRAY";
break;
case JTYPE_ELEMENTS:
name = "JTYPE_ELEMENTS";
break;
default:
name = "((JTYPE_UNKNOWN))";
warn(__func__, "called with unknown JSON type: %d", type);
break;
}
/* return read-only name constant string */
return name;
}
/*
* json_item_type_name - print a struct json item union type by name
*
* given:
* node pointer to a JSON parser tree node to get the type name
*
* returns:
* A constant (read-only) string that names the JTYPE_ enum.
*
* NOTE: This string returned is read only: It's not allocated on the stack.
*/
char const *
json_item_type_name(const struct json *node)
{
char const *name = "JTYPE_UNSET";
/*
* firewall
*/
if (node == NULL) {
name = "((node == NULL))";
/*
* determine type name based on type
*/
} else {
name = json_type_name(node->type);
}
/* return read-only name constant string */
return name;
}
/*
* json_get_type_str - print a struct json string (original match in scanner/parser)
*
* given:
* node pointer to a JSON parser tree node to get matching string from
* encoded true ==> return the encoded string, if it is a string type
*
* returns:
* A constant (read-only) string that originally matched in the
* lexer/parser
*
* NOTE: the string returned is read only; it's NOT allocated on the stack.
*
* NOTE: if encoded is true and the type is a string, then we return the encoded
* string.
*
* NOTE: this function can return a NULL pointer. It is the responsibility of
* the caller to check for a NULL return value.
*/
char const *
json_get_type_str(struct json *node, bool encoded)
{
char const *str = NULL;
/*
* firewall
*/
if (node == NULL) {
return NULL;
}
switch (node->type) {
case JTYPE_NUMBER:
{
struct json_number *item = &(node->item.number);
if (item != NULL && VALID_JSON_NODE(item)) {
str = item->as_str;
}
}
break;
case JTYPE_STRING:
{
struct json_string *item = &(node->item.string);
if (item != NULL && CONVERTED_PARSED_JSON_NODE(item)) {
str = encoded ? item->as_str : item->str;
}
}
break;
case JTYPE_BOOL:
{
struct json_boolean *item = &(node->item.boolean);
if (item != NULL && CONVERTED_PARSED_JSON_NODE(item)) {
str = item->as_str;
}
}
break;
case JTYPE_NULL:
{
struct json_null *item = &(node->item.null);
if (item != NULL && CONVERTED_PARSED_JSON_NODE(item)) {
str = item->as_str;
}
}
break;
case JTYPE_MEMBER:
{
struct json_member *item = &(node->item.member);
if (item != NULL && CONVERTED_PARSED_JSON_NODE(item)) {
str = encoded ? item->name_as_str : item->name_str;
}
}
break;
case JTYPE_OBJECT: /*fallthrough*/
case JTYPE_ARRAY: /*fallthrough*/
case JTYPE_ELEMENTS: /*fallthrough*/
default:
str = NULL;
break;
}
/* return read-only name constant string */
return str;