FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
json.cpp/json.cpp at main · jart/json.cpp · GitHub
jart
/
json.cpp
Public
Notifications
You must be signed in to change notification settings
Fork
38
Star
794
Code
Issues
2
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
json.cpp
/
json.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1371 lines (1289 loc) · 43.5 KB
Breadcrumbs
json.cpp
/
json.cpp
Copy path
File metadata and controls
1371 lines (1289 loc) · 43.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
//
-*- mode:c++;indent-tabs-mode:nil;c-basic-offset:4;coding:utf-8 -*-
//
vi: set et ft=cpp ts=4 sts=4 sw=4 fenc=utf-8 :vi
//
//
Copyright 2024 Mozilla Foundation
//
//
Licensed under the Apache License, Version 2.0 (the "License");
//
you may not use this file except in compliance with the License.
//
You may obtain a copy of the License at
//
//
http://www.apache.org/licenses/LICENSE-2.0
//
//
Unless required by applicable law or agreed to in writing, software
//
distributed under the License is distributed on an "AS IS" BASIS,
//
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
See the License for the specific language governing permissions and
//
limitations under the License.
#
include
"
json.h
"
#
include
"
jtckdint.h
"
#
include
<
cassert
>
#
include
<
cctype
>
#
include
<
climits
>
#
include
<
cstdint
>
#
include
<
cstdlib
>
#
include
<
stdexcept
>
#
include
"
double-conversion/double-to-string.h
"
#
include
"
double-conversion/string-to-double.h
"
#
define
KEY
1
#
define
COMMA
2
#
define
COLON
4
#
define
ARRAY
8
#
define
OBJECT
16
#
define
DEPTH
20
#
define
ASCII
0
#
define
C0
1
#
define
DQUOTE
2
#
define
BACKSLASH
3
#
define
UTF8_2
4
#
define
UTF8_3
5
#
define
UTF8_4
6
#
define
C1
7
#
define
UTF8_3_E0
8
#
define
UTF8_3_ED
9
#
define
UTF8_4_F0
10
#
define
BADUTF8
11
#
define
EVILUTF8
12
#
define
UTF16_MASK
0xfc00
#
define
UTF16_MOAR
0xd800
//
0xD800..0xDBFF
#
define
UTF16_CONT
0xdc00
//
0xDC00..0xDFFF
#
define
READ32LE
(
S
) \
((
uint_least32_t
)(
255
& (S)[
3
]) <<
030
| \
(
uint_least32_t
)(
255
& (S)[
2
]) <<
020
| \
(
uint_least32_t
)(
255
& (S)[
1
]) <<
010
| \
(
uint_least32_t
)(
255
& (S)[
0
]) <<
000
)
#
define
ThomPikeCont
(
x
) (
0200
== (
0300
& (x)))
#
define
ThomPikeByte
(
x
) ((x) & (((
1
<< ThomPikeMsb(x)) -
1
) |
3
))
#
define
ThomPikeLen
(
x
) (
7
- ThomPikeMsb(x))
#
define
ThomPikeMsb
(
x
) ((
255
& (x)) <
252
? Bsr(
255
& ~(x)) :
1
)
#
define
ThomPikeMerge
(
x, y
) ((x) <<
6
| (
077
& (y)))
#
define
IsSurrogate
(
wc
) ((
0xf800
& (wc)) ==
0xd800
)
#
define
IsHighSurrogate
(
wc
) (((wc) &
UTF16_MASK
) ==
UTF16_MOAR
)
#
define
IsLowSurrogate
(
wc
) (((wc) &
UTF16_MASK
) ==
UTF16_CONT
)
#
define
MergeUtf16
(
hi, lo
) ((((hi) -
0xD800
) <<
10
) + ((lo) -
0xDC00
) +
0x10000
)
#
define
EncodeUtf16
(
wc
) \
((
0x0000
<= (wc) && (wc) <=
0xFFFF
) || (
0xE000
<= (wc) && (wc) <=
0xFFFF
) \
? (wc) \
:
0x10000
<= (wc) && (wc) <=
0x10FFFF
\
? (((((wc) -
0x10000
) >>
10
) +
0xD800
) | \
(
unsigned
)((((wc) -
0x10000
) &
1023
) +
0xDC00
) <<
16
) \
:
0xFFFD
)
#
if
defined(__cpp_exceptions) || defined(__EXCEPTIONS) || defined(_CPPUNWIND)
#
define
ON_LOGIC_ERROR
(
s
)
throw
std::logic_error
(s)
#
else
#
define
ON_LOGIC_ERROR
(
s
) abort()
#
endif
namespace
jt
{
static
const
char
kJsonStr
[
256
] = {
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
//
0000 ascii (0)
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
//
0010
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
//
0020 c0 (1)
1
,
1
,
1
,
1
,
1
,
1
,
1
,
1
,
//
0030
0
,
0
,
2
,
0
,
0
,
0
,
0
,
0
,
//
0040 dquote (2)
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0050
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0060
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0070
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0100
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0110
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0120
0
,
0
,
0
,
0
,
3
,
0
,
0
,
0
,
//
0130 backslash (3)
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0140
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0150
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0160
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0170
7
,
7
,
7
,
7
,
7
,
7
,
7
,
7
,
//
0200 c1 (8)
7
,
7
,
7
,
7
,
7
,
7
,
7
,
7
,
//
0210
7
,
7
,
7
,
7
,
7
,
7
,
7
,
7
,
//
0220
7
,
7
,
7
,
7
,
7
,
7
,
7
,
7
,
//
0230
11
,
11
,
11
,
11
,
11
,
11
,
11
,
11
,
//
0240 latin1 (4)
11
,
11
,
11
,
11
,
11
,
11
,
11
,
11
,
//
0250
11
,
11
,
11
,
11
,
11
,
11
,
11
,
11
,
//
0260
11
,
11
,
11
,
11
,
11
,
11
,
11
,
11
,
//
0270
12
,
12
,
4
,
4
,
4
,
4
,
4
,
4
,
//
0300 utf8-2 (5)
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
//
0310
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
//
0320 utf8-2
4
,
4
,
4
,
4
,
4
,
4
,
4
,
4
,
//
0330
8
,
5
,
5
,
5
,
5
,
5
,
5
,
5
,
//
0340 utf8-3 (6)
5
,
5
,
5
,
5
,
5
,
9
,
5
,
5
,
//
0350
10
,
6
,
6
,
6
,
6
,
11
,
11
,
11
,
//
0360 utf8-4 (7)
11
,
11
,
11
,
11
,
11
,
11
,
11
,
11
,
//
0370
};
static
const
char
kEscapeLiteral
[
128
] = {
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
1
,
2
,
9
,
4
,
3
,
9
,
9
,
//
0x00
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
9
,
//
0x10
0
,
0
,
7
,
0
,
0
,
0
,
9
,
9
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
6
,
//
0x20
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
9
,
9
,
9
,
0
,
//
0x30
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0x40
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
5
,
0
,
0
,
0
,
//
0x50
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
//
0x60
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
0
,
9
,
//
0x70
};
alignas
(
signed
char
)
static
const
signed
char
kHexToInt
[
256
] = {
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x00
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x10
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x20
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x30
-
1
,
10
,
11
,
12
,
13
,
14
,
15
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x40
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x50
-
1
,
10
,
11
,
12
,
13
,
14
,
15
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x60
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x70
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x80
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0x90
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0xa0
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0xb0
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0xc0
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0xd0
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0xe0
-
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
, -
1
,
//
0xf0
};
static
const
double_conversion::DoubleToStringConverter
kDoubleToJson
(
double_conversion::DoubleToStringConverter::
UNIQUE_ZERO
|
double_conversion::DoubleToStringConverter::
EMIT_POSITIVE_EXPONENT_SIGN
,
"
1e5000
"
,
"
null
"
,
'
e
'
,
-
6
,
21
,
6
,
0
);
static
const
double_conversion::StringToDoubleConverter
kJsonToDouble
(
double_conversion::StringToDoubleConverter::
ALLOW_CASE_INSENSITIVITY
|
double_conversion::StringToDoubleConverter::
ALLOW_LEADING_SPACES
|
double_conversion::StringToDoubleConverter::
ALLOW_TRAILING_JUNK
|
double_conversion::StringToDoubleConverter::
ALLOW_TRAILING_SPACES
,
0.0
,
1.0
,
"
Infinity
"
,
"
NaN
"
);
#
if
defined(__GNUC__) || defined(__clang__)
#
define
Bsr
(
x
) (__builtin_clz(x) ^ (
sizeof
(
int
) *
CHAR_BIT
-
1
))
#
else
static
int
Bsr
(
int
x)
{
int
r =
0
;
if
(x &
0xFFFF0000u
) {
x >>=
16
;
r |=
16
;
}
if
(x &
0xFF00
) {
x >>=
8
;
r |=
8
;
}
if
(x &
0xF0
) {
x >>=
4
;
r |=
4
;
}
if
(x &
0xC
) {
x >>=
2
;
r |=
2
;
}
if
(x &
0x2
) {
r |=
1
;
}
return
r;
}
#
endif
static
double
StringToDouble
(
const
char
* s,
size_t
n,
int
* out_processed)
{
if
(n == (
size_t
)-
1
)
n =
strlen
(s);
int
processed;
double
res =
kJsonToDouble
.
StringToDouble
(s, n, &processed);
if
(out_processed)
*out_processed = processed;
return
res;
}
static
char
*
UlongToString
(
char
* p,
unsigned
long
long
x)
{
char
t;
size_t
i, a, b;
i =
0
;
do
{
p[i++] = x %
10
+
'
0
'
;
x = x /
10
;
}
while
(x >
0
);
p[i] =
'
\0
'
;
if
(i) {
for
(a =
0
, b = i -
1
; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
}
return
p + i;
}
static
char
*
LongToString
(
char
* p,
long
long
x)
{
if
(x <
0
)
*p++ =
'
-
'
, x =
0
- (
unsigned
long
long
)x;
return
UlongToString
(p, x);
}
Json::Json
(
unsigned
long
value)
{
if
(value <=
LLONG_MAX
) {
type_ = Long;
long_value = value;
}
else
{
type_ = Double;
double_value = value;
}
}
Json::Json
(
unsigned
long
long
value)
{
if
(value <=
LLONG_MAX
) {
type_ = Long;
long_value = value;
}
else
{
type_ = Double;
double_value = value;
}
}
Json::Json
(
const
char
* value)
{
if
(value) {
type_ = String;
new
(&string_value)
std::string
(value);
}
else
{
type_ = Null;
}
}
Json::Json
(
const
std::string& value) : type_(String), string_value(value)
{
}
Json::~Json
()
{
if
(type_ >= String)
clear
();
}
void
Json::clear
()
{
switch
(type_) {
case
String:
string_value.
~basic_string
();
break
;
case
Array:
array_value.
~vector
();
break
;
case
Object:
object_value.
~map
();
break
;
default
:
break
;
}
type_ = Null;
}
Json::Json
(
const
Json& other) : type_(other.type_)
{
switch
(type_) {
case
Null:
break
;
case
Bool:
bool_value = other.
bool_value
;
break
;
case
Long:
long_value = other.
long_value
;
break
;
case
Float:
float_value = other.
float_value
;
break
;
case
Double:
double_value = other.
double_value
;
break
;
case
String:
new
(&string_value)
std::string
(other.
string_value
);
break
;
case
Array:
new
(&array_value) std::vector<Json>(other.
array_value
);
break
;
case
Object:
new
(&object_value) std::map<std::string, Json>(other.
object_value
);
break
;
default
:
ON_LOGIC_ERROR
(
"
Unhandled JSON type.
"
);
}
}
Json&
Json::
operator
=(
const
Json& other)
{
if
(
this
!= &other) {
if
(type_ >= String)
clear
();
type_ = other.
type_
;
switch
(type_) {
case
Null:
break
;
case
Bool:
bool_value = other.
bool_value
;
break
;
case
Long:
long_value = other.
long_value
;
break
;
case
Float:
float_value = other.
float_value
;
break
;
case
Double:
double_value = other.
double_value
;
break
;
case
String:
new
(&string_value)
std::string
(other.
string_value
);
break
;
case
Array:
new
(&array_value) std::vector<Json>(other.
array_value
);
break
;
case
Object:
new
(&object_value)
std::map<std::string, Json>(other.
object_value
);
break
;
default
:
ON_LOGIC_ERROR
(
"
Unhandled JSON type.
"
);
}
}
return
*
this
;
}
Json::Json
(Json&& other) : type_(other.type_)
{
switch
(type_) {
case
Null:
break
;
case
Bool:
bool_value = other.
bool_value
;
break
;
case
Long:
long_value = other.
long_value
;
break
;
case
Float:
float_value = other.
float_value
;
break
;
case
Double:
double_value = other.
double_value
;
break
;
case
String:
new
(&string_value)
std::string
(
std::move
(other.
string_value
));
break
;
case
Array:
new
(&array_value) std::vector<Json>(
std::move
(other.
array_value
));
break
;
case
Object:
new
(&object_value)
std::map<std::string, Json>(
std::move
(other.
object_value
));
break
;
default
:
ON_LOGIC_ERROR
(
"
Unhandled JSON type.
"
);
}
other.
type_
= Null;
}
Json&
Json::
operator
=(Json&& other)
{
if
(
this
!= &other) {
if
(type_ >= String)
clear
();
type_ = other.
type_
;
switch
(type_) {
case
Null:
break
;
case
Bool:
bool_value = other.
bool_value
;
break
;
case
Long:
long_value = other.
long_value
;
break
;
case
Float:
float_value = other.
float_value
;
break
;
case
Double:
double_value = other.
double_value
;
break
;
case
String:
new
(&string_value)
std::string
(
std::move
(other.
string_value
));
break
;
case
Array:
new
(&array_value)
std::vector<Json>(
std::move
(other.
array_value
));
break
;
case
Object:
new
(&object_value)
std::map<std::string, Json>(
std::move
(other.
object_value
));
break
;
default
:
ON_LOGIC_ERROR
(
"
Unhandled JSON type.
"
);;
}
other.
type_
= Null;
}
return
*
this
;
}
double
Json::getNumber
()
const
{
switch
(type_) {
case
Long:
return
long_value;
case
Float:
return
float_value;
case
Double:
return
double_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not a number.
"
);
}
}
long
long
Json::getLong
()
const
{
switch
(type_) {
case
Long:
return
long_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not a long.
"
);
}
}
bool
Json::getBool
()
const
{
switch
(type_) {
case
Bool:
return
bool_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not a bool.
"
);
}
}
float
Json::getFloat
()
const
{
switch
(type_) {
case
Float:
return
float_value;
case
Double:
return
double_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not a floating-point number.
"
);
}
}
double
Json::getDouble
()
const
{
switch
(type_) {
case
Float:
return
float_value;
case
Double:
return
double_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not a floating-point number.
"
);
}
}
std::string&
Json::getString
()
{
switch
(type_) {
case
String:
return
string_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not a string.
"
);
}
}
const
std::string&
Json::getString
()
const
{
switch
(type_) {
case
String:
return
string_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not a string.
"
);
}
}
std::vector<Json>&
Json::getArray
()
{
switch
(type_) {
case
Array:
return
array_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not an array.
"
);
}
}
const
std::vector<Json>&
Json::getArray
()
const
{
switch
(type_) {
case
Array:
return
array_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not an array.
"
);
}
}
std::map<std::string, Json>&
Json::getObject
()
{
switch
(type_) {
case
Object:
return
object_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not an object.
"
);
}
}
const
std::map<std::string, Json>&
Json::getObject
()
const
{
switch
(type_) {
case
Object:
return
object_value;
default
:
ON_LOGIC_ERROR
(
"
JSON value is not an object.
"
);
}
}
void
Json::setArray
()
{
if
(type_ >= String)
clear
();
type_ = Array;
new
(&array_value) std::vector<Json>();
}
void
Json::setObject
()
{
if
(type_ >= String)
clear
();
type_ = Object;
new
(&object_value) std::map<std::string, Json>();
}
bool
Json::contains
(
const
std::string& key)
const
{
if
(!
isObject
())
return
false
;
return
object_value.
find
(key) != object_value.
end
();
}
Json&
Json::
operator
[](
size_t
index)
{
if
(!
isArray
())
setArray
();
if
(index >= array_value.
size
()) {
array_value.
resize
(index +
1
);
}
return
array_value[index];
}
const
Json&
Json::
operator
[](
size_t
index)
const
{
if
(!
isArray
())
ON_LOGIC_ERROR
(
"
JSON value is not an array.
"
);
if
(index >= array_value.
size
()) {
ON_LOGIC_ERROR
(
"
JSON index not in array.
"
);
}
return
array_value[index];
}
Json&
Json::
operator
[](
const
std::string& key)
{
if
(!
isObject
())
setObject
();
return
object_value[key];
}
const
Json&
Json::
operator
[](
const
std::string& key)
const
{
if
(!
isObject
())
ON_LOGIC_ERROR
(
"
JSON value is not an object.
"
);
auto
it = object_value.
find
(key);
if
(it == object_value.
end
())
ON_LOGIC_ERROR
(
"
JSON object does not contain key.
"
);
return
it->
second
;
}
std::string
Json::toString
()
const
{
std::string b;
marshal
(b,
false
,
0
);
return
b;
}
std::string
Json::toStringPretty
()
const
{
std::string b;
marshal
(b,
true
,
0
);
return
b;
}
void
Json::marshal
(std::string& b,
bool
pretty,
int
indent)
const
{
switch
(type_) {
case
Null:
b +=
"
null
"
;
break
;
case
String:
stringify
(b, string_value);
break
;
case
Bool:
b += bool_value ?
"
true
"
:
"
false
"
;
break
;
case
Long: {
char
buf[
64
];
b.
append
(buf,
LongToString
(buf, long_value) - buf);
break
;
}
case
Float: {
char
buf[
128
];
double_conversion::StringBuilder
db
(buf,
128
);
kDoubleToJson
.
ToShortestSingle
(float_value, &db);
db.
Finalize
();
b += buf;
break
;
}
case
Double: {
char
buf[
128
];
double_conversion::StringBuilder
db
(buf,
128
);
kDoubleToJson
.
ToShortest
(double_value, &db);
db.
Finalize
();
b += buf;
break
;
}
case
Array: {
bool
once =
false
;
b +=
'
[
'
;
for
(
auto
i = array_value.
begin
(); i != array_value.
end
(); ++i) {
if
(once) {
b +=
'
,
'
;
if
(pretty)
b +=
'
'
;
}
else
{
once =
true
;
}
i->
marshal
(b, pretty, indent);
}
b +=
'
]
'
;
break
;
}
case
Object: {
bool
once =
false
;
b +=
'
{
'
;
for
(
auto
i = object_value.
begin
(); i != object_value.
end
(); ++i) {
if
(once) {
b +=
'
,
'
;
}
else
{
once =
true
;
}
if
(pretty && object_value.
size
() >
1
) {
b +=
'
\n
'
;
++indent;
for
(
int
j =
0
; j < indent; ++j)
b +=
"
"
;
}
stringify
(b, i->
first
);
b +=
'
:
'
;
if
(pretty)
b +=
'
'
;
i->
second
.
marshal
(b, pretty, indent);
if
(pretty && object_value.
size
() >
1
)
--indent;
}
if
(pretty && object_value.
size
() >
1
) {
b +=
'
\n
'
;
for
(
int
j =
0
; j < indent; ++j)
b +=
"
"
;
++indent;
}
b +=
'
}
'
;
break
;
}
default
:
ON_LOGIC_ERROR
(
"
Unhandled JSON type.
"
);
}
}
void
Json::stringify
(std::string& b,
const
std::string& s)
{
b +=
'
"
'
;
serialize
(b, s);
b +=
'
"
'
;
}
void
Json::serialize
(std::string& sb,
const
std::string& s)
{
size_t
i, j, m;
wint_t
x, a, b;
unsigned
long
long
w;
for
(i =
0
; i < s.
size
();) {
x = s[i++] &
255
;
if
(x >=
0300
) {
a =
ThomPikeByte
(x);
m =
ThomPikeLen
(x) -
1
;
if
(i + m <= s.
size
()) {
for
(j =
0
;;) {
b = s[i + j] &
0xff
;
if
(!
ThomPikeCont
(b))
break
;
a =
ThomPikeMerge
(a, b);
if
(++j == m) {
x = a;
i += j;
break
;
}
}
}
}
switch
(
0
<= x && x <=
127
?
kEscapeLiteral
[x] :
9
) {
case
0
:
sb += x;
break
;
case
1
:
sb +=
"
\\
t
"
;
break
;
case
2
:
sb +=
"
\\
n
"
;
break
;
case
3
:
sb +=
"
\\
r
"
;
break
;
case
4
:
sb +=
"
\\
f
"
;
break
;
case
5
:
sb +=
"
\\\\
"
;
break
;
case
6
:
sb +=
"
\\
/
"
;
break
;
case
7
:
sb +=
"
\\\"
"
;
break
;
case
9
:
w =
EncodeUtf16
(x);
do
{
char
esc[
6
];
esc[
0
] =
'
\\
'
;
esc[
1
] =
'
u
'
;
esc[
2
] =
"
0123456789abcdef
"
[(w &
0xF000
) >>
014
];
esc[
3
] =
"
0123456789abcdef
"
[(w &
0x0F00
) >>
010
];
esc[
4
] =
"
0123456789abcdef
"
[(w &
0x00F0
) >>
004
];
esc[
5
] =
"
0123456789abcdef
"
[(w &
0x000F
) >>
000
];
sb.
append
(esc,
6
);
}
while
((w >>=
16
));
break
;
default
:
ON_LOGIC_ERROR
(
"
Unhandled character escape code during string serialization.
"
);
}
}
}
Json::Status
Json::parse
(Json& json,
const
char
*& p,
const
char
* e,
int
context,
int
depth)
{
char
w[
4
];
long
long
x;
const
char
* a;
int
A, B, C, D, c, d, i, u;
if
(!depth)
return
depth_exceeded;
for
(a = p, d = +
1
; p < e;) {
switch
((c = *p++ &
255
)) {
case
'
'
:
//
spaces
case
'
\n
'
:
case
'
\r
'
:
case
'
\t
'
:
a = p;
break
;
case
'
,
'
:
//
present in list and object
if
(context &
COMMA
) {
context =
0
;
a = p;
break
;
}
else
{
return
unexpected_comma;
}
case
'
:
'
:
//
present only in object after key
if
(context &
COLON
) {
context =
0
;
a = p;
break
;
}
else
{
return
unexpected_colon;
}
case
'
n
'
:
//
null
if
(context & (
KEY
|
COLON
|
COMMA
))
goto
OnColonCommaKey;
if
(p +
3
<= e &&
READ32LE
(p -
1
) ==
READ32LE
(
"
null
"
)) {
p +=
3
;
return
success;
}
else
{
return
illegal_character;
}
case
'
f
'
:
//
false
if
(context & (
KEY
|
COLON
|
COMMA
))
goto
OnColonCommaKey;
if
(p +
4
<= e &&
READ32LE
(p) ==
READ32LE
(
"
alse
"
)) {
json.
type_
= Bool;
json.
bool_value
=
false
;
p +=
4
;
return
success;
}
else
{
return
illegal_character;
}
case
'
t
'
:
//
true
if
(context & (
KEY
|
COLON
|
COMMA
))
goto
OnColonCommaKey;
if
(p +
3
<= e &&
READ32LE
(p -
1
) ==
READ32LE
(
"
true
"
)) {
json.
type_
= Bool;
json.
bool_value
=
true
;
p +=
3
;
return
success;
}
else
{
return
illegal_character;
}
default
:
return
illegal_character;
OnColonCommaKey:
if
(context &
KEY
)
return
object_key_must_be_string;
OnColonComma:
if
(context &
COLON
)
return
missing_colon;
return
missing_comma;
case
'
-
'
:
//
negative
if
(context & (
COLON
|
COMMA
|
KEY
))
goto
OnColonCommaKey;
if
(p < e &&
isdigit
(*p)) {
d = -
1
;
break
;
}
else
{
return
bad_negative;
}
case
'
0
'
:
//
zero or number
if
(context & (
COLON
|
COMMA
|
KEY
))
goto
OnColonCommaKey;
if
(p < e) {
if
(*p ==
'
.
'
) {
if
(p +
1
== e || !
isdigit
(p[
1
]))
return
bad_double;
goto
UseDubble;
}
else
if
(*p ==
'
e
'
|| *p ==
'
E
'
) {
goto
UseDubble;
}
else
if
(
isdigit
(*p)) {
return
unexpected_octal;
}
}
json.
type_
= Long;
json.
long_value
=
0
;
return
success;
case
'
1
'
:
case
'
2
'
:
case
'
3
'
:
case
'
4
'
:
case
'
5
'
:
case
'
6
'
:
case
'
7
'
:
case
'
8
'
:
case
'
9
'
:
//
integer
if
(context & (
COLON
|
COMMA
|
KEY
))
goto
OnColonCommaKey;
for
(x = (c -
'
0
'
) * d; p < e; ++p) {
c = *p &
255
;
if
(
isdigit
(c)) {
if
(
ckd_mul
(&x, x,
10
) ||
ckd_add
(&x, x, (c -
'
0
'
) * d)) {
goto
UseDubble;
}
}
else
if
(c ==
'
.
'
) {
if
(p +
1
== e || !
isdigit
(p[
1
]))
return
bad_double;
goto
UseDubble;
}
else
if
(c ==
'
e
'
|| c ==
'
E
'
) {
goto
UseDubble;
}
else
{
break
;
}
}
json.
type_
= Long;
json.
long_value
= x;
return
success;
UseDubble:
//
number
json.
type_
= Double;
json.
double_value
=
StringToDouble
(a, e - a, &c);
if
(c <=
0
)
return
bad_double;
if
(a + c < e && (a[c] ==
'
e
'
|| a[c] ==
'
E
'
))
return
bad_exponent;
p = a + c;
return
success;
case
'
[
'
: {
//
Array
if
(context & (
COLON
|
COMMA
|
KEY
))
goto
OnColonCommaKey;
json.
setArray
();
Json value;
for
(context =
ARRAY
, i =
0
;;) {
Status status =
parse
(value, p, e, context, depth -
1
);
if
(status == absent_value)
return
success;
if
(status != success)
return
status;
json.
array_value
.
emplace_back
(
std::move
(value));
context =
ARRAY
|
COMMA
;
}
}
case
'
]
'
:
if
(context &
ARRAY
)
return
absent_value;
return
unexpected_end_of_array;
case
'
}
'
:
if
(context &
OBJECT
)
return
absent_value;
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL