github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/issue_test/issue195_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      `testing`
    21      `encoding/json`
    22      `github.com/stretchr/testify/require`
    23  
    24      `github.com/bytedance/sonic`
    25  )
    26  
    27  func TestDecodeStringToJsonNumber(t *testing.T) {
    28  	var objs json.Number 
    29  	errs := sonic.UnmarshalString(`"1234"`, &objs)
    30  	var obje json.Number 
    31  	erre := json.Unmarshal([]byte(`"1234"`), &obje)
    32  	require.Equal(t, erre, errs)
    33  	require.Equal(t, obje, objs)
    34  
    35  	errs = sonic.UnmarshalString(`"12x4"`, &objs)
    36  	erre = json.Unmarshal([]byte(`"12x4"`), &obje)
    37  	require.Error(t, errs)
    38  	require.Error(t, erre)
    39  
    40  	errs = sonic.UnmarshalString(`"1234`, &objs)
    41  	erre = json.Unmarshal([]byte(`"1234`), &obje)
    42  	require.Error(t, errs)
    43  	require.Error(t, erre)
    44  
    45  	errs = sonic.UnmarshalString(`1234"`, &objs)
    46  	erre = json.Unmarshal([]byte(`1234"`), &obje)
    47  	require.Error(t, errs)
    48  	require.Error(t, erre)
    49  }