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