go-json/decode.go

529 lines
10 KiB
Go
Raw Normal View History

2020-04-22 07:06:52 +03:00
package json
import (
"errors"
"io"
"math"
"reflect"
"strings"
"sync"
"unsafe"
)
type Token interface{}
type Delim rune
const (
stateNone int = iota
stateLiteral
stateObject
stateArray
)
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
}
type context struct {
2020-04-22 11:59:01 +03:00
idx int
keys [][]byte
values [][]byte
start int
stack int
2020-04-22 07:06:52 +03:00
}
func newContext() *context {
return &context{
2020-04-22 11:59:01 +03:00
keys: make([][]byte, 64),
values: make([][]byte, 64),
2020-04-22 07:06:52 +03:00
}
}
func (c *context) pushStack() {
if len(c.keys) <= c.stack {
c.keys = append(c.keys, nil)
2020-04-22 11:59:01 +03:00
c.values = append(c.values, nil)
2020-04-22 07:06:52 +03:00
}
c.stack++
}
func (c *context) popStack() {
c.stack--
}
func (c *context) setKey(key []byte) {
c.keys[c.stack] = key
}
2020-04-22 11:59:01 +03:00
func (c *context) setValue(value []byte) {
c.values[c.stack] = value
2020-04-22 07:06:52 +03:00
}
func (c *context) key() ([]byte, error) {
if len(c.keys) <= c.stack {
2020-04-22 11:59:01 +03:00
return nil, errors.New("unexpected error key")
2020-04-22 07:06:52 +03:00
}
key := c.keys[c.stack]
if len(key) == 0 {
2020-04-22 11:59:01 +03:00
return nil, errors.New("unexpected error key")
2020-04-22 07:06:52 +03:00
}
return key, nil
}
2020-04-22 11:59:01 +03:00
func (c *context) value() ([]byte, error) {
if len(c.values) <= c.stack {
return nil, errors.New("unexpected error value")
2020-04-22 07:06:52 +03:00
}
2020-04-22 11:59:01 +03:00
return c.values[c.stack], nil
2020-04-22 07:06:52 +03:00
}
var (
ctxPool sync.Pool
cachedDecodeOp map[string]DecodeOp
)
func init() {
cachedDecodeOp = map[string]DecodeOp{}
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()
op, exists := cachedDecodeOp[name]
if !exists {
decodeOp, err := d.compile(rv.Elem())
if err != nil {
return err
}
if name != "" {
cachedDecodeOp[name] = decodeOp
}
op = decodeOp
}
ptr := rv.Pointer()
ctx := ctxPool.Get().(*context)
if err := d.decode(ctx, src, ptr, op); err != nil {
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()
op, exists := cachedDecodeOp[name]
if !exists {
decodeOp, err := d.compile(rv.Elem())
if err != nil {
return err
}
if name != "" {
cachedDecodeOp[name] = decodeOp
}
op = decodeOp
}
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
}
if err := d.decode(ctx, buf[:n], ptr, op); err != nil {
return err
}
}
return nil
}
type DecodeOp func(uintptr, []byte, []byte) error
func (d *Decoder) compile(v reflect.Value) (DecodeOp, error) {
switch v.Type().Kind() {
case reflect.Struct:
return d.compileStruct(v)
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()
}
return nil, nil
}
func parseInt(b []byte) (int64, error) {
isNegative := false
if b[0] == '-' {
b = b[1:]
isNegative = true
}
maxDigit := len(b)
sum := int64(0)
for i := 0; i < maxDigit; i++ {
c := int64(b[i]) - 48
if 0 <= c && c <= 9 {
digitValue := int64(math.Pow10(maxDigit - i - 1))
sum += c * digitValue
} else {
return 0, errors.New("failed to parse int")
}
}
if isNegative {
return -1 * sum, nil
}
return sum, nil
}
func parseUint(b []byte) (uint64, error) {
maxDigit := len(b)
sum := uint64(0)
for i := 0; i < maxDigit; i++ {
c := uint64(b[i]) - 48
if 0 <= c && c <= 9 {
digitValue := uint64(math.Pow10(maxDigit - i - 1))
sum += c * digitValue
} else {
return 0, errors.New("failed to parse uint")
}
}
return sum, nil
}
func (d *Decoder) compileInt() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
i64, err := parseInt(src)
if err != nil {
return err
}
*(*int)(unsafe.Pointer(p)) = int(i64)
return nil
}, nil
}
func (d *Decoder) compileInt8() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
i64, err := parseInt(src)
if err != nil {
return err
}
*(*int8)(unsafe.Pointer(p)) = int8(i64)
return nil
}, nil
}
func (d *Decoder) compileInt16() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
i64, err := parseInt(src)
if err != nil {
return err
}
*(*int16)(unsafe.Pointer(p)) = int16(i64)
return nil
}, nil
}
func (d *Decoder) compileInt32() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
i64, err := parseInt(src)
if err != nil {
return err
}
*(*int32)(unsafe.Pointer(p)) = int32(i64)
return nil
}, nil
}
func (d *Decoder) compileInt64() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
i64, err := parseInt(src)
if err != nil {
return err
}
*(*int64)(unsafe.Pointer(p)) = i64
return nil
}, nil
}
func (d *Decoder) compileUint() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
u64, err := parseUint(src)
if err != nil {
return err
}
*(*uint)(unsafe.Pointer(p)) = uint(u64)
return nil
}, nil
}
func (d *Decoder) compileUint8() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
u64, err := parseUint(src)
if err != nil {
return err
}
*(*uint8)(unsafe.Pointer(p)) = uint8(u64)
return nil
}, nil
}
func (d *Decoder) compileUint16() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
u64, err := parseUint(src)
if err != nil {
return err
}
*(*uint16)(unsafe.Pointer(p)) = uint16(u64)
return nil
}, nil
}
func (d *Decoder) compileUint32() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
u64, err := parseUint(src)
if err != nil {
return err
}
*(*uint32)(unsafe.Pointer(p)) = uint32(u64)
return nil
}, nil
}
func (d *Decoder) compileUint64() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
u64, err := parseUint(src)
if err != nil {
return err
}
*(*uint64)(unsafe.Pointer(p)) = u64
return nil
}, nil
}
func (d *Decoder) compileString() (DecodeOp, error) {
return func(p uintptr, src []byte, _ []byte) error {
*(*string)(unsafe.Pointer(p)) = *(*string)(unsafe.Pointer(&src))
return nil
}, nil
}
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
}
func (d *Decoder) compileStruct(v reflect.Value) (DecodeOp, error) {
type opset struct {
key []byte
op func(uintptr, []byte) error
}
typ := v.Type()
fieldNum := typ.NumField()
opMap := map[string]func(uintptr, []byte) error{}
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]
}
}
op, err := d.compile(v.Field(i))
if err != nil {
return nil, err
}
if op == nil {
continue
}
fieldOp := func(base uintptr, value []byte) error {
return op(base+field.Offset, value, nil)
}
opMap[field.Name] = fieldOp
opMap[keyName] = fieldOp
opMap[strings.ToLower(keyName)] = fieldOp
}
return func(p uintptr, key []byte, value []byte) error {
k := *(*string)(unsafe.Pointer(&key))
op, exists := opMap[k]
if !exists {
return nil
}
return op(p, value)
}, nil
}
func (d *Decoder) decode(ctx *context, src []byte, ptr uintptr, op DecodeOp) error {
slen := len(src)
2020-04-22 11:59:01 +03:00
for i := 0; i < slen; i++ {
2020-04-22 07:06:52 +03:00
c := src[i]
switch c {
case '{':
ctx.pushStack()
case '}':
key, err := ctx.key()
if err != nil {
return err
}
2020-04-22 11:59:01 +03:00
if err := op(ptr, key, d.value); err != nil {
2020-04-22 07:06:52 +03:00
return err
}
ctx.popStack()
case '[':
case ']':
case ':':
2020-04-22 11:59:01 +03:00
if len(d.value) == 0 {
return errors.New("unexpected error map value")
2020-04-22 07:06:52 +03:00
}
2020-04-22 11:59:01 +03:00
ctx.setKey(d.value)
2020-04-22 07:06:52 +03:00
case ',':
key, err := ctx.key()
if err != nil {
return err
}
2020-04-22 11:59:01 +03:00
if err := op(ptr, key, d.value); err != nil {
2020-04-22 07:06:52 +03:00
return err
}
case '"':
start := i + 1
for i = start; i < slen && src[i] != '"'; i++ {
if src[i] == '\\' {
i++
}
}
end := i
if end <= start {
2020-04-22 11:59:01 +03:00
return errors.New("unexpected error value")
2020-04-22 07:06:52 +03:00
}
2020-04-22 11:59:01 +03:00
d.value = src[start:end]
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
start := i
for ; i < slen; i++ {
c := src[i]
switch c {
case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', 'e', 'E':
default:
goto end
}
}
end:
end := i
if end <= start {
return errors.New("unexpected error number")
}
d.value = src[start:end]
i--
case 't':
if i+3 < slen && src[i+1] == 'r' && src[i+2] == 'u' && src[i+3] == 'e' {
d.value = []byte("true")
} else {
return errors.New("unexpected error true")
}
i += 3
case 'f':
if i+4 < slen && src[i+1] == 'a' && src[i+2] == 'l' && src[i+3] == 's' && src[i+4] == 'e' {
d.value = []byte("false")
} else {
return errors.New("unexpected error false")
}
i += 4
case 'n':
if i+3 < slen && src[i+1] == 'u' && src[i+2] == 'l' && src[i+3] == 'l' {
d.value = []byte("null")
} else {
return errors.New("unexpected error null")
}
i += 3
2020-04-22 07:06:52 +03:00
}
}
return nil
}
func (d *Decoder) parse(tokens []Token) {
}
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() {
}