github.com/goshafaq/sonic@v0.0.0-20231026082336-871835fb94c6/issue_test/issue182_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  	"strconv"
    22  	"testing"
    23  
    24  	"github.com/goshafaq/sonic"
    25  	"github.com/stretchr/testify/require"
    26  )
    27  
    28  type MarshalerWrap struct {
    29  	A *ValueMarshaler
    30  	B ValueMarshaler
    31  	C *PointerMarshaler
    32  	D PointerMarshaler
    33  
    34  	E *ValueTextMarshaler
    35  	F ValueTextMarshaler
    36  	G *PointerTextMarshaler
    37  	H PointerTextMarshaler
    38  }
    39  
    40  type ValueMarshaler struct {
    41  	X int
    42  }
    43  
    44  func (v ValueMarshaler) MarshalJSON() ([]byte, error) {
    45  	return []byte(strconv.Itoa(v.X)), nil
    46  }
    47  
    48  type PointerMarshaler struct {
    49  	X int
    50  }
    51  
    52  func (v *PointerMarshaler) MarshalJSON() ([]byte, error) {
    53  	return []byte(strconv.Itoa(v.X)), nil
    54  }
    55  
    56  type ValueTextMarshaler struct {
    57  	X int
    58  }
    59  
    60  func (v ValueTextMarshaler) MarshalText() ([]byte, error) {
    61  	return []byte(strconv.Itoa(v.X)), nil
    62  }
    63  
    64  type PointerTextMarshaler struct {
    65  	X int
    66  }
    67  
    68  func (v *PointerTextMarshaler) MarshalText() ([]byte, error) {
    69  	return []byte(strconv.Itoa(v.X)), nil
    70  }
    71  
    72  func TestIssue182(t *testing.T) {
    73  	v0 := MarshalerWrap{}
    74  	ret, err := json.Marshal(v0)
    75  	rets, errs := sonic.Marshal(v0)
    76  	require.Equal(t, err, errs)
    77  	require.Equal(t, string(ret), string(rets))
    78  	ret, err = json.Marshal(&v0)
    79  	rets, errs = sonic.Marshal(&v0)
    80  	require.Equal(t, err, errs)
    81  	require.Equal(t, string(ret), string(rets))
    82  
    83  	v1 := MarshalerWrap{A: &ValueMarshaler{}, C: &PointerMarshaler{}, E: &ValueTextMarshaler{}, G: &PointerTextMarshaler{}}
    84  	ret, err = json.Marshal(v1)
    85  	rets, errs = sonic.Marshal(v1)
    86  	require.Equal(t, err, errs)
    87  	require.Equal(t, string(ret), string(rets))
    88  	ret, err = json.Marshal(&v1)
    89  	rets, errs = sonic.Marshal(&v1)
    90  	require.Equal(t, err, errs)
    91  	require.Equal(t, string(ret), string(rets))
    92  }