Fix encoding of MarshalJSON type

This commit is contained in:
Masaaki Goshima 2021-02-01 18:43:28 +09:00
parent c410c7e5fa
commit 0297427ef5
1 changed files with 43 additions and 28 deletions

View File

@ -7801,6 +7801,15 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o
ptr := load(ctxptr, code.headIdx) ptr := load(ctxptr, code.headIdx)
b = append(b, code.escapedKey...) b = append(b, code.escapedKey...)
p := ptr + code.offset 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) v := ptrToInterface(code, p)
bb, err := v.(Marshaler).MarshalJSON() bb, err := v.(Marshaler).MarshalJSON()
if err != nil { if err != nil {
@ -7816,23 +7825,25 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o
case opStructFieldOmitEmptyMarshalJSON: case opStructFieldOmitEmptyMarshalJSON:
ptr := load(ctxptr, code.headIdx) ptr := load(ctxptr, code.headIdx)
p := ptr + code.offset p := ptr + code.offset
if code.typ.Kind() == reflect.Ptr && code.typ.Elem().Implements(marshalJSONType) { if code.typ.Kind() == reflect.Ptr {
p = ptrToPtr(p) p = ptrToPtr(p)
} }
v := ptrToInterface(code, p) if p == 0 {
if v != nil && p != 0 { code = code.next
bb, err := v.(Marshaler).MarshalJSON() break
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)
} }
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 code = code.next
case opStructFieldStringTagMarshalJSON: case opStructFieldStringTagMarshalJSON:
ptr := load(ctxptr, code.headIdx) ptr := load(ctxptr, code.headIdx)
@ -9256,20 +9267,10 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o
case opStructEndOmitEmptyMarshalJSON: case opStructEndOmitEmptyMarshalJSON:
ptr := load(ctxptr, code.headIdx) ptr := load(ctxptr, code.headIdx)
p := ptr + code.offset p := ptr + code.offset
v := ptrToInterface(code, p) if code.typ.Kind() == reflect.Ptr {
if v != nil && (code.typ.Kind() != reflect.Ptr || ptrToPtr(p) != 0) { p = ptrToPtr(p)
bb, err := v.(Marshaler).MarshalJSON() }
if err != nil { if p == 0 {
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 {
last := len(b) - 1 last := len(b) - 1
if b[last] == ',' { if b[last] == ',' {
b[last] = '}' b[last] = '}'
@ -9277,7 +9278,21 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o
} else { } else {
b = appendStructEnd(b) 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 code = code.next
case opStructEndStringTagMarshalJSON: case opStructEndStringTagMarshalJSON:
ptr := load(ctxptr, code.headIdx) ptr := load(ctxptr, code.headIdx)