github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/issue_test/issue206_test.go (about) 1 /* 2 * Copyright 2021 ByteDance Inc. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 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 // spew.Dump(iobje2) 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 }