123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374 |
- package jsoniter
- import (
- "reflect"
- "unsafe"
- )
- type objectLazyAny struct {
- baseAny
- cfg *frozenConfig
- buf []byte
- err error
- }
- func (any *objectLazyAny) ValueType() ValueType {
- return ObjectValue
- }
- func (any *objectLazyAny) MustBeValid() Any {
- return any
- }
- func (any *objectLazyAny) LastError() error {
- return any.err
- }
- func (any *objectLazyAny) ToBool() bool {
- return true
- }
- func (any *objectLazyAny) ToInt() int {
- return 0
- }
- func (any *objectLazyAny) ToInt32() int32 {
- return 0
- }
- func (any *objectLazyAny) ToInt64() int64 {
- return 0
- }
- func (any *objectLazyAny) ToUint() uint {
- return 0
- }
- func (any *objectLazyAny) ToUint32() uint32 {
- return 0
- }
- func (any *objectLazyAny) ToUint64() uint64 {
- return 0
- }
- func (any *objectLazyAny) ToFloat32() float32 {
- return 0
- }
- func (any *objectLazyAny) ToFloat64() float64 {
- return 0
- }
- func (any *objectLazyAny) ToString() string {
- return *(*string)(unsafe.Pointer(&any.buf))
- }
- 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 {
- if len(path) == 0 {
- return any
- }
- 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)
- }
- iter.ResetBytes(valueBytes)
- return locatePath(iter, path[1:])
- case int32:
- if '*' == firstPath {
- mappedAll := map[string]Any{}
- iter := any.cfg.BorrowIterator(any.buf)
- defer any.cfg.ReturnIterator(iter)
- iter.ReadMapCB(func(iter *Iterator, field string) bool {
- mapped := locatePath(iter, path[1:])
- if mapped.ValueType() != InvalidValue {
- mappedAll[field] = mapped
- }
- return true
- })
- return wrapMap(mappedAll)
- }
- return newInvalidAny(path)
- default:
- return newInvalidAny(path)
- }
- }
- func (any *objectLazyAny) Keys() []string {
- keys := []string{}
- iter := any.cfg.BorrowIterator(any.buf)
- defer any.cfg.ReturnIterator(iter)
- iter.ReadMapCB(func(iter *Iterator, field string) bool {
- iter.Skip()
- keys = append(keys, field)
- return true
- })
- return keys
- }
- 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
- }
- func (any *objectLazyAny) WriteTo(stream *Stream) {
- stream.Write(any.buf)
- }
- func (any *objectLazyAny) GetInterface() interface{} {
- iter := any.cfg.BorrowIterator(any.buf)
- defer any.cfg.ReturnIterator(iter)
- return iter.Read()
- }
- type objectAny struct {
- baseAny
- err error
- val reflect.Value
- }
- func wrapStruct(val interface{}) *objectAny {
- return &objectAny{baseAny{}, nil, reflect.ValueOf(val)}
- }
- func (any *objectAny) ValueType() ValueType {
- return ObjectValue
- }
- func (any *objectAny) MustBeValid() Any {
- return any
- }
- func (any *objectAny) Parse() *Iterator {
- return nil
- }
- func (any *objectAny) LastError() error {
- return any.err
- }
- func (any *objectAny) ToBool() bool {
- return any.val.NumField() != 0
- }
- func (any *objectAny) ToInt() int {
- return 0
- }
- func (any *objectAny) ToInt32() int32 {
- return 0
- }
- func (any *objectAny) ToInt64() int64 {
- return 0
- }
- func (any *objectAny) ToUint() uint {
- return 0
- }
- func (any *objectAny) ToUint32() uint32 {
- return 0
- }
- func (any *objectAny) ToUint64() uint64 {
- return 0
- }
- func (any *objectAny) ToFloat32() float32 {
- return 0
- }
- func (any *objectAny) ToFloat64() float64 {
- return 0
- }
- func (any *objectAny) ToString() string {
- str, err := MarshalToString(any.val.Interface())
- any.err = err
- return str
- }
- func (any *objectAny) Get(path ...interface{}) Any {
- if len(path) == 0 {
- return any
- }
- switch firstPath := path[0].(type) {
- case string:
- field := any.val.FieldByName(firstPath)
- if !field.IsValid() {
- return newInvalidAny(path)
- }
- return Wrap(field.Interface())
- 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() != InvalidValue {
- mappedAll[any.val.Type().Field(i).Name] = mapped
- }
- }
- }
- return wrapMap(mappedAll)
- }
- return newInvalidAny(path)
- default:
- return newInvalidAny(path)
- }
- }
- 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)
- }
- return keys
- }
- func (any *objectAny) Size() int {
- return any.val.NumField()
- }
- func (any *objectAny) WriteTo(stream *Stream) {
- stream.WriteVal(any.val)
- }
- func (any *objectAny) GetInterface() interface{} {
- return any.val.Interface()
- }
- type mapAny struct {
- baseAny
- err error
- val reflect.Value
- }
- func wrapMap(val interface{}) *mapAny {
- return &mapAny{baseAny{}, nil, reflect.ValueOf(val)}
- }
- func (any *mapAny) ValueType() ValueType {
- return ObjectValue
- }
- func (any *mapAny) MustBeValid() Any {
- return any
- }
- func (any *mapAny) Parse() *Iterator {
- return nil
- }
- func (any *mapAny) LastError() error {
- return any.err
- }
- func (any *mapAny) ToBool() bool {
- return true
- }
- func (any *mapAny) ToInt() int {
- return 0
- }
- func (any *mapAny) ToInt32() int32 {
- return 0
- }
- func (any *mapAny) ToInt64() int64 {
- return 0
- }
- func (any *mapAny) ToUint() uint {
- return 0
- }
- func (any *mapAny) ToUint32() uint32 {
- return 0
- }
- func (any *mapAny) ToUint64() uint64 {
- return 0
- }
- func (any *mapAny) ToFloat32() float32 {
- return 0
- }
- func (any *mapAny) ToFloat64() float64 {
- return 0
- }
- func (any *mapAny) ToString() string {
- str, err := MarshalToString(any.val.Interface())
- any.err = err
- return str
- }
- func (any *mapAny) Get(path ...interface{}) Any {
- if len(path) == 0 {
- return any
- }
- switch firstPath := path[0].(type) {
- case int32:
- if '*' == firstPath {
- mappedAll := map[string]Any{}
- for _, key := range any.val.MapKeys() {
- keyAsStr := key.String()
- element := Wrap(any.val.MapIndex(key).Interface())
- mapped := element.Get(path[1:]...)
- if mapped.ValueType() != InvalidValue {
- mappedAll[keyAsStr] = mapped
- }
- }
- return wrapMap(mappedAll)
- }
- return newInvalidAny(path)
- default:
- value := any.val.MapIndex(reflect.ValueOf(firstPath))
- if !value.IsValid() {
- return newInvalidAny(path)
- }
- return Wrap(value.Interface())
- }
- }
- func (any *mapAny) Keys() []string {
- keys := make([]string, 0, any.val.Len())
- for _, key := range any.val.MapKeys() {
- keys = append(keys, key.String())
- }
- return keys
- }
- func (any *mapAny) Size() int {
- return any.val.Len()
- }
- func (any *mapAny) WriteTo(stream *Stream) {
- stream.WriteVal(any.val)
- }
- func (any *mapAny) GetInterface() interface{} {
- return any.val.Interface()
- }
|