实现json序列化接口后调用原生序列化方法会造成代码死循环而导致stack溢出
代码如下
type test struct{
}
func (t test) MarshalJSON() ([]byte, error){
return json.Marshal(t) // stack over
}
json.Marshal核心代码中包含会调用对象实现的MarshalJSON方法
核心代码如下
type Marshaler interface {
MarshalJSON() ([]byte, error)
}
// newTypeEncoder constructs an encoderFunc for a type.
// The returned encoder only checks CanAddr when allowAddr is true.
func newTypeEncoder(t reflect.Type, allowAddr bool) encoderFunc {
// If we have a non-pointer value whose type implements
// Marshaler with a value receiver, then we're better off taking
// the address of the value - otherwise we end up with an
// allocation as we cast the value to an interface.
if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(marshalerType) {
return newCondAddrEncoder(addrMarshalerEncoder, newTypeEncoder(t, false))
}
if t.Implements(marshalerType) {
return marshalerEncoder
}
if t.Kind() != reflect.Pointer && allowAddr && reflect.PointerTo(t).Implements(textMarshalerType) {
return newCondAddrEncoder(addrTextMarshalerEncoder, newTypeEncoder(t, false))
}
if t.Implements(textMarshalerType) {
return textMarshalerEncoder
}
// 省略 ...
}
func marshalerEncoder(e *encodeState, v reflect.Value, opts encOpts) {
if v.Kind() == reflect.Pointer && v.IsNil() {
e.WriteString("null")
return
}
m, ok := v.Interface().(Marshaler)
if !ok {
e.WriteString("null")
return
}
b, err := m.MarshalJSON() // 调用自身,循环核心问题
if err == nil {
// copy JSON into buffer, checking validity.
err = compact(&e.Buffer, b, opts.escapeHTML)
}
if err != nil {
e.error(&MarshalerError{v.Type(), err, "MarshalJSON"})
}
}
1. 真实现自身序列化 MarshalJSON() ([]byte, error) 方法
2. 打断json.MashalJson循环条件,即m, ok := v.Interface().(Marshaler) 不成立
代码如下
type test struct {
}
//方案1
func (t test) MarshalJSON() ([]byte, error) {
return []byte("{}")
}
//方案2
func (t test) MarshalJSON() ([]byte, error) {
type x test
return json.Marshal(x(t))
}
func main() {
t := test{}
type x test
marshalerType := reflect.TypeOf((*json.Marshaler)(nil)).Elem()
println(reflect.TypeOf(t).Implements(marshalerType)) // true
println(reflect.TypeOf(x(t)).Implements(marshalerType)) //false
}