-
Notifications
You must be signed in to change notification settings - Fork 6
/
json_test.go
1583 lines (1387 loc) · 35.5 KB
/
json_test.go
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
package jsoncolor
import (
"bytes"
"compress/gzip"
"encoding"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"math"
"os"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
"testing"
"time"
)
// The encoding/json package does not export the msg field of json.SyntaxError,
// so we use this replacement type in tests.
type testSyntaxError struct {
msg string
Offset int64
}
func (e *testSyntaxError) Error() string { return e.msg }
var (
marshal func([]byte, interface{}) ([]byte, error)
unmarshal func([]byte, interface{}) error
escapeHTML bool
)
func TestMain(m *testing.M) {
var pkg string
flag.StringVar(&pkg, "package", ".", "The name of the package to test (encoding/json, or default to this package)")
flag.BoolVar(&escapeHTML, "escapehtml", false, "Whether to enable HTML escaping or not")
flag.Parse()
switch pkg {
case "encoding/json":
buf := &buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(escapeHTML)
marshal = func(b []byte, v interface{}) ([]byte, error) {
buf.data = b
err := enc.Encode(v)
return buf.data, err
}
unmarshal = json.Unmarshal
default:
flags := AppendFlags(0)
if escapeHTML {
flags |= EscapeHTML
}
marshal = func(b []byte, v interface{}) ([]byte, error) {
return Append(b, v, flags, nil, nil)
}
unmarshal = func(b []byte, v interface{}) error {
_, err := Parse(b, v, ZeroCopy)
return err
}
}
os.Exit(m.Run())
}
type point struct {
X int `json:"x"`
Y int `json:"y"`
}
type tree struct {
Value string
Left *tree
Right *tree
}
var testValues = [...]interface{}{
// constants
nil,
false,
true,
// int
int(0),
int(1),
int(42),
int(-1),
int(-42),
int8(math.MaxInt8),
int8(math.MinInt8),
int16(math.MaxInt16),
int16(math.MinInt16),
int32(math.MaxInt32),
int32(math.MinInt32),
int64(math.MaxInt64),
int64(math.MinInt64),
// uint
uint(0),
uint(1),
uintptr(0),
uintptr(1),
uint8(math.MaxUint8),
uint16(math.MaxUint16),
uint32(math.MaxUint32),
uint64(math.MaxUint64),
// float
float32(0),
float32(0.5),
float32(math.SmallestNonzeroFloat32),
float32(math.MaxFloat32),
float64(0),
float64(0.5),
float64(math.SmallestNonzeroFloat64),
float64(math.MaxFloat64),
// number
Number("0"),
Number("1234567890"),
Number("-0.5"),
Number("-1e+2"),
// string
"",
"Hello World!",
"Hello\"World!",
"Hello\\World!",
"Hello\nWorld!",
"Hello\rWorld!",
"Hello\tWorld!",
"Hello\bWorld!",
"Hello\fWorld!",
"你好",
"<",
">",
"&",
"\u001944",
"\u00c2e>",
"\u00c2V?",
"\u000e=8",
"\u001944\u00c2e>\u00c2V?\u000e=8",
"ir\u001bQJ\u007f\u0007y\u0015)",
strings.Repeat("A", 32),
strings.Repeat("A", 250),
strings.Repeat("A", 1020),
// bytes
[]byte(""),
[]byte("Hello World!"),
bytes.Repeat([]byte("A"), 250),
bytes.Repeat([]byte("A"), 1020),
// time
time.Unix(0, 0).In(time.UTC),
time.Unix(1, 42).In(time.UTC),
time.Unix(17179869184, 999999999).In(time.UTC),
time.Date(2016, 12, 20, 0, 20, 1, 0, time.UTC),
// array
[...]int{},
[...]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
// slice
[]int{},
[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
makeSlice(250),
makeSlice(1020),
[]string{"A", "B", "C"},
[]interface{}{nil, true, false, 0.5, "Hello World!"},
// map
makeMapStringBool(0),
makeMapStringBool(15),
makeMapStringBool(1020),
makeMapStringInterface(0),
makeMapStringInterface(15),
makeMapStringInterface(1020),
map[int]bool{1: false, 42: true},
map[textValue]bool{{1, 2}: true, {3, 4}: false},
map[string]*point{
"A": {1, 2},
"B": {3, 4},
"C": {5, 6},
},
map[string]RawMessage{
"A": RawMessage(`{}`),
"B": RawMessage(`null`),
"C": RawMessage(`42`),
},
// struct
struct{}{},
struct{ A int }{42},
struct{ A, B, C int }{1, 2, 3},
struct {
A int
T time.Time
S string
}{42, time.Date(2016, 12, 20, 0, 20, 1, 0, time.UTC), "Hello World!"},
// These types are interesting because they fit in a pointer so the compiler
// puts their value directly into the pointer field of the interface{} that
// is passed to Marshal.
struct{ X *int }{},
struct{ X *int }{new(int)},
struct{ X **int }{},
// Struct types with more than one pointer, those exercise the regular
// pointer handling with code that dereferences the fields.
struct{ X, Y *int }{},
struct{ X, Y *int }{new(int), new(int)},
struct {
A string `json:"name"`
B string `json:"-"`
C string `json:",omitempty"`
D map[string]interface{} `json:",string"`
e string
}{A: "Luke", D: map[string]interface{}{"answer": float64(42)}},
struct{ point }{point{1, 2}},
tree{
Value: "T",
Left: &tree{Value: "L"},
Right: &tree{Value: "R", Left: &tree{Value: "R-L"}},
},
// pointer
(*string)(nil),
new(int),
// Marshaler/Unmarshaler
jsonValue{},
jsonValue{1, 2},
// encoding.TextMarshaler/encoding.TextUnmarshaler
textValue{},
textValue{1, 2},
// RawMessage
RawMessage(`{
"answer": 42,
"hello": "world"
}`),
// fixtures
loadTestdata(filepath.Join(runtime.GOROOT(), "src/encoding/json/testdata/code.json.gz")),
}
var durationTestValues = []interface{}{
// duration
time.Nanosecond,
time.Microsecond,
time.Millisecond,
time.Second,
time.Minute,
time.Hour,
// struct with duration
struct{ D1, D2 time.Duration }{time.Millisecond, time.Hour},
}
func makeSlice(n int) []int {
s := make([]int, n)
for i := range s {
s[i] = i
}
return s
}
func makeMapStringBool(n int) map[string]bool {
m := make(map[string]bool, n)
for i := 0; i != n; i++ {
m[strconv.Itoa(i)] = true
}
return m
}
func makeMapStringInterface(n int) map[string]interface{} {
m := make(map[string]interface{}, n)
for i := 0; i != n; i++ {
m[strconv.Itoa(i)] = nil
}
return m
}
func testName(v interface{}) string {
return fmt.Sprintf("%T", v)
}
type codeResponse2 struct {
Tree *codeNode2 `json:"tree"`
Username string `json:"username"`
}
type codeNode2 struct {
Name string `json:"name"`
Kids []*codeNode `json:"kids"`
CLWeight float64 `json:"cl_weight"`
Touches int `json:"touches"`
MinT int64 `json:"min_t"`
MaxT int64 `json:"max_t"`
MeanT int64 `json:"mean_t"`
}
func loadTestdata(path string) interface{} {
f, err := os.Open(path)
if err != nil {
return err.Error()
}
defer f.Close()
r, err := gzip.NewReader(f)
if err != nil {
return err.Error()
}
defer r.Close()
testdata := new(codeResponse2)
if err := json.NewDecoder(r).Decode(testdata); err != nil {
return err.Error()
}
return testdata
}
func TestCodec(t *testing.T) {
for _, v1 := range testValues {
t.Run(testName(v1), func(t *testing.T) {
v2 := newValue(v1)
a, err := json.MarshalIndent(v1, "", "\t")
if err != nil {
t.Error(err)
return
}
a = append(a, '\n')
buf := &bytes.Buffer{}
enc := NewEncoder(buf)
enc.SetIndent("", "\t")
if err := enc.Encode(v1); err != nil {
t.Error(err)
return
}
b := buf.Bytes()
if !Valid(b) {
t.Error("invalid JSON representation")
}
if !bytes.Equal(a, b) {
t.Error("JSON representations mismatch")
t.Log("expected:", string(a))
t.Log("found: ", string(b))
}
dec := NewDecoder(bytes.NewBuffer(b))
if err := dec.Decode(v2.Interface()); err != nil {
t.Errorf("%T: %v", err, err)
return
}
x1 := v1
x2 := v2.Elem().Interface()
if !reflect.DeepEqual(x1, x2) {
t.Error("values mismatch")
t.Logf("expected: %#v", x1)
t.Logf("found: %#v", x2)
}
if b, err := ioutil.ReadAll(dec.Buffered()); err != nil {
t.Error(err)
} else if len(b) != 0 {
t.Errorf("leftover trailing bytes in the decoder: %q", b)
}
})
}
}
// TestCodecDuration isolates testing of time.Duration. The stdlib un/marshals
// this type as integers whereas this library un/marshals formatted string
// values. Therefore, plugging durations into TestCodec would cause fail since
// it checks equality on the marshaled strings from the two libraries.
func TestCodecDuration(t *testing.T) {
t.Skip("Skipping because neilotoole/jsoncolor follows stdlib (encode to int64) rather than segmentj (encode to string)")
for _, v1 := range durationTestValues {
t.Run(testName(v1), func(t *testing.T) {
v2 := newValue(v1)
// encode using stdlib. (will be an int)
std, err := json.MarshalIndent(v1, "", "\t")
if err != nil {
t.Error(err)
return
}
std = append(std, '\n')
// decode using our decoder. (reads int to duration)
dec := NewDecoder(bytes.NewBuffer([]byte(std)))
if err := dec.Decode(v2.Interface()); err != nil {
t.Errorf("%T: %v", err, err)
return
}
x1 := v1
x2 := v2.Elem().Interface()
if !reflect.DeepEqual(x1, x2) {
t.Error("values mismatch")
t.Logf("expected: %#v", x1)
t.Logf("found: %#v", x2)
}
// encoding using our encoder. (writes duration as string)
buf := &bytes.Buffer{}
enc := NewEncoder(buf)
enc.SetIndent("", "\t")
if err := enc.Encode(v1); err != nil {
t.Error(err)
return
}
b := buf.Bytes()
if !Valid(b) {
t.Error("invalid JSON representation")
}
if reflect.DeepEqual(std, b) {
t.Error("encoded durations should not match stdlib")
t.Logf("got: %s", b)
}
// decode using our decoder. (reads string to duration)
dec = NewDecoder(bytes.NewBuffer([]byte(std)))
if err := dec.Decode(v2.Interface()); err != nil {
t.Errorf("%T: %v", err, err)
return
}
x1 = v1
x2 = v2.Elem().Interface()
if !reflect.DeepEqual(x1, x2) {
t.Error("values mismatch")
t.Logf("expected: %#v", x1)
t.Logf("found: %#v", x2)
}
})
}
}
func newValue(model interface{}) reflect.Value {
if model == nil {
return reflect.New(reflect.TypeOf(&model).Elem())
}
return reflect.New(reflect.TypeOf(model))
}
func BenchmarkMarshal(b *testing.B) {
j := make([]byte, 0, 128*1024)
for _, v := range testValues {
b.Run(testName(v), func(b *testing.B) {
if marshal == nil {
return
}
for i := 0; i != b.N; i++ {
j, _ = marshal(j[:0], v)
}
b.SetBytes(int64(len(j)))
})
}
}
func BenchmarkUnmarshal(b *testing.B) {
for _, v := range testValues {
b.Run(testName(v), func(b *testing.B) {
if unmarshal == nil {
return
}
x := v
if d, ok := x.(time.Duration); ok {
x = duration(d)
}
j, _ := json.Marshal(x)
x = newValue(v).Interface()
for i := 0; i != b.N; i++ {
unmarshal(j, x)
}
b.SetBytes(int64(len(j)))
})
}
}
type buffer struct{ data []byte }
func (buf *buffer) Write(b []byte) (int, error) {
buf.data = append(buf.data, b...)
return len(b), nil
}
func (buf *buffer) WriteString(s string) (int, error) {
buf.data = append(buf.data, s...)
return len(s), nil
}
type jsonValue struct {
x int32
y int32
}
func (v jsonValue) MarshalJSON() ([]byte, error) {
return Marshal([2]int32{v.x, v.y})
}
func (v *jsonValue) UnmarshalJSON(b []byte) error {
var a [2]int32
err := Unmarshal(b, &a)
v.x = a[0]
v.y = a[1]
return err
}
type textValue struct {
x int32
y int32
}
func (v textValue) MarshalText() ([]byte, error) {
return []byte(fmt.Sprintf("(%d,%d)", v.x, v.y)), nil
}
func (v *textValue) UnmarshalText(b []byte) error {
_, err := fmt.Sscanf(string(b), "(%d,%d)", &v.x, &v.y)
return err
}
type duration time.Duration
func (d duration) MarshalJSON() ([]byte, error) {
return []byte(`"` + time.Duration(d).String() + `"`), nil
}
func (d *duration) UnmarshalJSON(b []byte) error {
var s string
if err := json.Unmarshal(b, &s); err != nil {
return err
}
x, err := time.ParseDuration(s)
*d = duration(x)
return err
}
var (
_ Marshaler = jsonValue{}
_ Marshaler = duration(0)
_ encoding.TextMarshaler = textValue{}
_ Unmarshaler = (*jsonValue)(nil)
_ Unmarshaler = (*duration)(nil)
_ encoding.TextUnmarshaler = (*textValue)(nil)
)
func TestDecodeStructFieldCaseInsensitive(t *testing.T) {
b := []byte(`{ "type": "changed" }`)
s := struct {
Type string
}{"unchanged"}
if err := Unmarshal(b, &s); err != nil {
t.Error(err)
}
if s.Type != "changed" {
t.Error("s.Type: expected to be changed but found", s.Type)
}
}
func TestDecodeLines(t *testing.T) {
tests := []struct {
desc string
reader io.Reader
expectCount int
}{
// simple
{
desc: "bare object",
reader: strings.NewReader("{\"Good\":true}"),
expectCount: 1,
},
{
desc: "multiple objects on one line",
reader: strings.NewReader("{\"Good\":true}{\"Good\":true}\n"),
expectCount: 2,
},
{
desc: "object spanning multiple lines",
reader: strings.NewReader("{\n\"Good\":true\n}\n"),
expectCount: 1,
},
// whitespace handling
{
desc: "trailing newline",
reader: strings.NewReader("{\"Good\":true}\n{\"Good\":true}\n"),
expectCount: 2,
},
{
desc: "multiple trailing newlines",
reader: strings.NewReader("{\"Good\":true}\n{\"Good\":true}\n\n"),
expectCount: 2,
},
{
desc: "blank lines",
reader: strings.NewReader("{\"Good\":true}\n\n{\"Good\":true}"),
expectCount: 2,
},
{
desc: "no trailing newline",
reader: strings.NewReader("{\"Good\":true}\n{\"Good\":true}"),
expectCount: 2,
},
{
desc: "leading whitespace",
reader: strings.NewReader(" {\"Good\":true}\n\t{\"Good\":true}"),
expectCount: 2,
},
// multiple reads
{
desc: "one object, multiple reads",
reader: io.MultiReader(
strings.NewReader("{"),
strings.NewReader("\"Good\": true"),
strings.NewReader("}\n"),
),
expectCount: 1,
},
// EOF reads
{
desc: "one object + EOF",
reader: &eofReader{"{\"Good\":true}\n"},
expectCount: 1,
},
{
desc: "leading whitespace + EOF",
reader: &eofReader{"\n{\"Good\":true}\n"},
expectCount: 1,
},
{
desc: "multiple objects + EOF",
reader: &eofReader{"{\"Good\":true}\n{\"Good\":true}\n"},
expectCount: 2,
},
{
desc: "one object + multiple reads + EOF",
reader: io.MultiReader(
strings.NewReader("{"),
strings.NewReader(" \"Good\": true"),
&eofReader{"}\n"},
),
expectCount: 1,
},
{
desc: "multiple objects + multiple reads + EOF",
reader: io.MultiReader(
strings.NewReader("{"),
strings.NewReader(" \"Good\": true}{\"Good\": true}"),
&eofReader{"\n"},
),
expectCount: 2,
},
{
// the 2nd object should be discarded, as 42 cannot be cast to bool
desc: "unmarshal error while decoding",
reader: strings.NewReader("{\"Good\":true}\n{\"Good\":42}\n{\"Good\":true}\n"),
expectCount: 2,
},
{
// the 2nd object should be discarded, as 42 cannot be cast to bool
desc: "unmarshal error while decoding last object",
reader: strings.NewReader("{\"Good\":true}\n{\"Good\":42}\n"),
expectCount: 1,
},
}
type obj struct {
Good bool
}
for _, test := range tests {
t.Run(test.desc, func(t *testing.T) {
d := NewDecoder(test.reader)
var count int
var err error
for {
var o obj
err = d.Decode(&o)
if err != nil {
if err == io.EOF {
break
}
switch err.(type) {
case *SyntaxError, *UnmarshalTypeError, *UnmarshalFieldError:
t.Log("unmarshal error", err)
continue
}
t.Error("decode error", err)
break
}
if !o.Good {
t.Errorf("object was not unmarshaled correctly: %#v", o)
}
count++
}
if err != nil && err != io.EOF {
t.Error(err)
}
if count != test.expectCount {
t.Errorf("expected %d objects, got %d", test.expectCount, count)
}
})
}
}
// eofReader is a simple io.Reader that reads its full contents _and_ returns
// and EOF in the first call. Subsequent Read calls only return EOF.
type eofReader struct {
s string
}
func (r *eofReader) Read(p []byte) (n int, err error) {
n = copy(p, r.s)
r.s = r.s[n:]
if r.s == "" {
err = io.EOF
}
return
}
func TestDontMatchCaseIncensitiveStructFields(t *testing.T) {
b := []byte(`{ "type": "changed" }`)
s := struct {
Type string
}{"unchanged"}
if _, err := Parse(b, &s, DontMatchCaseInsensitiveStructFields); err != nil {
t.Error(err)
}
if s.Type != "unchanged" {
t.Error("s.Type: expected to be unchanged but found", s.Type)
}
}
func TestMarshalFuzzBugs(t *testing.T) {
tests := []struct {
value interface{}
output string
}{
{ // html sequences are escaped even in RawMessage
value: struct {
P RawMessage
}{P: RawMessage(`"<"`)},
output: "{\"P\":\"\\u003c\"}",
},
{ // raw message output is compacted
value: struct {
P RawMessage
}{P: RawMessage(`{"" :{}}`)},
output: "{\"P\":{\"\":{}}}",
},
}
for _, test := range tests {
t.Run("", func(t *testing.T) {
b, err := Marshal(test.value)
if err != nil {
t.Fatal(err)
}
if string(b) != test.output {
t.Error("values mismatch")
t.Logf("expected: %#v", test.output)
t.Logf("found: %#v", string(b))
}
})
}
}
func TestUnmarshalFuzzBugs(t *testing.T) {
tests := []struct {
input string
value interface{}
}{
{ // non-UTF8 sequences must be converted to the utf8.RuneError character.
input: "[\"00000\xef\"]",
value: []interface{}{"00000�"},
},
{ // UTF16 surrogate followed by null character
input: "[\"\\ud800\\u0000\"]",
value: []interface{}{"�\x00"},
},
{ // UTF16 surrogate followed by ascii character
input: "[\"\\uDF00\\u000e\"]",
value: []interface{}{"�\x0e"},
},
{ // UTF16 surrogate followed by unicode character
input: "[[\"\\uDF00\\u0800\"]]",
value: []interface{}{[]interface{}{"�ࠀ"}},
},
{ // invalid UTF16 surrogate sequenced followed by a valid UTF16 surrogate sequence
input: "[\"\\udf00\\udb00\\udf00\"]",
value: []interface{}{"�\U000d0300"},
},
{ // decode single-element slice into []byte field
input: "{\"f\":[0],\"0\":[0]}",
value: struct{ F []byte }{F: []byte{0}},
},
{ // decode multi-element slice into []byte field
input: "{\"F\":[3,1,1,1,9,9]}",
value: struct{ F []byte }{F: []byte{3, 1, 1, 1, 9, 9}},
},
{ // decode string with escape sequence into []byte field
input: "{\"F\":\"0p00\\r\"}",
value: struct{ F []byte }{F: []byte("ҝ4")},
},
{ // decode unicode code points which fold into ascii characters
input: "{\"ſ\":\"8\"}",
value: struct {
S int `json:",string"`
}{S: 8},
},
{ // decode unicode code points which don't fold into ascii characters
input: "{\"İ\":\"\"}",
value: struct{ I map[string]string }{I: nil},
},
{ // override pointer-to-pointer field clears the inner pointer only
input: "{\"o\":0,\"o\":null}",
value: struct{ O **int }{O: new(*int)},
},
{ // subsequent occurrences of a map field retain keys previously loaded
input: "{\"i\":{\"\":null},\"i\":{}}",
value: struct{ I map[string]string }{I: map[string]string{"": ""}},
},
{ // an empty string is an invalid JSON input
input: "",
},
{ // ASCII character below 0x20 are invalid JSON input
input: "[\"\b\"]",
},
{ // random byte before any value
input: "\xad",
},
{ // cloud be the beginning of a false value but not
input: "f",
value: false,
},
{ // random ASCII character
input: "}",
value: []interface{}{},
},
{ // random byte after valid JSON, decoded to a nil type
input: "0\x93",
},
{ // random byte after valid JSON, decoded to a int type
input: "0\x93",
value: 0,
},
{ // random byte after valid JSON, decoded to a slice type
input: "0\x93",
value: []interface{}{},
},
{ // decode integer into slice
input: "0",
value: []interface{}{},
},
{ // decode integer with trailing space into slice
input: "0\t",
value: []interface{}{},
},
{ // decode integer with leading random bytes into slice
input: "\b0",
value: []interface{}{},
},
{ // decode string into slice followed by number
input: "\"\"0",
value: []interface{}{},
},
{ // decode what looks like an object followed by a number into a string
input: "{0",
value: "",
},
{ // decode what looks like an object followed by a number into a map
input: "{0",
value: map[string]string{},
},
{ // decode string into string with trailing random byte
input: "\"\"\f",
value: "",
},
{ // decode weird number value into nil
input: "-00",
},
{ // decode an invalid escaped sequence
input: "\"\\0\"",
value: "",
},
{ // decode what looks like an array followed by a number into a slice
input: "[9E600",
value: []interface{}{},
},
{ // decode a number which is too large to fit in a float64
input: "[1e900]",
value: []interface{}{},
},
{ // many nested arrays openings
input: "[[[[[[",
value: []interface{}{},
},
{ // decode a map with value type mismatch and missing closing character
input: "{\"\":0",
value: map[string]string{},
},
{ // decode a struct with value type mismatch and missing closing character
input: "{\"E\":\"\"",
value: struct{ E uint8 }{},
},
{ // decode a map with value type mismatch
input: "{\"\":0}",
value: map[string]string{},
},
{ // decode number with exponent into integer field
input: "{\"e\":0e0}",
value: struct{ E uint8 }{},
},
{ // decode invalid integer representation into integer field
input: "{\"e\":00}",
value: struct{ E uint8 }{},
},
{ // decode unterminated array into byte slice
input: "{\"F\":[",
value: struct{ F []byte }{},
},
{ // attempt to decode string into in
input: "{\"S\":\"\"}",
value: struct {
S int `json:",string"`
}{},
},
{ // decode object with null key into map
input: "{null:0}",
value: map[string]interface{}{},
},
{ // decode unquoted integer into struct field with string tag
input: "{\"S\":0}",
value: struct {
S int `json:",string"`
}{},
},
{ // invalid base64 content when decoding string into byte slice
input: "{\"F\":\"0\"}",
value: struct{ F []byte }{},
},
{ // decode an object with a "null" string as key
input: "{\"null\":null}",