From 305e858ea121708b055918fed395068c0916a91d Mon Sep 17 00:00:00 2001 From: Masaaki Goshima Date: Fri, 22 Jan 2021 13:28:33 +0900 Subject: [PATCH] Fix encoding with nil pointer type of implementing MarshalJSON --- encode_vm.go | 6 ++++++ encode_vm_escaped.go | 6 ++++++ encode_vm_escaped_indent.go | 6 ++++++ encode_vm_indent.go | 6 ++++++ 4 files changed, 24 insertions(+) diff --git a/encode_vm.go b/encode_vm.go index 01f3fa0..9d783c6 100644 --- a/encode_vm.go +++ b/encode_vm.go @@ -267,6 +267,12 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte code = code.next case opMarshalJSON: ptr := load(ctxptr, code.idx) + if ptr == 0 { + b = encodeNull(b) + b = encodeComma(b) + code = code.next + break + } v := e.ptrToInterface(code, ptr) bb, err := v.(Marshaler).MarshalJSON() if err != nil { diff --git a/encode_vm_escaped.go b/encode_vm_escaped.go index 0345338..416d1eb 100644 --- a/encode_vm_escaped.go +++ b/encode_vm_escaped.go @@ -229,6 +229,12 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, code *opcode) code = code.next case opMarshalJSON: ptr := load(ctxptr, code.idx) + if ptr == 0 { + b = encodeNull(b) + b = encodeComma(b) + code = code.next + break + } v := e.ptrToInterface(code, ptr) bb, err := v.(Marshaler).MarshalJSON() if err != nil { diff --git a/encode_vm_escaped_indent.go b/encode_vm_escaped_indent.go index 785c0a2..5adf19e 100644 --- a/encode_vm_escaped_indent.go +++ b/encode_vm_escaped_indent.go @@ -242,6 +242,12 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, code *op code = code.next case opMarshalJSON: ptr := load(ctxptr, code.idx) + if ptr == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.next + break + } v := e.ptrToInterface(code, ptr) bb, err := v.(Marshaler).MarshalJSON() if err != nil { diff --git a/encode_vm_indent.go b/encode_vm_indent.go index 3507a46..ad9c30e 100644 --- a/encode_vm_indent.go +++ b/encode_vm_indent.go @@ -242,6 +242,12 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, code *opcode) ( code = code.next case opMarshalJSON: ptr := load(ctxptr, code.idx) + if ptr == 0 { + b = encodeNull(b) + b = encodeIndentComma(b) + code = code.next + break + } v := e.ptrToInterface(code, ptr) bb, err := v.(Marshaler).MarshalJSON() if err != nil {