...
1
16
17 package issue_test
18
19 import (
20 `encoding/json`
21 `strconv`
22 `testing`
23
24 `github.com/bytedance/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 }
93
94
95
View as plain text