From fa4d5842e6e0c71d6dba5e55e0db6b923bed2882 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 22 Jan 2021 18:26:28 +0900 Subject: [PATCH] Fix encoding of pointered MarshalJSON type with omitempty --- encode_vm.go | 4 ++-- encode_vm_escaped.go | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/encode_vm.go b/encode_vm.go index 58c2c95..694ff17 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -7871,7 +7871,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte ptr := load(ctxptr, code.headIdx) p := ptr + code.offset v := e.ptrToInterface(code, p) - if v != nil { + if v != nil && (code.typ.Kind() != reflect.Ptr || e.ptrToPtr(p) != 0) { bb, err := v.(Marshaler).MarshalJSON() if err != nil { return nil, errMarshaler(code, err) @@ -9060,7 +9060,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte ptr := load(ctxptr, code.headIdx) p := ptr + code.offset v := e.ptrToInterface(code, p) - if v != nil { + if v != nil && (code.typ.Kind() != reflect.Ptr || e.ptrToPtr(p) != 0) { bb, err := v.(Marshaler).MarshalJSON() if err != nil { return nil, errMarshaler(code, err) diff --git a/encode_vm_escaped.go b/encode_vm_escaped.go index bef089c..839da2c 100644 --- a/encode_vm_escaped.go +++ b/encode_vm_escaped.go @@ -7841,7 +7841,7 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, code *opcode) ptr := load(ctxptr, code.headIdx) p := ptr + code.offset v := e.ptrToInterface(code, p) - if v != nil { + if v != nil && (code.typ.Kind() != reflect.Ptr || e.ptrToPtr(p) != 0) { bb, err := v.(Marshaler).MarshalJSON() if err != nil { return nil, errMarshaler(code, err) @@ -9046,7 +9046,7 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, code *opcode) ptr := load(ctxptr, code.headIdx) p := ptr + code.offset v := e.ptrToInterface(code, p) - if v != nil { + if v != nil && (code.typ.Kind() != reflect.Ptr || e.ptrToPtr(p) != 0) { bb, err := v.(Marshaler).MarshalJSON() if err != nil { return nil, errMarshaler(code, err)