...
1
16
17 package issue_test
18
19 import (
20 `bytes`
21 `encoding/json`
22 `strings`
23 `testing`
24
25 `github.com/stretchr/testify/require`
26
27 `github.com/bytedance/sonic`
28 `github.com/bytedance/sonic/decoder`
29 )
30
31 var issue_19x_idata = "\"" + strings.Repeat("9", 1000) + "\""
32 var issue_19x_fdata = "\"" + strings.Repeat("9", 100) + "." + strings.Repeat("9", 1000) + "\""
33 var issue_19x_ndata = strings.Repeat("9", 1000)
34 var issue_19x_invalid = strings.Repeat("9", 100) + "abc99"
35
36 func TestDecodeLongStringToJsonNumber(t *testing.T) {
37 var objs, obje json.Number
38 errs := sonic.UnmarshalString(issue_19x_idata, &objs)
39 erre := json.Unmarshal([]byte(issue_19x_idata), &obje)
40 require.Equal(t, erre, errs)
41 require.Equal(t, obje, objs)
42
43 var fobjs, fobje json.Number
44 errs = sonic.UnmarshalString(issue_19x_fdata, &fobjs)
45 erre = json.Unmarshal([]byte(issue_19x_fdata), &fobje)
46 require.Equal(t, erre, errs)
47 require.Equal(t, fobje, fobjs)
48
49 var objs2, obje2 json.Number
50 errs = sonic.UnmarshalString(issue_19x_invalid, &objs2)
51 erre = json.Unmarshal([]byte(issue_19x_invalid), &obje2)
52 require.NotNil(t, erre)
53 require.NotNil(t, errs)
54
55
56 var iobjs, iobje interface{}
57 dc := decoder.NewDecoder(issue_19x_ndata)
58 dc.UseNumber()
59 errs = dc.Decode(&iobjs)
60 r := json.NewDecoder(bytes.NewBufferString(issue_19x_ndata))
61 r.UseNumber()
62 erre = r.Decode(&iobje)
63 require.Equal(t, erre, errs)
64 require.Equal(t, iobje, iobjs)
65
66 var iobjs2, iobje2 interface{}
67 dc = decoder.NewDecoder(issue_19x_invalid)
68 dc.UseNumber()
69 errs = dc.Decode(&iobjs2)
70 r = json.NewDecoder(bytes.NewBufferString(issue_19x_invalid))
71 r.UseNumber()
72 erre = r.Decode(&iobje2)
73 require.Equal(t, erre, errs)
74 require.Equal(t, iobje2, iobjs2)
75
76 }
77
78 var jsonNumberBig = "\"" + strings.Repeat("9", 10) + "." + strings.Repeat("9", 100) + "\""
79
80 func BenchmarkDecodeJsonNumber_Sonic(b *testing.B) {
81 b.SetBytes(int64(len(jsonNumberBig)))
82 b.ResetTimer()
83 for i:=0; i<b.N; i++ {
84 var obj json.Number
85 _ = sonic.UnmarshalString(jsonNumberBig, &obj)
86 }
87 }
88
89 func BenchmarkDecodeUseNumber_Sonic(b *testing.B) {
90 b.SetBytes(int64(len(jsonNumberBig)))
91 b.ResetTimer()
92 for i:=0; i<b.N; i++ {
93 var obj interface{}
94 dc := decoder.NewDecoder(jsonNumberBig)
95 dc.UseNumber()
96 _ = dc.Decode(&obj)
97 }
98 }
View as plain text