1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/feature_any_object.go

481 lines
8.5 KiB
Go
Raw Normal View History

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