github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/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/goshafaq/sonic"
    28  	"github.com/goshafaq/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  	var iobjs, iobje interface{}
    56  	dc := decoder.NewDecoder(issue_19x_ndata)
    57  	dc.UseNumber()
    58  	errs = dc.Decode(&iobjs)
    59  	r := json.NewDecoder(bytes.NewBufferString(issue_19x_ndata))
    60  	r.UseNumber()
    61  	erre = r.Decode(&iobje)
    62  	require.Equal(t, erre, errs)
    63  	require.Equal(t, iobje, iobjs)
    64  
    65  	var iobjs2, iobje2 interface{}
    66  	dc = decoder.NewDecoder(issue_19x_invalid)
    67  	dc.UseNumber()
    68  	errs = dc.Decode(&iobjs2)
    69  	r = json.NewDecoder(bytes.NewBufferString(issue_19x_invalid))
    70  	r.UseNumber()
    71  	erre = r.Decode(&iobje2)
    72  	require.Equal(t, erre, errs)
    73  	require.Equal(t, iobje2, iobjs2)
    74  	// spew.Dump(iobje2)
    75  }
    76  
    77  var jsonNumberBig = "\"" + strings.Repeat("9", 10) + "." + strings.Repeat("9", 100) + "\""
    78  
    79  func BenchmarkDecodeJsonNumber_Sonic(b *testing.B) {
    80  	b.SetBytes(int64(len(jsonNumberBig)))
    81  	b.ResetTimer()
    82  	for i := 0; i < b.N; i++ {
    83  		var obj json.Number
    84  		_ = sonic.UnmarshalString(jsonNumberBig, &obj)
    85  	}
    86  }
    87  
    88  func BenchmarkDecodeUseNumber_Sonic(b *testing.B) {
    89  	b.SetBytes(int64(len(jsonNumberBig)))
    90  	b.ResetTimer()
    91  	for i := 0; i < b.N; i++ {
    92  		var obj interface{}
    93  		dc := decoder.NewDecoder(jsonNumberBig)
    94  		dc.UseNumber()
    95  		_ = dc.Decode(&obj)
    96  	}
    97  }