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