mirror of
https://github.com/ii64/sonic.git
synced 2026-06-20 16:45:22 +08:00
fix: check nil pointer for Marshaler (#194)
This commit is contained in:
parent
ce41dd9636
commit
8caa4eef05
3 changed files with 101 additions and 2 deletions
|
|
@ -839,7 +839,7 @@ func (self *_Compiler) compileMarshaler(p *_Program, op _Op, vt reflect.Type, mt
|
|||
vk := vt.Kind()
|
||||
|
||||
/* direct receiver */
|
||||
if vk != reflect.Ptr || !vt.Elem().Implements(mt) {
|
||||
if vk != reflect.Ptr {
|
||||
p.rtt(op, vt)
|
||||
return
|
||||
}
|
||||
|
|
|
|||
94
issue_test/issue182_test.go
Normal file
94
issue_test/issue182_test.go
Normal file
|
|
@ -0,0 +1,94 @@
|
|||
/*
|
||||
* Copyright 2021 ByteDance Inc.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package issue_test
|
||||
|
||||
import (
|
||||
`encoding/json`
|
||||
`strconv`
|
||||
`testing`
|
||||
|
||||
`github.com/bytedance/sonic`
|
||||
`github.com/stretchr/testify/require`
|
||||
)
|
||||
|
||||
type MarshalerWrap struct {
|
||||
A *ValueMarshaler
|
||||
B ValueMarshaler
|
||||
C *PointerMarshaler
|
||||
D PointerMarshaler
|
||||
|
||||
E *ValueTextMarshaler
|
||||
F ValueTextMarshaler
|
||||
G *PointerTextMarshaler
|
||||
H PointerTextMarshaler
|
||||
}
|
||||
|
||||
type ValueMarshaler struct {
|
||||
X int
|
||||
}
|
||||
|
||||
func (v ValueMarshaler) MarshalJSON() ([]byte, error) {
|
||||
return []byte(strconv.Itoa(v.X)), nil
|
||||
}
|
||||
|
||||
type PointerMarshaler struct {
|
||||
X int
|
||||
}
|
||||
|
||||
func (v *PointerMarshaler) MarshalJSON() ([]byte, error) {
|
||||
return []byte(strconv.Itoa(v.X)), nil
|
||||
}
|
||||
|
||||
type ValueTextMarshaler struct {
|
||||
X int
|
||||
}
|
||||
|
||||
func (v ValueTextMarshaler) MarshalText() ([]byte, error) {
|
||||
return []byte(strconv.Itoa(v.X)), nil
|
||||
}
|
||||
|
||||
type PointerTextMarshaler struct {
|
||||
X int
|
||||
}
|
||||
|
||||
func (v *PointerTextMarshaler) MarshalText() ([]byte, error) {
|
||||
return []byte(strconv.Itoa(v.X)), nil
|
||||
}
|
||||
|
||||
func TestIssue182(t *testing.T) {
|
||||
v0 := MarshalerWrap{}
|
||||
ret, err := json.Marshal(v0)
|
||||
rets, errs := sonic.Marshal(v0)
|
||||
require.Equal(t, err, errs)
|
||||
require.Equal(t, string(ret), string(rets))
|
||||
ret, err = json.Marshal(&v0)
|
||||
rets, errs = sonic.Marshal(&v0)
|
||||
require.Equal(t, err, errs)
|
||||
require.Equal(t, string(ret), string(rets))
|
||||
|
||||
v1 := MarshalerWrap{A:&ValueMarshaler{}, C:&PointerMarshaler{}, E: &ValueTextMarshaler{}, G:&PointerTextMarshaler{}}
|
||||
ret, err = json.Marshal(v1)
|
||||
rets, errs = sonic.Marshal(v1)
|
||||
require.Equal(t, err, errs)
|
||||
require.Equal(t, string(ret), string(rets))
|
||||
ret, err = json.Marshal(&v1)
|
||||
rets, errs = sonic.Marshal(&v1)
|
||||
require.Equal(t, err, errs)
|
||||
require.Equal(t, string(ret), string(rets))
|
||||
}
|
||||
|
||||
|
||||
|
|
@ -19,6 +19,7 @@ package issue_test
|
|||
import (
|
||||
. `github.com/bytedance/sonic`
|
||||
`testing`
|
||||
`encoding/json`
|
||||
|
||||
`github.com/stretchr/testify/require`
|
||||
)
|
||||
|
|
@ -38,5 +39,9 @@ func TestIssue58_NilPointerOnValueMethod(t *testing.T) {
|
|||
}{}
|
||||
buf, err := Marshal(v)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte(`{"X":null,"Y":"pointer"}`), buf)
|
||||
require.Equal(t, []byte(`{"X":null,"Y":null}`), buf)
|
||||
buf, err = json.Marshal(v)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, []byte(`{"X":null,"Y":null}`), buf)
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue