Keep reference value while encoding by MarshalJSON

This commit is contained in:
Masaaki Goshima 2021-01-22 16:28:12 +09:00
parent d4b5171fdf
commit d933310ad4
4 changed files with 8 additions and 0 deletions

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
"runtime"
"sort" "sort"
"strconv" "strconv"
"unsafe" "unsafe"
@ -278,6 +279,7 @@ func (e *Encoder) run(ctx *encodeRuntimeContext, b []byte, code *opcode) ([]byte
if err != nil { if err != nil {
return nil, errMarshaler(code, err) return nil, errMarshaler(code, err)
} }
runtime.KeepAlive(v)
if len(bb) == 0 { if len(bb) == 0 {
return nil, errUnexpectedEndOfJSON( return nil, errUnexpectedEndOfJSON(
fmt.Sprintf("error calling MarshalJSON for type %s", code.typ), fmt.Sprintf("error calling MarshalJSON for type %s", code.typ),

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
"runtime"
"sort" "sort"
"unsafe" "unsafe"
) )
@ -240,6 +241,7 @@ func (e *Encoder) runEscaped(ctx *encodeRuntimeContext, b []byte, code *opcode)
if err != nil { if err != nil {
return nil, errMarshaler(code, err) return nil, errMarshaler(code, err)
} }
runtime.KeepAlive(v)
if len(bb) == 0 { if len(bb) == 0 {
return nil, errUnexpectedEndOfJSON( return nil, errUnexpectedEndOfJSON(
fmt.Sprintf("error calling MarshalJSON for type %s", code.typ), fmt.Sprintf("error calling MarshalJSON for type %s", code.typ),

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
"runtime"
"sort" "sort"
"unsafe" "unsafe"
) )
@ -253,6 +254,7 @@ func (e *Encoder) runEscapedIndent(ctx *encodeRuntimeContext, b []byte, code *op
if err != nil { if err != nil {
return nil, errMarshaler(code, err) return nil, errMarshaler(code, err)
} }
runtime.KeepAlive(v)
if len(bb) == 0 { if len(bb) == 0 {
return nil, errUnexpectedEndOfJSON( return nil, errUnexpectedEndOfJSON(
fmt.Sprintf("error calling MarshalJSON for type %s", code.typ), fmt.Sprintf("error calling MarshalJSON for type %s", code.typ),

View File

@ -6,6 +6,7 @@ import (
"fmt" "fmt"
"math" "math"
"reflect" "reflect"
"runtime"
"sort" "sort"
"unsafe" "unsafe"
) )
@ -253,6 +254,7 @@ func (e *Encoder) runIndent(ctx *encodeRuntimeContext, b []byte, code *opcode) (
if err != nil { if err != nil {
return nil, errMarshaler(code, err) return nil, errMarshaler(code, err)
} }
runtime.KeepAlive(v)
if len(bb) == 0 { if len(bb) == 0 {
return nil, errUnexpectedEndOfJSON( return nil, errUnexpectedEndOfJSON(
fmt.Sprintf("error calling MarshalJSON for type %s", code.typ), fmt.Sprintf("error calling MarshalJSON for type %s", code.typ),