github.com/bytedance/sonic@v1.11.7-0.20240517092252-d2edb31b167b/issue_test/issue390_test.go (about)

     1  /*
     2   * Copyright 2023 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      `github.com/bytedance/sonic`
    22      `encoding/json`
    23      `github.com/stretchr/testify/require`
    24  )
    25  
    26  type DirectStruct struct {
    27      Ptr *int
    28  }
    29  
    30  func (d DirectStruct) MarshalJSON() ([]byte, error) {
    31      return json.Marshal((d.Ptr))
    32  }
    33  
    34  type DirectArray [1]*int
    35  
    36  func (d DirectArray) MarshalJSON() ([]byte, error) {
    37      return json.Marshal(d[0])
    38  }
    39  
    40  type DirectNested [1]DirectStruct
    41  
    42  func (d DirectNested) MarshalJSON() ([]byte, error) {
    43      return json.Marshal(d[0])
    44  }
    45  
    46  
    47  type DirectStruct2 struct {
    48      Ptr *int
    49  }
    50  
    51  func (d DirectStruct2) MarshalText() ([]byte, error) {
    52      return json.Marshal((d.Ptr))
    53  }
    54  
    55  type DirectArray2 [1]*int
    56  
    57  func (d DirectArray2) MarshalText() ([]byte, error) {
    58      return json.Marshal(d[0])
    59  }
    60  
    61  type DirectNested2 [1]DirectStruct2
    62  
    63  func (d DirectNested2) MarshalText() ([]byte, error) {
    64      return json.Marshal(d[0])
    65  }
    66  
    67  func TestDirectStructType(t *testing.T) {
    68      val  := 123
    69      real := &val
    70      realds := DirectStruct{real}
    71      nullds := DirectStruct{}
    72      realda := DirectArray{real}
    73      nullda := DirectArray{}
    74      nested := DirectNested{realds}
    75  
    76      realds2 := DirectStruct2{real}
    77      nullds2 := DirectStruct2{}
    78      realda2 := DirectArray2{real}
    79      nullda2 := DirectArray2{}
    80      nested2 := DirectNested2{realds2}
    81  
    82      tests := []interface{} {
    83          // test direct iface type implemented encoding.JSONMarshaler
    84          &realds, realds, &nullds, nullds,
    85          map[string]DirectStruct{ "a": realds, "b": nullds},
    86          map[string]*DirectStruct{ "a": &realds, "b": &nullds},
    87          []DirectStruct{realds, nullds},
    88          []*DirectStruct{&realds, &nullds},
    89          &realda, realda, &nullda, nullda,
    90          nested, &nested,
    91  
    92          // test direct iface implemented encoding.TextMarshaler
    93          &realds2, realds2, &nullds2, nullds2,
    94          map[string]DirectStruct2{ "a": realds2, "b": nullds2},
    95          map[string]*DirectStruct2{ "a": &realds2, "b": &nullds2},
    96          []DirectStruct2{realds2, nullds2},
    97          []*DirectStruct2{&realds2, &nullds2},
    98          &realda2, realda2, &nullda2, nullda2,
    99          nested2, &nested2,
   100  
   101          // test map key implement encoding.TextMarshaler
   102          map[DirectStruct2]DirectArray{
   103              realds2 : realda,
   104              nullds2 : nullda,
   105          },
   106      }
   107      for _, tt := range tests {
   108          jout, jerr := json.Marshal(tt)
   109          sout, serr := sonic.ConfigStd.Marshal(tt)
   110          require.Equal(t, string(jout), string(sout))
   111          require.NoError(t, jerr)
   112          require.Equal(t, jerr, serr)
   113      }
   114  }
   115  
   116  
   117  type recurePtr struct {
   118      Name string
   119      Recur *recurePtr
   120  }
   121  
   122  func TestRecursiveIssue(t *testing.T) {
   123      data := `{
   124          "Name": "",
   125          "Recur": null
   126      }`
   127      jv, sv := recurePtr{}, recurePtr{}
   128      jerr := json.Unmarshal([]byte(data), &jv)
   129      serr := sonic.Unmarshal([]byte(data), &sv)
   130      require.Equal(t, jv, sv)
   131      require.Equal(t, jerr, serr)
   132  }
   133