2017-01-23 18:23:07 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2017-01-28 16:45:03 +02:00
|
|
|
"reflect"
|
2017-06-06 17:27:00 +02:00
|
|
|
"unsafe"
|
2017-01-23 18:23:07 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type objectLazyAny struct {
|
2017-01-24 16:36:16 +02:00
|
|
|
baseAny
|
2017-06-18 16:21:54 +02:00
|
|
|
cfg *frozenConfig
|
|
|
|
buf []byte
|
|
|
|
err error
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
2017-01-26 10:24:01 +02:00
|
|
|
func (any *objectLazyAny) ValueType() ValueType {
|
|
|
|
return Object
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:21:20 +02:00
|
|
|
func (any *objectLazyAny) MustBeValid() Any {
|
|
|
|
return any
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:23:07 +02:00
|
|
|
func (any *objectLazyAny) LastError() error {
|
2017-01-28 16:45:03 +02:00
|
|
|
return any.err
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) ToBool() bool {
|
2017-07-03 13:40:12 +02:00
|
|
|
return true
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) ToInt() int {
|
2017-07-03 18:53:10 +02:00
|
|
|
return 0
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) ToInt32() int32 {
|
2017-07-03 18:53:10 +02:00
|
|
|
return 0
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) ToInt64() int64 {
|
2017-07-03 18:53:10 +02:00
|
|
|
return 0
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 10:55:32 +02:00
|
|
|
func (any *objectLazyAny) ToUint() uint {
|
2017-07-03 18:53:10 +02:00
|
|
|
return 0
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) ToUint32() uint32 {
|
2017-07-03 18:53:10 +02:00
|
|
|
return 0
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) ToUint64() uint64 {
|
2017-07-03 18:53:10 +02:00
|
|
|
return 0
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
2017-01-23 18:23:07 +02:00
|
|
|
func (any *objectLazyAny) ToFloat32() float32 {
|
2017-07-03 18:53:10 +02:00
|
|
|
return 0
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) ToFloat64() float64 {
|
2017-07-03 18:53:10 +02:00
|
|
|
return 0
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) ToString() string {
|
2017-06-18 16:21:54 +02:00
|
|
|
return *(*string)(unsafe.Pointer(&any.buf))
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
2017-06-19 09:40:00 +02:00
|
|
|
func (any *objectLazyAny) ToVal(obj interface{}) {
|
|
|
|
iter := any.cfg.BorrowIterator(any.buf)
|
|
|
|
defer any.cfg.ReturnIterator(iter)
|
|
|
|
iter.ReadVal(obj)
|
|
|
|
}
|
|
|
|
|
2017-01-23 18:23:07 +02:00
|
|
|
func (any *objectLazyAny) Get(path ...interface{}) Any {
|
2017-01-24 17:13:58 +02:00
|
|
|
if len(path) == 0 {
|
|
|
|
return any
|
|
|
|
}
|
2017-01-31 14:26:35 +02:00
|
|
|
switch firstPath := path[0].(type) {
|
|
|
|
case string:
|
2017-06-18 16:21:54 +02:00
|
|
|
iter := any.cfg.BorrowIterator(any.buf)
|
|
|
|
defer any.cfg.ReturnIterator(iter)
|
|
|
|
valueBytes := locateObjectField(iter, firstPath)
|
|
|
|
if valueBytes == nil {
|
|
|
|
return newInvalidAny(path)
|
2017-01-26 10:24:01 +02:00
|
|
|
}
|
2017-07-09 10:15:45 +02:00
|
|
|
iter.ResetBytes(valueBytes)
|
|
|
|
return locatePath(iter, path[1:])
|
2017-01-31 14:26:35 +02:00
|
|
|
case int32:
|
|
|
|
if '*' == firstPath {
|
|
|
|
mappedAll := map[string]Any{}
|
2017-06-18 16:21:54 +02:00
|
|
|
iter := any.cfg.BorrowIterator(any.buf)
|
|
|
|
defer any.cfg.ReturnIterator(iter)
|
|
|
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
|
|
|
mapped := locatePath(iter, path[1:])
|
2017-01-31 14:26:35 +02:00
|
|
|
if mapped.ValueType() != Invalid {
|
2017-06-18 16:21:54 +02:00
|
|
|
mappedAll[field] = mapped
|
2017-01-31 14:26:35 +02:00
|
|
|
}
|
2017-06-18 16:21:54 +02:00
|
|
|
return true
|
|
|
|
})
|
2017-01-31 14:26:35 +02:00
|
|
|
return wrapMap(mappedAll)
|
|
|
|
}
|
2017-07-09 10:15:45 +02:00
|
|
|
return newInvalidAny(path)
|
2017-01-31 14:26:35 +02:00
|
|
|
default:
|
2017-06-18 16:21:54 +02:00
|
|
|
return newInvalidAny(path)
|
2017-01-24 17:13:58 +02:00
|
|
|
}
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) Keys() []string {
|
2017-06-18 16:21:54 +02:00
|
|
|
keys := []string{}
|
|
|
|
iter := any.cfg.BorrowIterator(any.buf)
|
|
|
|
defer any.cfg.ReturnIterator(iter)
|
|
|
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
|
|
|
iter.Skip()
|
|
|
|
keys = append(keys, field)
|
|
|
|
return true
|
|
|
|
})
|
2017-01-23 18:23:07 +02:00
|
|
|
return keys
|
|
|
|
}
|
2017-01-24 16:47:56 +02:00
|
|
|
|
2017-01-24 17:13:58 +02:00
|
|
|
func (any *objectLazyAny) Size() int {
|
2017-06-18 16:21:54 +02:00
|
|
|
size := 0
|
|
|
|
iter := any.cfg.BorrowIterator(any.buf)
|
|
|
|
defer any.cfg.ReturnIterator(iter)
|
|
|
|
iter.ReadObjectCB(func(iter *Iterator, field string) bool {
|
|
|
|
iter.Skip()
|
2017-06-19 17:43:53 +02:00
|
|
|
size++
|
2017-06-18 16:21:54 +02:00
|
|
|
return true
|
|
|
|
})
|
|
|
|
return size
|
2017-01-23 18:23:07 +02:00
|
|
|
}
|
|
|
|
|
2017-01-25 18:25:17 +02:00
|
|
|
func (any *objectLazyAny) WriteTo(stream *Stream) {
|
2017-06-18 16:21:54 +02:00
|
|
|
stream.Write(any.buf)
|
2017-01-26 09:44:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectLazyAny) GetInterface() interface{} {
|
2017-06-18 16:21:54 +02:00
|
|
|
iter := any.cfg.BorrowIterator(any.buf)
|
|
|
|
defer any.cfg.ReturnIterator(iter)
|
|
|
|
return iter.Read()
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type objectAny struct {
|
|
|
|
baseAny
|
2017-06-19 17:43:53 +02:00
|
|
|
err error
|
|
|
|
val reflect.Value
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func wrapStruct(val interface{}) *objectAny {
|
2017-06-18 17:09:30 +02:00
|
|
|
return &objectAny{baseAny{}, nil, reflect.ValueOf(val)}
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) ValueType() ValueType {
|
|
|
|
return Object
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:21:20 +02:00
|
|
|
func (any *objectAny) MustBeValid() Any {
|
|
|
|
return any
|
|
|
|
}
|
|
|
|
|
2017-01-28 16:45:03 +02:00
|
|
|
func (any *objectAny) Parse() *Iterator {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) LastError() error {
|
|
|
|
return any.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) ToBool() bool {
|
2017-01-29 10:55:32 +02:00
|
|
|
return any.val.NumField() != 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) ToInt() int {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) ToInt32() int32 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) ToInt64() int64 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) ToUint() uint {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 10:55:32 +02:00
|
|
|
func (any *objectAny) ToUint32() uint32 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) ToUint64() uint64 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 10:55:32 +02:00
|
|
|
func (any *objectAny) ToFloat32() float32 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) ToFloat64() float64 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) ToString() string {
|
2017-06-18 17:09:30 +02:00
|
|
|
str, err := MarshalToString(any.val.Interface())
|
|
|
|
any.err = err
|
|
|
|
return str
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) Get(path ...interface{}) Any {
|
|
|
|
if len(path) == 0 {
|
|
|
|
return any
|
|
|
|
}
|
2017-01-31 17:16:40 +02:00
|
|
|
switch firstPath := path[0].(type) {
|
|
|
|
case string:
|
2017-06-18 17:09:30 +02:00
|
|
|
field := any.val.FieldByName(firstPath)
|
|
|
|
if !field.IsValid() {
|
|
|
|
return newInvalidAny(path)
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
2017-06-18 17:09:30 +02:00
|
|
|
return Wrap(field.Interface())
|
2017-01-31 17:16:40 +02:00
|
|
|
case int32:
|
|
|
|
if '*' == firstPath {
|
|
|
|
mappedAll := map[string]Any{}
|
2017-06-18 17:09:30 +02:00
|
|
|
for i := 0; i < any.val.NumField(); i++ {
|
|
|
|
field := any.val.Field(i)
|
|
|
|
if field.CanInterface() {
|
|
|
|
mapped := Wrap(field.Interface()).Get(path[1:]...)
|
|
|
|
if mapped.ValueType() != Invalid {
|
|
|
|
mappedAll[any.val.Type().Field(i).Name] = mapped
|
|
|
|
}
|
2017-01-31 17:16:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wrapMap(mappedAll)
|
|
|
|
}
|
2017-07-09 10:15:45 +02:00
|
|
|
return newInvalidAny(path)
|
2017-01-31 17:16:40 +02:00
|
|
|
default:
|
2017-06-18 17:09:30 +02:00
|
|
|
return newInvalidAny(path)
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) Keys() []string {
|
2017-06-18 17:09:30 +02:00
|
|
|
keys := make([]string, 0, any.val.NumField())
|
|
|
|
for i := 0; i < any.val.NumField(); i++ {
|
|
|
|
keys = append(keys, any.val.Type().Field(i).Name)
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) Size() int {
|
2017-06-18 17:09:30 +02:00
|
|
|
return any.val.NumField()
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) WriteTo(stream *Stream) {
|
2017-06-18 17:09:30 +02:00
|
|
|
stream.WriteVal(any.val)
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *objectAny) GetInterface() interface{} {
|
2017-06-18 17:09:30 +02:00
|
|
|
return any.val.Interface()
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type mapAny struct {
|
|
|
|
baseAny
|
2017-06-19 17:43:53 +02:00
|
|
|
err error
|
|
|
|
val reflect.Value
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func wrapMap(val interface{}) *mapAny {
|
2017-06-18 17:18:32 +02:00
|
|
|
return &mapAny{baseAny{}, nil, reflect.ValueOf(val)}
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) ValueType() ValueType {
|
|
|
|
return Object
|
|
|
|
}
|
|
|
|
|
2017-06-19 15:21:20 +02:00
|
|
|
func (any *mapAny) MustBeValid() Any {
|
|
|
|
return any
|
|
|
|
}
|
|
|
|
|
2017-01-28 16:45:03 +02:00
|
|
|
func (any *mapAny) Parse() *Iterator {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) LastError() error {
|
|
|
|
return any.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) ToBool() bool {
|
2017-07-05 14:30:54 +02:00
|
|
|
return true
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) ToInt() int {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) ToInt32() int32 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) ToInt64() int64 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) ToUint() uint {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 10:55:32 +02:00
|
|
|
func (any *mapAny) ToUint32() uint32 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) ToUint64() uint64 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
2017-01-29 10:55:32 +02:00
|
|
|
func (any *mapAny) ToFloat32() float32 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-29 10:55:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) ToFloat64() float64 {
|
2017-07-05 14:30:54 +02:00
|
|
|
return 0
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) ToString() string {
|
2017-06-18 17:18:32 +02:00
|
|
|
str, err := MarshalToString(any.val.Interface())
|
|
|
|
any.err = err
|
|
|
|
return str
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) Get(path ...interface{}) Any {
|
|
|
|
if len(path) == 0 {
|
|
|
|
return any
|
|
|
|
}
|
2017-01-31 17:16:40 +02:00
|
|
|
switch firstPath := path[0].(type) {
|
|
|
|
case int32:
|
|
|
|
if '*' == firstPath {
|
|
|
|
mappedAll := map[string]Any{}
|
2017-06-18 17:18:32 +02:00
|
|
|
for _, key := range any.val.MapKeys() {
|
|
|
|
keyAsStr := key.String()
|
|
|
|
element := Wrap(any.val.MapIndex(key).Interface())
|
|
|
|
mapped := element.Get(path[1:]...)
|
2017-01-31 17:16:40 +02:00
|
|
|
if mapped.ValueType() != Invalid {
|
2017-06-18 17:18:32 +02:00
|
|
|
mappedAll[keyAsStr] = mapped
|
2017-01-31 17:16:40 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return wrapMap(mappedAll)
|
|
|
|
}
|
2017-07-09 10:15:45 +02:00
|
|
|
return newInvalidAny(path)
|
2017-01-31 17:16:40 +02:00
|
|
|
default:
|
2017-06-18 17:18:32 +02:00
|
|
|
value := any.val.MapIndex(reflect.ValueOf(firstPath))
|
|
|
|
if !value.IsValid() {
|
|
|
|
return newInvalidAny(path)
|
|
|
|
}
|
|
|
|
return Wrap(value.Interface())
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) Keys() []string {
|
2017-06-18 17:18:32 +02:00
|
|
|
keys := make([]string, 0, any.val.Len())
|
|
|
|
for _, key := range any.val.MapKeys() {
|
|
|
|
keys = append(keys, key.String())
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
return keys
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) Size() int {
|
2017-06-18 17:18:32 +02:00
|
|
|
return any.val.Len()
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) WriteTo(stream *Stream) {
|
2017-06-18 17:18:32 +02:00
|
|
|
stream.WriteVal(any.val)
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *mapAny) GetInterface() interface{} {
|
2017-06-18 17:18:32 +02:00
|
|
|
return any.val.Interface()
|
2017-01-28 16:45:03 +02:00
|
|
|
}
|