From 20b409ed1f6434868daa97496bb3ffbf53082c4c Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 22 Jan 2021 20:08:57 +0900 Subject: [PATCH] Fix operation of StructFieldOmitEmptyMarshalJSON --- encode_vm.go | 5 ++++- encode_vm_escaped.go | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/encode_vm.go b/encode_vm.go index 7fc7438..14a755c 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -7870,8 +7870,11 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte case opStructFieldOmitEmptyMarshalJSON: ptr := load(ctxptr, code.headIdx) p := ptr + code.offset + if code.typ.Kind() == reflect.Ptr && code.typ.Elem().Implements(marshalJSONType) { + p = e.ptrToPtr(p) + } v := e.ptrToInterface(code, p) - if v != nil && (code.typ.Kind() != reflect.Ptr || e.ptrToPtr(p) != 0) { + if v != nil && 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 d2f9c0f..619c21a 100644 --- a/encode_vm_escaped.go +++ b/encode_vm_escaped.go @@ -7840,8 +7840,11 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, code *opcode) case opStructFieldOmitEmptyMarshalJSON: ptr := load(ctxptr, code.headIdx) p := ptr + code.offset + if code.typ.Kind() == reflect.Ptr && code.typ.Elem().Implements(marshalJSONType) { + p = e.ptrToPtr(p) + } v := e.ptrToInterface(code, p) - if v != nil && (code.typ.Kind() != reflect.Ptr || e.ptrToPtr(p) != 0) { + if v != nil && p != 0 { bb, err := v.(Marshaler).MarshalJSON() if err != nil { return nil, errMarshaler(code, err)