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 {