forked from mirror/go-json
Add test case for MarshalJSON type
This commit is contained in:
parent
249c4da6dd
commit
734267f9ae
File diff suppressed because it is too large
Load Diff
|
@ -790,8 +790,8 @@ func encodeTypeToHeaderType(ctx *encodeCompileContext, code *opcode) opType {
|
|||
return opStructFieldHeadArray
|
||||
case opSliceHead:
|
||||
return opStructFieldHeadSlice
|
||||
case opStructFieldHead:
|
||||
return opStructFieldHeadStruct
|
||||
// case opStructFieldHead:
|
||||
// return opStructFieldHeadStruct
|
||||
case opMarshalJSON:
|
||||
return opStructFieldHeadMarshalJSON
|
||||
case opMarshalJSONPtr:
|
||||
|
@ -1264,13 +1264,12 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
|
|||
fieldOpcodeIndex := ctx.opcodeIndex
|
||||
fieldPtrIndex := ctx.ptrIndex
|
||||
ctx.incIndex()
|
||||
nilcheck := indirect
|
||||
nilcheck := indirect //fieldType.Kind() == reflect.Ptr || isPtr && !indirect
|
||||
var valueCode *opcode
|
||||
if i == 0 && fieldNum == 1 && isPtr && rtype_ptrTo(fieldType).Implements(marshalJSONType) && !fieldType.Implements(marshalJSONType) {
|
||||
// *struct{ field implementedMarshalJSONType } => struct { field *implementedMarshalJSONType }
|
||||
// move pointer position from head to first field
|
||||
ctx.typ = rtype_ptrTo(fieldType)
|
||||
code, err := encodeCompileMarshalJSON(ctx)
|
||||
code, err := encodeCompileMarshalJSON(ctx.withType(rtype_ptrTo(fieldType)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
@ -1278,9 +1277,16 @@ func encodeCompileStruct(ctx *encodeCompileContext, isPtr bool) (*opcode, error)
|
|||
nilcheck = false
|
||||
indirect = false
|
||||
disableIndirectConversion = true
|
||||
} else if isPtr && fieldNum > 1 && fieldType.Kind() != reflect.Ptr && !fieldType.Implements(marshalJSONType) && rtype_ptrTo(fieldType).Implements(marshalJSONType) {
|
||||
ctx.typ = rtype_ptrTo(fieldType)
|
||||
code, err := encodeCompileMarshalJSON(ctx)
|
||||
fmt.Println("nilcheck false")
|
||||
} else if isPtr && fieldType.Kind() != reflect.Ptr && !fieldType.Implements(marshalJSONType) && rtype_ptrTo(fieldType).Implements(marshalJSONType) {
|
||||
code, err := encodeCompileMarshalJSON(ctx.withType(rtype_ptrTo(fieldType)))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
nilcheck = false
|
||||
valueCode = code
|
||||
} else if fieldType.Implements(marshalJSONType) && fieldType.Kind() != reflect.Ptr {
|
||||
code, err := encodeCompileMarshalJSON(ctx.withType(fieldType))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
179
encode_vm.go
179
encode_vm.go
|
@ -3222,7 +3222,9 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
code = code.end.next
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldHeadMarshalJSON, opStructFieldHeadStringTagMarshalJSON:
|
||||
p := load(ctxptr, code.idx)
|
||||
|
@ -3234,6 +3236,7 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
}
|
||||
b = append(b, '{')
|
||||
b = append(b, code.key...)
|
||||
fmt.Println("p = ", p, "nilcheck = ", code.nilcheck)
|
||||
if p == 0 || code.nilcheck && code.typ.Kind() == reflect.Ptr && ptrToPtr(p) == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
|
@ -3253,7 +3256,9 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
code = code.end.next
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldHeadOmitEmptyMarshalJSON:
|
||||
p := load(ctxptr, code.idx)
|
||||
|
@ -3306,6 +3311,16 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
}
|
||||
*/
|
||||
}
|
||||
case opStructFieldPtrHeadMarshalJSONPtr, opStructFieldPtrHeadStringTagMarshalJSONPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
fallthrough
|
||||
case opStructFieldHeadMarshalJSONPtr, opStructFieldHeadStringTagMarshalJSONPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
|
@ -3330,6 +3345,16 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
}
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrHeadOmitEmptyMarshalJSONPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
fallthrough
|
||||
case opStructFieldHeadOmitEmptyMarshalJSONPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
|
@ -3357,12 +3382,12 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
case opStructFieldPtrAnonymousHeadMarshalJSON:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
b = encodeComma(b)
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadMarshalJSON:
|
||||
p := load(ctxptr, code.idx)
|
||||
|
@ -3371,13 +3396,103 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
break
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
if p == 0 || code.nilcheck && code.typ.Kind() == reflect.Ptr && ptrToPtr(p) == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = bb
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrAnonymousHeadMarshalJSONPtr, opStructFieldPtrAnonymousHeadStringTagMarshalJSONPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadMarshalJSONPtr, opStructFieldAnonymousHeadStringTagMarshalJSONPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
b = append(b, code.key...)
|
||||
if code.indirect {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
if p == 0 {
|
||||
b = encodeNull(b)
|
||||
} else {
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
}
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrAnonymousHeadOmitEmptyMarshalJSON:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadOmitEmptyMarshalJSON:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if p == 0 || code.nilcheck && code.typ.Kind() == reflect.Ptr && ptrToPtr(p) == 0 {
|
||||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
}
|
||||
case opStructFieldPtrAnonymousHeadOmitEmptyMarshalJSONPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
store(ctxptr, code.idx, ptrToPtr(p))
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadOmitEmptyMarshalJSONPtr:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 && code.indirect {
|
||||
code = code.end.next
|
||||
break
|
||||
}
|
||||
if code.indirect {
|
||||
p = ptrToPtr(p + code.offset)
|
||||
}
|
||||
if p == 0 {
|
||||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
bb, err := encodeMarshalJSON(b, ptrToInterface(code, p+code.offset))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = bb
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
}
|
||||
case opStructFieldPtrHeadMarshalText:
|
||||
p := load(ctxptr, code.idx)
|
||||
if p == 0 {
|
||||
|
@ -3429,52 +3544,6 @@ func encodeRun(ctx *encodeRuntimeContext, b []byte, codeSet *opcodeSet, opt Enco
|
|||
b = bb
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
case opStructFieldPtrAnonymousHeadOmitEmptyMarshalJSON:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr != 0 {
|
||||
store(ctxptr, code.idx, ptrToPtr(ptr))
|
||||
}
|
||||
fallthrough
|
||||
case opStructFieldAnonymousHeadOmitEmptyMarshalJSON:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr == 0 {
|
||||
code = code.end.next
|
||||
} else {
|
||||
ptr += code.offset
|
||||
p := ptrToUnsafePtr(ptr)
|
||||
isPtr := code.typ.Kind() == reflect.Ptr
|
||||
if p == nil || (!isPtr && *(*unsafe.Pointer)(p) == nil) {
|
||||
code = code.nextField
|
||||
} else {
|
||||
v := *(*interface{})(unsafe.Pointer(&interfaceHeader{typ: code.typ, ptr: p}))
|
||||
bb, err := v.(Marshaler).MarshalJSON()
|
||||
if err != nil {
|
||||
return nil, &MarshalerError{
|
||||
Type: rtype2type(code.typ),
|
||||
Err: err,
|
||||
}
|
||||
}
|
||||
if len(bb) == 0 {
|
||||
if isPtr {
|
||||
return nil, errUnexpectedEndOfJSON(
|
||||
fmt.Sprintf("error calling MarshalJSON for type %s", code.typ),
|
||||
0,
|
||||
)
|
||||
}
|
||||
code = code.nextField
|
||||
} else {
|
||||
b = append(b, code.key...)
|
||||
buf := bytes.NewBuffer(b)
|
||||
//TODO: we should validate buffer with `compact`
|
||||
if err := compact(buf, bb, false); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
b = buf.Bytes()
|
||||
b = encodeComma(b)
|
||||
code = code.next
|
||||
}
|
||||
}
|
||||
}
|
||||
case opStructFieldPtrHeadOmitEmptyMarshalText:
|
||||
ptr := load(ctxptr, code.idx)
|
||||
if ptr != 0 {
|
||||
|
|
Loading…
Reference in New Issue