go-json/decode.go

296 lines
5.8 KiB
Go
Raw Normal View History

2020-04-22 07:06:52 +03:00
package json
import (
"io"
"reflect"
"strings"
"sync"
"unsafe"
)
type Token interface{}
type Delim rune
const (
stateNone int = iota
stateLiteral
stateObject
stateArray
)
2020-04-23 19:39:20 +03:00
type decoder interface {
decode(*context, uintptr) error
}
2020-04-22 07:06:52 +03:00
type Decoder struct {
2020-04-22 11:59:01 +03:00
r io.Reader
state int
value []byte
2020-04-22 07:06:52 +03:00
}
var (
2020-04-23 19:39:20 +03:00
ctxPool sync.Pool
cachedDecoder map[string]decoder
2020-04-22 07:06:52 +03:00
)
func init() {
2020-04-23 19:39:20 +03:00
cachedDecoder = map[string]decoder{}
2020-04-22 07:06:52 +03:00
ctxPool = sync.Pool{
New: func() interface{} {
return newContext()
},
}
}
func NewDecoder(r io.Reader) *Decoder {
return &Decoder{
r: r,
}
}
func (d *Decoder) Buffered() io.Reader {
return d.r
}
func (d *Decoder) decodeForUnmarshal(src []byte, v interface{}) error {
rv := reflect.ValueOf(v)
typ := rv.Type()
if typ.Kind() != reflect.Ptr {
return ErrDecodePointer
}
name := typ.String()
2020-04-23 19:39:20 +03:00
dec, exists := cachedDecoder[name]
2020-04-22 07:06:52 +03:00
if !exists {
2020-04-23 19:39:20 +03:00
compiledDec, err := d.compile(typ.Elem())
2020-04-22 07:06:52 +03:00
if err != nil {
return err
}
if name != "" {
2020-04-23 19:39:20 +03:00
cachedDecoder[name] = compiledDec
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
dec = compiledDec
2020-04-22 07:06:52 +03:00
}
ptr := rv.Pointer()
ctx := ctxPool.Get().(*context)
2020-04-23 19:39:20 +03:00
ctx.setBuf(src)
if err := dec.decode(ctx, ptr); err != nil {
2020-04-22 07:06:52 +03:00
ctxPool.Put(ctx)
return err
}
ctxPool.Put(ctx)
return nil
}
func (d *Decoder) Decode(v interface{}) error {
rv := reflect.ValueOf(v)
typ := rv.Type()
if typ.Kind() != reflect.Ptr {
return ErrDecodePointer
}
name := typ.String()
2020-04-23 19:39:20 +03:00
dec, exists := cachedDecoder[name]
2020-04-22 07:06:52 +03:00
if !exists {
2020-04-23 19:39:20 +03:00
compiledDec, err := d.compile(typ.Elem())
2020-04-22 07:06:52 +03:00
if err != nil {
return err
}
if name != "" {
2020-04-23 19:39:20 +03:00
cachedDecoder[name] = compiledDec
2020-04-22 07:06:52 +03:00
}
2020-04-23 19:39:20 +03:00
dec = compiledDec
2020-04-22 07:06:52 +03:00
}
ptr := rv.Pointer()
ctx := ctxPool.Get().(*context)
defer ctxPool.Put(ctx)
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-04-23 19:39:20 +03:00
ctx.setBuf(buf[:n])
if err := dec.decode(ctx, ptr); err != nil {
2020-04-22 07:06:52 +03:00
return err
}
}
return nil
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compile(typ reflect.Type) (decoder, error) {
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-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-22 07:06:52 +03:00
}
return nil, nil
}
2020-04-23 19:39:20 +03:00
func (d *Decoder) compilePtr(typ reflect.Type) (decoder, error) {
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-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-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-23 19:39:20 +03:00
func (d *Decoder) compileStruct(typ reflect.Type) (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-23 19:39:20 +03:00
dec, err := d.compile(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
}
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
}
func (d *Decoder) UseNumber() {
}