go-json/decode_interface.go

351 lines
8.8 KiB
Go
Raw Permalink Normal View History

2020-05-08 09:13:30 +03:00
package json
import (
2021-01-22 07:28:03 +03:00
"encoding"
2020-05-08 09:13:30 +03:00
"reflect"
"unsafe"
)
type interfaceDecoder struct {
typ *rtype
structName string
fieldName string
2021-02-09 15:37:18 +03:00
dec *Decoder
2020-05-08 09:13:30 +03:00
}
2021-02-09 15:37:18 +03:00
func newInterfaceDecoder(dec *Decoder, typ *rtype, structName, fieldName string) *interfaceDecoder {
2020-08-11 12:04:32 +03:00
return &interfaceDecoder{
typ: typ,
structName: structName,
fieldName: fieldName,
2021-02-09 15:37:18 +03:00
dec: dec,
2020-08-11 12:04:32 +03:00
}
}
func (d *interfaceDecoder) numDecoder(s *stream) decoder {
if s.useNumber {
return newNumberDecoder(d.structName, d.fieldName, func(p unsafe.Pointer, v Number) {
2020-11-19 06:47:42 +03:00
*(*interface{})(p) = v
2020-08-11 12:04:32 +03:00
})
}
return newFloatDecoder(d.structName, d.fieldName, func(p unsafe.Pointer, v float64) {
2020-11-19 06:47:42 +03:00
*(*interface{})(p) = v
2020-08-11 12:04:32 +03:00
})
2020-05-08 09:13:30 +03:00
}
var (
interfaceMapType = type2rtype(
2020-08-25 05:17:38 +03:00
reflect.TypeOf((*map[string]interface{})(nil)).Elem(),
2020-05-08 09:13:30 +03:00
)
2020-12-22 15:55:59 +03:00
stringType = type2rtype(
reflect.TypeOf(""),
)
2020-05-08 09:13:30 +03:00
)
2021-02-09 16:13:58 +03:00
func decodeStreamUnmarshaler(s *stream, unmarshaler Unmarshaler) error {
2021-01-22 07:28:03 +03:00
start := s.cursor
if err := s.skipValue(); err != nil {
return err
}
src := s.buf[start:s.cursor]
dst := make([]byte, len(src))
copy(dst, src)
if err := unmarshaler.UnmarshalJSON(dst); err != nil {
return err
}
return nil
}
2021-02-09 16:13:58 +03:00
func decodeStreamTextUnmarshaler(s *stream, unmarshaler encoding.TextUnmarshaler) error {
2021-01-22 07:28:03 +03:00
start := s.cursor
if err := s.skipValue(); err != nil {
return err
}
src := s.buf[start:s.cursor]
dst := make([]byte, len(src))
copy(dst, src)
if err := unmarshaler.UnmarshalText(dst); err != nil {
return err
}
return nil
}
2021-02-09 16:13:58 +03:00
func (d *interfaceDecoder) decodeStreamEmptyInterface(s *stream, p unsafe.Pointer) error {
2020-07-30 16:41:53 +03:00
s.skipWhiteSpace()
2020-07-31 11:10:03 +03:00
for {
switch s.char() {
case '{':
2020-08-25 05:17:38 +03:00
var v map[string]interface{}
2020-07-31 11:10:03 +03:00
ptr := unsafe.Pointer(&v)
2020-08-14 11:59:49 +03:00
if err := newMapDecoder(
2020-08-11 12:04:32 +03:00
interfaceMapType,
2020-12-22 15:55:59 +03:00
stringType,
newStringDecoder(d.structName, d.fieldName),
2020-12-22 15:55:59 +03:00
interfaceMapType.Elem(),
2021-02-09 15:37:18 +03:00
newInterfaceDecoder(d.dec, d.typ, d.structName, d.fieldName),
d.structName,
d.fieldName,
2020-11-19 06:47:42 +03:00
).decodeStream(s, ptr); err != nil {
2020-07-31 11:10:03 +03:00
return err
}
2020-12-05 16:27:33 +03:00
*(*interface{})(p) = v
2020-07-31 11:10:03 +03:00
return nil
case '[':
var v []interface{}
ptr := unsafe.Pointer(&v)
2020-08-14 11:59:49 +03:00
if err := newSliceDecoder(
2021-02-09 15:37:18 +03:00
newInterfaceDecoder(d.dec, d.typ, d.structName, d.fieldName),
2020-08-11 12:04:32 +03:00
d.typ,
d.typ.Size(),
d.structName,
d.fieldName,
2020-11-19 06:47:42 +03:00
).decodeStream(s, ptr); err != nil {
2020-07-31 11:10:03 +03:00
return err
}
2020-12-05 16:27:33 +03:00
*(*interface{})(p) = v
2020-07-31 11:10:03 +03:00
return nil
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
2020-08-11 12:04:32 +03:00
return d.numDecoder(s).decodeStream(s, p)
2020-07-31 11:10:03 +03:00
case '"':
s.cursor++
start := s.cursor
for {
switch s.char() {
case '\\':
2020-08-14 10:00:07 +03:00
if err := decodeEscapeString(s); err != nil {
return err
}
2020-07-31 11:10:03 +03:00
case '"':
literal := s.buf[start:s.cursor]
s.cursor++
2020-12-05 16:27:33 +03:00
*(*interface{})(p) = string(literal)
2020-07-31 11:10:03 +03:00
return nil
case nul:
if s.read() {
continue
}
return errUnexpectedEndOfJSON("string", s.totalOffset())
}
s.cursor++
}
case 't':
if err := trueBytes(s); err != nil {
return err
}
**(**interface{})(unsafe.Pointer(&p)) = true
2020-07-31 11:10:03 +03:00
return nil
case 'f':
if err := falseBytes(s); err != nil {
return err
}
**(**interface{})(unsafe.Pointer(&p)) = false
2020-07-31 11:10:03 +03:00
return nil
case 'n':
if err := nullBytes(s); err != nil {
return err
}
2020-12-05 16:27:33 +03:00
*(*interface{})(p) = nil
2020-07-31 11:10:03 +03:00
return nil
case nul:
if s.read() {
continue
2020-07-30 16:41:53 +03:00
}
}
2020-07-31 11:10:03 +03:00
break
2020-07-30 16:41:53 +03:00
}
return errNotAtBeginningOfValue(s.totalOffset())
}
2021-02-09 15:37:18 +03:00
func (d *interfaceDecoder) decodeStream(s *stream, p unsafe.Pointer) error {
runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: d.typ,
ptr: p,
}))
rv := reflect.ValueOf(runtimeInterfaceValue)
if rv.NumMethod() > 0 && rv.CanInterface() {
if u, ok := rv.Interface().(Unmarshaler); ok {
2021-02-09 16:13:58 +03:00
return decodeStreamUnmarshaler(s, u)
2021-02-09 15:37:18 +03:00
}
if u, ok := rv.Interface().(encoding.TextUnmarshaler); ok {
2021-02-09 16:13:58 +03:00
return decodeStreamTextUnmarshaler(s, u)
2021-02-09 15:37:18 +03:00
}
return nil
}
iface := rv.Interface()
ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface))
typ := ifaceHeader.typ
if d.typ == typ || typ == nil {
// concrete type is empty interface
2021-02-09 16:13:58 +03:00
return d.decodeStreamEmptyInterface(s, p)
}
if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr {
return d.decodeStreamEmptyInterface(s, p)
}
if s.char() == 'n' {
if err := nullBytes(s); err != nil {
return err
}
*(*interface{})(p) = nil
return nil
2021-02-09 15:37:18 +03:00
}
decoder, err := d.dec.compileToGetDecoder(uintptr(unsafe.Pointer(typ)), typ)
if err != nil {
return err
}
return decoder.decodeStream(s, ifaceHeader.ptr)
}
2020-11-19 06:47:42 +03:00
func (d *interfaceDecoder) decode(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
2021-02-09 16:13:58 +03:00
runtimeInterfaceValue := *(*interface{})(unsafe.Pointer(&interfaceHeader{
typ: d.typ,
ptr: p,
}))
rv := reflect.ValueOf(runtimeInterfaceValue)
iface := rv.Interface()
ifaceHeader := (*interfaceHeader)(unsafe.Pointer(&iface))
typ := ifaceHeader.typ
if d.typ == typ || typ == nil {
// concrete type is empty interface
return d.decodeEmptyInterface(buf, cursor, p)
}
if typ.Kind() == reflect.Ptr && typ.Elem() == d.typ || typ.Kind() != reflect.Ptr {
return d.decodeEmptyInterface(buf, cursor, p)
}
if buf[cursor] == 'n' {
if cursor+3 >= int64(len(buf)) {
return 0, errUnexpectedEndOfJSON("null", cursor)
}
if buf[cursor+1] != 'u' {
return 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
}
if buf[cursor+2] != 'l' {
return 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
}
if buf[cursor+3] != 'l' {
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
}
cursor += 4
**(**interface{})(unsafe.Pointer(&p)) = nil
return cursor, nil
}
decoder, err := d.dec.compileToGetDecoder(uintptr(unsafe.Pointer(typ)), typ)
if err != nil {
return 0, err
}
return decoder.decode(buf, cursor, ifaceHeader.ptr)
}
func (d *interfaceDecoder) decodeEmptyInterface(buf []byte, cursor int64, p unsafe.Pointer) (int64, error) {
2020-05-08 09:13:30 +03:00
cursor = skipWhiteSpace(buf, cursor)
switch buf[cursor] {
case '{':
2020-08-25 05:17:38 +03:00
var v map[string]interface{}
2020-05-08 09:13:30 +03:00
ptr := unsafe.Pointer(&v)
2020-08-11 12:04:32 +03:00
dec := newMapDecoder(
interfaceMapType,
2020-12-22 15:55:59 +03:00
stringType,
newStringDecoder(d.structName, d.fieldName),
2020-12-22 15:55:59 +03:00
interfaceMapType.Elem(),
2021-02-09 15:37:18 +03:00
newInterfaceDecoder(d.dec, d.typ, d.structName, d.fieldName),
d.structName, d.fieldName,
2020-08-11 12:04:32 +03:00
)
2020-11-19 06:47:42 +03:00
cursor, err := dec.decode(buf, cursor, ptr)
2020-05-08 09:13:30 +03:00
if err != nil {
return 0, err
}
**(**interface{})(unsafe.Pointer(&p)) = v
2020-05-08 09:13:30 +03:00
return cursor, nil
case '[':
var v []interface{}
ptr := unsafe.Pointer(&v)
2020-08-11 12:04:32 +03:00
dec := newSliceDecoder(
2021-02-09 15:37:18 +03:00
newInterfaceDecoder(d.dec, d.typ, d.structName, d.fieldName),
2020-08-11 12:04:32 +03:00
d.typ,
d.typ.Size(),
d.structName, d.fieldName,
2020-08-11 12:04:32 +03:00
)
2020-11-19 06:47:42 +03:00
cursor, err := dec.decode(buf, cursor, ptr)
2020-05-08 09:13:30 +03:00
if err != nil {
return 0, err
}
**(**interface{})(unsafe.Pointer(&p)) = v
2020-05-08 09:13:30 +03:00
return cursor, nil
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
return newFloatDecoder(d.structName, d.fieldName, func(p unsafe.Pointer, v float64) {
2020-11-19 06:47:42 +03:00
*(*interface{})(p) = v
2020-05-08 09:13:30 +03:00
}).decode(buf, cursor, p)
case '"':
cursor++
start := cursor
for {
switch buf[cursor] {
case '\\':
cursor++
continue
2020-05-08 09:13:30 +03:00
case '"':
literal := buf[start:cursor]
cursor++
**(**interface{})(unsafe.Pointer(&p)) = *(*string)(unsafe.Pointer(&literal))
2020-05-08 09:13:30 +03:00
return cursor, nil
2020-08-25 05:18:01 +03:00
case nul:
2020-05-23 06:51:09 +03:00
return 0, errUnexpectedEndOfJSON("string", cursor)
2020-05-08 09:13:30 +03:00
}
cursor++
}
case 't':
2020-05-23 06:51:09 +03:00
if cursor+3 >= int64(len(buf)) {
return 0, errUnexpectedEndOfJSON("bool(true)", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+1] != 'r' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+1], "bool(true)", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+2] != 'u' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+2], "bool(true)", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+3] != 'e' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+3], "bool(true)", cursor)
2020-05-08 09:13:30 +03:00
}
cursor += 4
**(**interface{})(unsafe.Pointer(&p)) = true
2020-05-08 09:13:30 +03:00
return cursor, nil
case 'f':
2020-05-23 06:51:09 +03:00
if cursor+4 >= int64(len(buf)) {
return 0, errUnexpectedEndOfJSON("bool(false)", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+1] != 'a' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+1], "bool(false)", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+2] != 'l' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+2], "bool(false)", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+3] != 's' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+3], "bool(false)", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+4] != 'e' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+4], "bool(false)", cursor)
2020-05-08 09:13:30 +03:00
}
cursor += 5
**(**interface{})(unsafe.Pointer(&p)) = false
2020-05-08 09:13:30 +03:00
return cursor, nil
case 'n':
2020-05-23 06:51:09 +03:00
if cursor+3 >= int64(len(buf)) {
return 0, errUnexpectedEndOfJSON("null", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+1] != 'u' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+1], "null", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+2] != 'l' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+2], "null", cursor)
2020-05-08 09:13:30 +03:00
}
if buf[cursor+3] != 'l' {
2020-05-23 06:51:09 +03:00
return 0, errInvalidCharacter(buf[cursor+3], "null", cursor)
2020-05-08 09:13:30 +03:00
}
cursor += 4
**(**interface{})(unsafe.Pointer(&p)) = nil
2020-05-08 09:13:30 +03:00
return cursor, nil
}
2020-05-23 06:51:09 +03:00
return cursor, errNotAtBeginningOfValue(cursor)
2020-05-08 09:13:30 +03:00
}