From 0297427ef56e1b6b8aa672f0bb71f50e2155ce63 Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Mon, 1 Feb 2021 18:43:28 +0900 Subject: [PATCH] Fix encoding of MarshalJSON type --- encode_vm_escaped.go | 71 +++++++++++++++++++++++++++----------------- 1 file changed, 43 insertions(+), 28 deletions(-) diff --git a/encode_vm_escaped.go b/encode_vm_escaped.go index 7bc194f..47f6486 100644 --- a/encode_vm_escaped.go +++ b/encode_vm_escaped.go @@ -7801,6 +7801,15 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o ptr := load(ctxptr, code.headIdx) b = append(b, code.escapedKey...) p := ptr + code.offset + if code.typ.Kind() == reflect.Ptr { + p = ptrToPtr(p) + } + if p == 0 { + b = encodeNull(b) + b = encodeComma(b) + code = code.next + break + } v := ptrToInterface(code, p) bb, err := v.(Marshaler).MarshalJSON() if err != nil { @@ -7816,23 +7825,25 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o case opStructFieldOmitEmptyMarshalJSON: ptr := load(ctxptr, code.headIdx) p := ptr + code.offset - if code.typ.Kind() == reflect.Ptr && code.typ.Elem().Implements(marshalJSONType) { + if code.typ.Kind() == reflect.Ptr { p = ptrToPtr(p) } - v := ptrToInterface(code, p) - if v != nil && p != 0 { - bb, err := v.(Marshaler).MarshalJSON() - if err != nil { - return nil, errMarshaler(code, err) - } - b = append(b, code.escapedKey...) - buf := bytes.NewBuffer(b) - if err := compact(buf, bb, true); err != nil { - return nil, err - } - b = buf.Bytes() - b = encodeComma(b) + if p == 0 { + code = code.next + break } + v := ptrToInterface(code, p) + bb, err := v.(Marshaler).MarshalJSON() + if err != nil { + return nil, errMarshaler(code, err) + } + b = append(b, code.escapedKey...) + buf := bytes.NewBuffer(b) + if err := compact(buf, bb, true); err != nil { + return nil, err + } + b = buf.Bytes() + b = encodeComma(b) code = code.next case opStructFieldStringTagMarshalJSON: ptr := load(ctxptr, code.headIdx) @@ -9256,20 +9267,10 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o case opStructEndOmitEmptyMarshalJSON: ptr := load(ctxptr, code.headIdx) p := ptr + code.offset - v := ptrToInterface(code, p) - if v != nil && (code.typ.Kind() != reflect.Ptr || ptrToPtr(p) != 0) { - bb, err := v.(Marshaler).MarshalJSON() - if err != nil { - return nil, errMarshaler(code, err) - } - b = append(b, code.escapedKey...) - buf := bytes.NewBuffer(b) - if err := compact(buf, bb, true); err != nil { - return nil, err - } - b = buf.Bytes() - b = appendStructEnd(b) - } else { + if code.typ.Kind() == reflect.Ptr { + p = ptrToPtr(p) + } + if p == 0 { last := len(b) - 1 if b[last] == ',' { b[last] = '}' @@ -9277,7 +9278,21 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o } else { b = appendStructEnd(b) } + code = code.next + break } + v := ptrToInterface(code, p) + bb, err := v.(Marshaler).MarshalJSON() + if err != nil { + return nil, errMarshaler(code, err) + } + b = append(b, code.escapedKey...) + buf := bytes.NewBuffer(b) + if err := compact(buf, bb, true); err != nil { + return nil, err + } + b = buf.Bytes() + b = appendStructEnd(b) code = code.next case opStructEndStringTagMarshalJSON: ptr := load(ctxptr, code.headIdx)