From 53590387e6f1a043389f54a49ba5ef71afa2efd1 Mon Sep 17 00:00:00 2001 From: IncSW Date: Mon, 12 Apr 2021 11:41:31 +0300 Subject: [PATCH] fix OpStructFieldOmitEmptyMarshalJSON with custom type marshaller on all VMs --- internal/encoder/vm/vm.go | 7 ++++++- internal/encoder/vm_escaped/vm.go | 7 ++++++- internal/encoder/vm_escaped_indent/vm.go | 7 ++++++- internal/encoder/vm_indent/vm.go | 7 ++++++- 4 files changed, 24 insertions(+), 4 deletions(-) diff --git a/internal/encoder/vm/vm.go b/internal/encoder/vm/vm.go index 6522548..b4b80b6 100644 --- a/internal/encoder/vm/vm.go +++ b/internal/encoder/vm/vm.go @@ -3646,8 +3646,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField break } + iface := ptrToInterface(code, p) + if code.Nilcheck && encoder.IsNilForMarshaler(iface) { + code = code.NextField + break + } b = append(b, code.Key...) - bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), false) + bb, err := appendMarshalJSON(code, b, iface, false) if err != nil { return nil, err } diff --git a/internal/encoder/vm_escaped/vm.go b/internal/encoder/vm_escaped/vm.go index b0c608f..846119b 100644 --- a/internal/encoder/vm_escaped/vm.go +++ b/internal/encoder/vm_escaped/vm.go @@ -3646,8 +3646,13 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField break } + iface := ptrToInterface(code, p) + if code.Nilcheck && encoder.IsNilForMarshaler(iface) { + code = code.NextField + break + } b = append(b, code.EscapedKey...) - bb, err := appendMarshalJSON(code, b, ptrToInterface(code, p), true) + bb, err := appendMarshalJSON(code, b, iface, true) if err != nil { return nil, err } diff --git a/internal/encoder/vm_escaped_indent/vm.go b/internal/encoder/vm_escaped_indent/vm.go index 00c03d9..bcddbc1 100644 --- a/internal/encoder/vm_escaped_indent/vm.go +++ b/internal/encoder/vm_escaped_indent/vm.go @@ -3901,10 +3901,15 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField break } + iface := ptrToInterface(code, p) + if code.Nilcheck && encoder.IsNilForMarshaler(iface) { + code = code.NextField + break + } b = appendIndent(ctx, b, code.Indent) b = append(b, code.EscapedKey...) b = append(b, ' ') - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent+1, true) + bb, err := appendMarshalJSON(ctx, code, b, iface, code.Indent+1, true) if err != nil { return nil, err } diff --git a/internal/encoder/vm_indent/vm.go b/internal/encoder/vm_indent/vm.go index 518ddaf..eddffcc 100644 --- a/internal/encoder/vm_indent/vm.go +++ b/internal/encoder/vm_indent/vm.go @@ -3901,10 +3901,15 @@ func Run(ctx *encoder.RuntimeContext, b []byte, codeSet *encoder.OpcodeSet, opt code = code.NextField break } + iface := ptrToInterface(code, p) + if code.Nilcheck && encoder.IsNilForMarshaler(iface) { + code = code.NextField + break + } b = appendIndent(ctx, b, code.Indent) b = append(b, code.Key...) b = append(b, ' ') - bb, err := appendMarshalJSON(ctx, code, b, ptrToInterface(code, p), code.Indent+1, false) + bb, err := appendMarshalJSON(ctx, code, b, iface, code.Indent+1, false) if err != nil { return nil, err }