go-json/decode.go

418 lines
9.6 KiB
Go
Raw Normal View History

2020-04-22 07:06:52 +03:00
package json
import (
2020-04-25 13:55:05 +03:00
"bytes"
2020-05-08 14:22:57 +03:00
"encoding"
2020-04-22 07:06:52 +03:00
"io"
"reflect"
"strings"
"sync"
"unsafe"
2020-05-08 09:13:30 +03:00
"golang.org/x/xerrors"
2020-04-22 07:06:52 +03:00
)
2020-04-25 13:55:05 +03:00
// A Token holds a value of one of these types:
//
// Delim, for the four JSON delimiters [ ] { }
// bool, for JSON booleans
// float64, for JSON numbers
// Number, for JSON numbers
// string, for JSON string literals
// nil, for JSON null
//
2020-04-22 07:06:52 +03:00
type Token interface{}
type Delim rune
2020-04-23 19:39:20 +03:00
type decoder interface {
2020-05-06 20:37:29 +03:00
decode([]byte, int, uintptr) (int, error)
2020-04-23 19:39:20 +03:00
}
2020-04-22 07:06:52 +03:00
type Decoder struct {
2020-04-25 13:55:05 +03:00
r io.Reader
buffered func() io.Reader
2020-04-22 07:06:52 +03:00
}
2020-04-28 12:35:08 +03:00
type decoderMap struct {
sync.Map
}
2020-05-06 16:22:13 +03:00
func (m *decoderMap) get(k uintptr) decoder {
2020-04-28 12:35:08 +03:00
if v, ok := m.Load(k); ok {
return v.(decoder)
}
return nil
}
2020-05-06 16:22:13 +03:00
func (m *decoderMap) set(k uintptr, dec decoder) {
2020-04-28 12:35:08 +03:00
m.Store(k, dec)
}
2020-04-22 07:06:52 +03:00
var (
2020-05-08 14:22:57 +03:00
cachedDecoder decoderMap
unmarshalJSONType = reflect.TypeOf((*Unmarshaler)(nil)).Elem()
unmarshalTextType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
2020-04-22 07:06:52 +03:00
)
func init() {
2020-04-28 12:35:08 +03:00
cachedDecoder = decoderMap{}
2020-04-22 07:06:52 +03:00
}
2020-04-25 13:55:05 +03:00
// NewDecoder returns a new decoder that reads from r.
//
// The decoder introduces its own buffering and may
// read data from r beyond the JSON values requested.
2020-04-22 07:06:52 +03:00
func NewDecoder(r io.Reader) *Decoder {
2020-04-25 13:55:05 +03:00
return &Decoder{r: r}
2020-04-22 07:06:52 +03:00
}
2020-04-25 13:55:05 +03:00
// Buffered returns a reader of the data remaining in the Decoder's
// buffer. The reader is valid until the next call to Decode.
2020-04-22 07:06:52 +03:00
func (d *Decoder) Buffered() io.Reader {
2020-04-25 13:55:05 +03:00
return d.buffered()
2020-04-22 07:06:52 +03:00
}
func (d *Decoder) validateType(typ *rtype, p uintptr) error {
if typ.Kind() != reflect.Ptr || p == 0 {
return &InvalidUnmarshalError{Type: rtype2type(typ)}
}
return nil
}
2020-04-24 15:57:11 +03:00
func (d *Decoder) decode(src []byte, header *interfaceHeader) error {
2020-04-24 14:23:26 +03:00
typ := header.typ
2020-05-06 16:22:13 +03:00
typeptr := uintptr(unsafe.Pointer(typ))
// noescape trick for header.typ ( reflect.*rtype )
copiedType := (*rtype)(unsafe.Pointer(typeptr))
ptr := uintptr(header.ptr)
if err := d.validateType(copiedType, ptr); err != nil {
return err
}
2020-05-06 16:22:13 +03:00
dec := cachedDecoder.get(typeptr)
2020-04-28 12:35:08 +03:00
if dec == nil {
2020-05-08 17:59:49 +03:00
compiledDec, err := d.compileHead(copiedType)
2020-04-22 07:06:52 +03:00
if err != nil {
return err
}
2020-05-06 16:22:13 +03:00
cachedDecoder.set(typeptr, compiledDec)
2020-04-23 19:39:20 +03:00
dec = compiledDec
2020-04-22 07:06:52 +03:00
}
2020-05-06 20:37:29 +03:00
if _, err := dec.decode(src, 0, ptr); err != nil {
2020-04-22 07:06:52 +03:00
return err
}
return nil
}
2020-04-24 15:57:11 +03:00
func (d *Decoder) decodeForUnmarshal(src []byte, v interface{}) error {
header := (*interfaceHeader)(unsafe.Pointer(&v))
header.typ.escape()
return d.decode(src, header)
}
func (d *Decoder) decodeForUnmarshalNoEscape(src []byte, v interface{}) error {
header := (*interfaceHeader)(unsafe.Pointer(&v))
return d.decode(src, header)
}
2020-04-25 13:55:05 +03:00
// Decode reads the next JSON-encoded value from its
// input and stores it in the value pointed to by v.
//
// See the documentation for Unmarshal for details about
// the conversion of JSON into a Go value.
2020-04-22 07:06:52 +03:00
func (d *Decoder) Decode(v interface{}) error {
2020-04-24 14:23:26 +03:00
header := (*interfaceHeader)(unsafe.Pointer(&v))
typ := header.typ
ptr := uintptr(header.ptr)
2020-05-06 16:22:13 +03:00
typeptr := uintptr(unsafe.Pointer(typ))
// noescape trick for header.typ ( reflect.*rtype )
copiedType := (*rtype)(unsafe.Pointer(typeptr))
if err := d.validateType(copiedType, ptr); err != nil {
return err
}
2020-05-06 16:22:13 +03:00
dec := cachedDecoder.get(typeptr)
2020-04-28 12:35:08 +03:00
if dec == nil {
2020-05-08 14:22:57 +03:00
compiledDec, err := d.compileHead(typ)
2020-04-22 07:06:52 +03:00
if err != nil {
return err
}
2020-05-06 16:22:13 +03:00
cachedDecoder.set(typeptr, compiledDec)
2020-04-23 19:39:20 +03:00
dec = compiledDec
2020-04-22 07:06:52 +03:00
}
2020-04-22 07:06:52 +03:00
for {
buf := make([]byte, 1024)
n, err := d.r.Read(buf)
if n == 0 || err == io.EOF {
return nil
}
if err != nil {
return err
}
2020-05-06 20:37:29 +03:00
cursor, err := dec.decode(buf[:n], 0, ptr)
if err != nil {
2020-04-22 07:06:52 +03:00
return err
}
2020-05-06 20:37:29 +03:00
d.buffered = func() io.Reader {
return bytes.NewReader(buf[cursor:])
}
2020-04-22 07:06:52 +03:00
}
return nil
}
2020-05-08 14:22:57 +03:00
func (d *Decoder) compileHead(typ *rtype) (decoder, error) {
if typ.Implements(unmarshalJSONType) {
return newUnmarshalJSONDecoder(typ), nil
} else if typ.Implements(unmarshalTextType) {
2020-05-08 14:25:49 +03:00
return newUnmarshalTextDecoder(typ), nil
2020-05-08 14:22:57 +03:00
}
return d.compile(typ.Elem())
}
2020-04-24 14:23:26 +03:00
func (d *Decoder) compile(typ *rtype) (decoder, error) {
2020-05-08 14:22:57 +03:00
if typ.Implements(unmarshalJSONType) {
return newUnmarshalJSONDecoder(typ), nil
} else if typ.Implements(unmarshalTextType) {
2020-05-08 14:25:49 +03:00
return newUnmarshalTextDecoder(typ), nil
2020-05-08 14:22:57 +03:00
}
2020-04-23 19:39:20 +03:00
switch typ.Kind() {
case reflect.Ptr:
return d.compilePtr(typ)
2020-04-22 07:06:52 +03:00
case reflect.Struct:
2020-04-23 19:39:20 +03:00
return d.compileStruct(typ)
2020-04-25 13:55:05 +03:00
case reflect.Slice:
return d.compileSlice(typ)
case reflect.Array:
return d.compileArray(typ)
2020-04-25 16:48:16 +03:00
case reflect.Map:
return d.compileMap(typ)
2020-05-08 09:13:30 +03:00
case reflect.Interface:
return d.compileInterface(typ)
2020-04-22 07:06:52 +03:00
case reflect.Int:
return d.compileInt()
case reflect.Int8:
return d.compileInt8()
case reflect.Int16:
return d.compileInt16()
case reflect.Int32:
return d.compileInt32()
case reflect.Int64:
return d.compileInt64()
case reflect.Uint:
return d.compileUint()
case reflect.Uint8:
return d.compileUint8()
case reflect.Uint16:
return d.compileUint16()
case reflect.Uint32:
return d.compileUint32()
case reflect.Uint64:
return d.compileUint64()
case reflect.String:
return d.compileString()
2020-04-22 12:51:42 +03:00
case reflect.Bool:
return d.compileBool()
2020-04-24 10:46:12 +03:00
case reflect.Float32:
return d.compileFloat32()
case reflect.Float64:
return d.compileFloat64()
2020-04-22 07:06:52 +03:00
}
2020-05-08 09:13:30 +03:00
return nil, xerrors.Errorf("unknown type %s", typ)
2020-04-22 07:06:52 +03:00
}
2020-04-24 14:23:26 +03:00
func (d *Decoder) compilePtr(typ *rtype) (decoder, error) {
2020-04-23 19:39:20 +03:00
dec, err := d.compile(typ.Elem())
if err != nil {
return nil, err
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
return newPtrDecoder(dec, typ.Elem()), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileInt() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) {
*(*int)(unsafe.Pointer(p)) = int(v)
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileInt8() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) {
*(*int8)(unsafe.Pointer(p)) = int8(v)
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileInt16() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) {
*(*int16)(unsafe.Pointer(p)) = int16(v)
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileInt32() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) {
*(*int32)(unsafe.Pointer(p)) = int32(v)
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileInt64() (decoder, error) {
return newIntDecoder(func(p uintptr, v int64) {
*(*int64)(unsafe.Pointer(p)) = v
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileUint() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) {
*(*uint)(unsafe.Pointer(p)) = uint(v)
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileUint8() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) {
*(*uint8)(unsafe.Pointer(p)) = uint8(v)
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileUint16() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) {
*(*uint16)(unsafe.Pointer(p)) = uint16(v)
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileUint32() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) {
*(*uint32)(unsafe.Pointer(p)) = uint32(v)
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileUint64() (decoder, error) {
return newUintDecoder(func(p uintptr, v uint64) {
*(*uint64)(unsafe.Pointer(p)) = v
}), nil
2020-04-22 07:06:52 +03:00
}
2020-04-24 10:46:12 +03:00
func (d *Decoder) compileFloat32() (decoder, error) {
return newFloatDecoder(func(p uintptr, v float64) {
*(*float32)(unsafe.Pointer(p)) = float32(v)
}), nil
}
func (d *Decoder) compileFloat64() (decoder, error) {
return newFloatDecoder(func(p uintptr, v float64) {
*(*float64)(unsafe.Pointer(p)) = v
}), nil
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileString() (decoder, error) {
return newStringDecoder(), nil
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compileBool() (decoder, error) {
return newBoolDecoder(), nil
2020-04-22 12:51:42 +03:00
}
2020-04-25 13:55:05 +03:00
func (d *Decoder) compileSlice(typ *rtype) (decoder, error) {
elem := typ.Elem()
decoder, err := d.compile(elem)
if err != nil {
return nil, err
}
return newSliceDecoder(decoder, elem, elem.Size()), nil
}
func (d *Decoder) compileArray(typ *rtype) (decoder, error) {
elem := typ.Elem()
decoder, err := d.compile(elem)
if err != nil {
return nil, err
}
return newArrayDecoder(decoder, elem, typ.Len()), nil
}
2020-04-25 16:48:16 +03:00
func (d *Decoder) compileMap(typ *rtype) (decoder, error) {
keyDec, err := d.compile(typ.Key())
if err != nil {
return nil, err
}
valueDec, err := d.compile(typ.Elem())
if err != nil {
return nil, err
}
return newMapDecoder(typ, keyDec, valueDec), nil
}
2020-05-08 09:13:30 +03:00
func (d *Decoder) compileInterface(typ *rtype) (decoder, error) {
return newInterfaceDecoder(typ), nil
}
2020-04-22 07:06:52 +03:00
func (d *Decoder) getTag(field reflect.StructField) string {
return field.Tag.Get("json")
}
func (d *Decoder) isIgnoredStructField(field reflect.StructField) bool {
if field.PkgPath != "" && !field.Anonymous {
// private field
return true
}
tag := d.getTag(field)
if tag == "-" {
return true
}
return false
}
2020-04-24 14:23:26 +03:00
func (d *Decoder) compileStruct(typ *rtype) (decoder, error) {
2020-04-22 07:06:52 +03:00
fieldNum := typ.NumField()
2020-04-23 19:39:20 +03:00
fieldMap := map[string]*structFieldSet{}
2020-04-22 07:06:52 +03:00
for i := 0; i < fieldNum; i++ {
field := typ.Field(i)
if d.isIgnoredStructField(field) {
continue
}
keyName := field.Name
tag := d.getTag(field)
opts := strings.Split(tag, ",")
if len(opts) > 0 {
if opts[0] != "" {
keyName = opts[0]
}
}
2020-04-24 14:23:26 +03:00
dec, err := d.compile(type2rtype(field.Type))
2020-04-22 07:06:52 +03:00
if err != nil {
return nil, err
}
2020-04-23 19:39:20 +03:00
fieldSet := &structFieldSet{dec: dec, offset: field.Offset}
fieldMap[field.Name] = fieldSet
fieldMap[keyName] = fieldSet
fieldMap[strings.ToLower(keyName)] = fieldSet
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
return newStructDecoder(fieldMap), nil
2020-04-22 07:06:52 +03:00
}
2020-04-25 13:55:05 +03:00
// DisallowUnknownFields causes the Decoder to return an error when the destination
// is a struct and the input contains object keys which do not match any
// non-ignored, exported fields in the destination.
2020-04-22 07:06:52 +03:00
func (d *Decoder) DisallowUnknownFields() {
}
func (d *Decoder) InputOffset() int64 {
return 0
}
func (d *Decoder) More() bool {
return false
}
func (d *Decoder) Token() (Token, error) {
return nil, nil
}
2020-04-25 13:55:05 +03:00
// UseNumber causes the Decoder to unmarshal a number into an interface{} as a
// Number instead of as a float64.
2020-04-22 07:06:52 +03:00
func (d *Decoder) UseNumber() {
}