any.go 7.0 KB
Newer Older
S
stormgbs 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
package jsoniter

import (
	"errors"
	"fmt"
	"github.com/modern-go/reflect2"
	"io"
	"reflect"
	"strconv"
	"unsafe"
)

// Any generic object representation.
// The lazy json implementation holds []byte and parse lazily.
type Any interface {
	LastError() error
	ValueType() ValueType
	MustBeValid() Any
	ToBool() bool
	ToInt() int
	ToInt32() int32
	ToInt64() int64
	ToUint() uint
	ToUint32() uint32
	ToUint64() uint64
	ToFloat32() float32
	ToFloat64() float64
	ToString() string
	ToVal(val interface{})
	Get(path ...interface{}) Any
	Size() int
	Keys() []string
	GetInterface() interface{}
	WriteTo(stream *Stream)
}

type baseAny struct{}

func (any *baseAny) Get(path ...interface{}) Any {
	return &invalidAny{baseAny{}, fmt.Errorf("GetIndex %v from simple value", path)}
}

func (any *baseAny) Size() int {
	return 0
}

func (any *baseAny) Keys() []string {
	return []string{}
}

func (any *baseAny) ToVal(obj interface{}) {
	panic("not implemented")
}

// WrapInt32 turn int32 into Any interface
func WrapInt32(val int32) Any {
	return &int32Any{baseAny{}, val}
}

// WrapInt64 turn int64 into Any interface
func WrapInt64(val int64) Any {
	return &int64Any{baseAny{}, val}
}

// WrapUint32 turn uint32 into Any interface
func WrapUint32(val uint32) Any {
	return &uint32Any{baseAny{}, val}
}

// WrapUint64 turn uint64 into Any interface
func WrapUint64(val uint64) Any {
	return &uint64Any{baseAny{}, val}
}

// WrapFloat64 turn float64 into Any interface
func WrapFloat64(val float64) Any {
	return &floatAny{baseAny{}, val}
}

// WrapString turn string into Any interface
func WrapString(val string) Any {
	return &stringAny{baseAny{}, val}
}

// Wrap turn a go object into Any interface
func Wrap(val interface{}) Any {
	if val == nil {
		return &nilAny{}
	}
	asAny, isAny := val.(Any)
	if isAny {
		return asAny
	}
	typ := reflect2.TypeOf(val)
	switch typ.Kind() {
	case reflect.Slice:
		return wrapArray(val)
	case reflect.Struct:
		return wrapStruct(val)
	case reflect.Map:
		return wrapMap(val)
	case reflect.String:
		return WrapString(val.(string))
	case reflect.Int:
		if strconv.IntSize == 32 {
			return WrapInt32(int32(val.(int)))
		}
		return WrapInt64(int64(val.(int)))
	case reflect.Int8:
		return WrapInt32(int32(val.(int8)))
	case reflect.Int16:
		return WrapInt32(int32(val.(int16)))
	case reflect.Int32:
		return WrapInt32(val.(int32))
	case reflect.Int64:
		return WrapInt64(val.(int64))
	case reflect.Uint:
		if strconv.IntSize == 32 {
			return WrapUint32(uint32(val.(uint)))
		}
		return WrapUint64(uint64(val.(uint)))
	case reflect.Uintptr:
		if ptrSize == 32 {
			return WrapUint32(uint32(val.(uintptr)))
		}
		return WrapUint64(uint64(val.(uintptr)))
	case reflect.Uint8:
		return WrapUint32(uint32(val.(uint8)))
	case reflect.Uint16:
		return WrapUint32(uint32(val.(uint16)))
	case reflect.Uint32:
		return WrapUint32(uint32(val.(uint32)))
	case reflect.Uint64:
		return WrapUint64(val.(uint64))
	case reflect.Float32:
		return WrapFloat64(float64(val.(float32)))
	case reflect.Float64:
		return WrapFloat64(val.(float64))
	case reflect.Bool:
		if val.(bool) == true {
			return &trueAny{}
		}
		return &falseAny{}
	}
	return &invalidAny{baseAny{}, fmt.Errorf("unsupported type: %v", typ)}
}

// ReadAny read next JSON element as an Any object. It is a better json.RawMessage.
func (iter *Iterator) ReadAny() Any {
	return iter.readAny()
}

func (iter *Iterator) readAny() Any {
	c := iter.nextToken()
	switch c {
	case '"':
		iter.unreadByte()
		return &stringAny{baseAny{}, iter.ReadString()}
	case 'n':
		iter.skipThreeBytes('u', 'l', 'l') // null
		return &nilAny{}
	case 't':
		iter.skipThreeBytes('r', 'u', 'e') // true
		return &trueAny{}
	case 'f':
		iter.skipFourBytes('a', 'l', 's', 'e') // false
		return &falseAny{}
	case '{':
		return iter.readObjectAny()
	case '[':
		return iter.readArrayAny()
	case '-':
		return iter.readNumberAny(false)
	case 0:
		return &invalidAny{baseAny{}, errors.New("input is empty")}
	default:
		return iter.readNumberAny(true)
	}
}

func (iter *Iterator) readNumberAny(positive bool) Any {
	iter.startCapture(iter.head - 1)
	iter.skipNumber()
	lazyBuf := iter.stopCapture()
	return &numberLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
}

func (iter *Iterator) readObjectAny() Any {
	iter.startCapture(iter.head - 1)
	iter.skipObject()
	lazyBuf := iter.stopCapture()
	return &objectLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
}

func (iter *Iterator) readArrayAny() Any {
	iter.startCapture(iter.head - 1)
	iter.skipArray()
	lazyBuf := iter.stopCapture()
	return &arrayLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
}

func locateObjectField(iter *Iterator, target string) []byte {
	var found []byte
	iter.ReadObjectCB(func(iter *Iterator, field string) bool {
		if field == target {
			found = iter.SkipAndReturnBytes()
			return false
		}
		iter.Skip()
		return true
	})
	return found
}

func locateArrayElement(iter *Iterator, target int) []byte {
	var found []byte
	n := 0
	iter.ReadArrayCB(func(iter *Iterator) bool {
		if n == target {
			found = iter.SkipAndReturnBytes()
			return false
		}
		iter.Skip()
		n++
		return true
	})
	return found
}

func locatePath(iter *Iterator, path []interface{}) Any {
	for i, pathKeyObj := range path {
		switch pathKey := pathKeyObj.(type) {
		case string:
			valueBytes := locateObjectField(iter, pathKey)
			if valueBytes == nil {
				return newInvalidAny(path[i:])
			}
			iter.ResetBytes(valueBytes)
		case int:
			valueBytes := locateArrayElement(iter, pathKey)
			if valueBytes == nil {
				return newInvalidAny(path[i:])
			}
			iter.ResetBytes(valueBytes)
		case int32:
			if '*' == pathKey {
				return iter.readAny().Get(path[i:]...)
			}
			return newInvalidAny(path[i:])
		default:
			return newInvalidAny(path[i:])
		}
	}
	if iter.Error != nil && iter.Error != io.EOF {
		return &invalidAny{baseAny{}, iter.Error}
	}
	return iter.readAny()
}

var anyType = reflect2.TypeOfPtr((*Any)(nil)).Elem()

func createDecoderOfAny(ctx *ctx, typ reflect2.Type) ValDecoder {
	if typ == anyType {
		return &directAnyCodec{}
	}
	if typ.Implements(anyType) {
		return &anyCodec{
			valType: typ,
		}
	}
	return nil
}

func createEncoderOfAny(ctx *ctx, typ reflect2.Type) ValEncoder {
	if typ == anyType {
		return &directAnyCodec{}
	}
	if typ.Implements(anyType) {
		return &anyCodec{
			valType: typ,
		}
	}
	return nil
}

type anyCodec struct {
	valType reflect2.Type
}

func (codec *anyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
	panic("not implemented")
}

func (codec *anyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
	obj := codec.valType.UnsafeIndirect(ptr)
	any := obj.(Any)
	any.WriteTo(stream)
}

func (codec *anyCodec) IsEmpty(ptr unsafe.Pointer) bool {
	obj := codec.valType.UnsafeIndirect(ptr)
	any := obj.(Any)
	return any.Size() == 0
}

type directAnyCodec struct {
}

func (codec *directAnyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
	*(*Any)(ptr) = iter.readAny()
}

func (codec *directAnyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
	any := *(*Any)(ptr)
	if any == nil {
		stream.WriteNil()
		return
	}
	any.WriteTo(stream)
}

func (codec *directAnyCodec) IsEmpty(ptr unsafe.Pointer) bool {
	any := *(*Any)(ptr)
	return any.Size() == 0
}