github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/issue_test/issue258_test.go (about) 1 /* 2 * Copyright 2022 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 "fmt" 22 "testing" 23 24 "github.com/davecgh/go-spew/spew" 25 "github.com/goshafaq/sonic" 26 "github.com/stretchr/testify/require" 27 ) 28 29 type M1 map[string]int 30 31 func (m *M1) MarshalJSON() ([]byte, error) { 32 return []byte(fmt.Sprintf(`{"m":%q}`, spew.Sprintf("%#+v", m))), nil 33 } 34 35 type M2 map[string]int 36 37 func (m M2) MarshalJSON() ([]byte, error) { 38 return []byte(fmt.Sprintf(`{"m":%q}`, spew.Sprintf("%#+v", m))), nil 39 } 40 41 func TestIssue258(t *testing.T) { 42 m1 := M1{} 43 oe, ee := json.Marshal(m1) 44 os, es := sonic.Marshal(m1) 45 require.Equal(t, ee, es) 46 require.Equal(t, oe, os) 47 48 m1p := &M1{} 49 oe, ee = json.Marshal(m1p) 50 os, es = sonic.Marshal(m1p) 51 require.Equal(t, ee, es) 52 require.Equal(t, oe, os) 53 54 m2 := M2{} 55 oe, ee = json.Marshal(m2) 56 os, es = sonic.Marshal(m2) 57 require.Equal(t, ee, es) 58 require.Equal(t, oe, os) 59 60 m2p := &M2{} 61 oe, ee = json.Marshal(m2p) 62 os, es = sonic.Marshal(m2p) 63 require.Equal(t, ee, es) 64 require.Equal(t, oe, os) 65 }