forked from mirror/go-json
Fix encoding of MarshalJSON type
This commit is contained in:
parent
c410c7e5fa
commit
0297427ef5
|
@ -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,11 +7825,14 @@ 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)
|
||||||
}
|
}
|
||||||
|
if p == 0 {
|
||||||
|
code = code.next
|
||||||
|
break
|
||||||
|
}
|
||||||
v := ptrToInterface(code, p)
|
v := ptrToInterface(code, p)
|
||||||
if v != nil && p != 0 {
|
|
||||||
bb, err := v.(Marshaler).MarshalJSON()
|
bb, err := v.(Marshaler).MarshalJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errMarshaler(code, err)
|
return nil, errMarshaler(code, err)
|
||||||
|
@ -7832,7 +7844,6 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o
|
||||||
}
|
}
|
||||||
b = buf.Bytes()
|
b = buf.Bytes()
|
||||||
b = encodeComma(b)
|
b = encodeComma(b)
|
||||||
}
|
|
||||||
code = code.next
|
code = code.next
|
||||||
case opStructFieldStringTagMarshalJSON:
|
case opStructFieldStringTagMarshalJSON:
|
||||||
ptr := load(ctxptr, code.headIdx)
|
ptr := load(ctxptr, code.headIdx)
|
||||||
|
@ -9256,8 +9267,21 @@ 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
|
||||||
|
if code.typ.Kind() == reflect.Ptr {
|
||||||
|
p = ptrToPtr(p)
|
||||||
|
}
|
||||||
|
if p == 0 {
|
||||||
|
last := len(b) - 1
|
||||||
|
if b[last] == ',' {
|
||||||
|
b[last] = '}'
|
||||||
|
b = encodeComma(b)
|
||||||
|
} else {
|
||||||
|
b = appendStructEnd(b)
|
||||||
|
}
|
||||||
|
code = code.next
|
||||||
|
break
|
||||||
|
}
|
||||||
v := ptrToInterface(code, p)
|
v := ptrToInterface(code, p)
|
||||||
if v != nil && (code.typ.Kind() != reflect.Ptr || ptrToPtr(p) != 0) {
|
|
||||||
bb, err := v.(Marshaler).MarshalJSON()
|
bb, err := v.(Marshaler).MarshalJSON()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errMarshaler(code, err)
|
return nil, errMarshaler(code, err)
|
||||||
|
@ -9269,15 +9293,6 @@ func encodeRunEscaped(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, o
|
||||||
}
|
}
|
||||||
b = buf.Bytes()
|
b = buf.Bytes()
|
||||||
b = appendStructEnd(b)
|
b = appendStructEnd(b)
|
||||||
} else {
|
|
||||||
last := len(b) - 1
|
|
||||||
if b[last] == ',' {
|
|
||||||
b[last] = '}'
|
|
||||||
b = encodeComma(b)
|
|
||||||
} else {
|
|
||||||
b = appendStructEnd(b)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
code = code.next
|
code = code.next
|
||||||
case opStructEndStringTagMarshalJSON:
|
case opStructEndStringTagMarshalJSON:
|
||||||
ptr := load(ctxptr, code.headIdx)
|
ptr := load(ctxptr, code.headIdx)
|
||||||
|
|
Loading…
Reference in New Issue