github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/issue_test/issue144_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 "encoding/json" 21 "testing" 22 23 "github.com/davecgh/go-spew/spew" 24 "github.com/goshafaq/sonic" 25 "github.com/stretchr/testify/require" 26 ) 27 28 type Issue144_StringOption struct { 29 S1 *string `json:"s1,string"` 30 S2 *string `json:"s2,string"` 31 S3 string `json:"s3,string"` 32 J1 json.Number `json:"j1,string"` 33 J2 *json.Number `json:"j2,string"` 34 J3 *json.Number `json:"j3,string"` 35 I1 int `json:"i1,string"` 36 I2 *int `json:"i2,string"` 37 I3 *int `json:"i3,string"` 38 } 39 40 func TestIssue144_StringOption(t *testing.T) { 41 data := []byte(`{ 42 "s1":"\"null\"", 43 "s2":"null", 44 "s3":"null", 45 "j1":"null", 46 "j2":"null", 47 "j3":"123.456", 48 "i1":"null", 49 "i2":"null", 50 "i3":"-123" 51 }`) 52 53 var v1, v2 Issue144_StringOption 54 e1 := json.Unmarshal(data, &v1) 55 e2 := sonic.Unmarshal(data, &v2) 56 require.NoError(t, e1) 57 require.NoError(t, e2) 58 require.Equal(t, v1, v2) 59 spew.Dump(v1) 60 61 i, j, s := int(1), json.Number("1"), "null" 62 v1.I2, v2.I2 = &i, &i 63 v1.J2, v2.J2 = &j, &j 64 v1.S2, v2.S2 = &s, &s 65 66 e1 = json.Unmarshal(data, &v1) 67 e2 = sonic.Unmarshal(data, &v2) 68 require.NoError(t, e1) 69 require.NoError(t, e2) 70 require.Equal(t, v1, v2) 71 spew.Dump(v1) 72 }