123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539 |
- package jsoniter
- import (
- "fmt"
- "reflect"
- "unsafe"
- )
- type arrayLazyAny struct {
- baseAny
- buf []byte
- iter *Iterator
- err error
- cache []Any
- remaining []byte
- }
- func (any *arrayLazyAny) ValueType() ValueType {
- return Array
- }
- func (any *arrayLazyAny) Parse() *Iterator {
- iter := any.iter
- if iter == nil {
- iter = NewIterator()
- any.iter = iter
- }
- iter.ResetBytes(any.remaining)
- return iter
- }
- func (any *arrayLazyAny) fillCacheUntil(target int) Any {
- if any.remaining == nil {
- if target >= len(any.cache) {
- return nil
- }
- return any.cache[target]
- }
- if any.cache == nil {
- any.cache = make([]Any, 0, 8)
- }
- i := len(any.cache)
- if target < i {
- return any.cache[target]
- }
- iter := any.Parse()
- if len(any.remaining) == len(any.buf) {
- iter.head++
- c := iter.nextToken()
- if c != ']' {
- iter.unreadByte()
- element := iter.readAny(iter)
- any.cache = append(any.cache, element)
- if target == 0 {
- any.remaining = iter.buf[iter.head:]
- any.err = iter.Error
- return element
- }
- i = 1
- } else {
- any.remaining = nil
- any.err = iter.Error
- return nil
- }
- }
- for iter.nextToken() == ',' {
- element := iter.readAny(iter)
- any.cache = append(any.cache, element)
- if i == target {
- any.remaining = iter.buf[iter.head:]
- any.err = iter.Error
- return element
- }
- i++
- }
- any.remaining = nil
- any.err = iter.Error
- return nil
- }
- func (any *arrayLazyAny) fillCache() {
- if any.remaining == nil {
- return
- }
- if any.cache == nil {
- any.cache = make([]Any, 0, 8)
- }
- iter := any.Parse()
- if len(any.remaining) == len(any.buf) {
- iter.head++
- c := iter.nextToken()
- if c != ']' {
- iter.unreadByte()
- any.cache = append(any.cache, iter.readAny(iter))
- } else {
- any.remaining = nil
- any.err = iter.Error
- return
- }
- }
- for iter.nextToken() == ',' {
- any.cache = append(any.cache, iter.readAny(iter))
- }
- any.remaining = nil
- any.err = iter.Error
- }
- func (any *arrayLazyAny) LastError() error {
- return any.err
- }
- func (any *arrayLazyAny) ToBool() bool {
- if any.cache == nil {
- any.IterateArray() // trigger first element read
- }
- return len(any.cache) != 0
- }
- func (any *arrayLazyAny) ToInt() int {
- if any.cache == nil {
- any.IterateArray() // trigger first element read
- }
- if len(any.cache) == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayLazyAny) ToInt32() int32 {
- if any.cache == nil {
- any.IterateArray() // trigger first element read
- }
- if len(any.cache) == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayLazyAny) ToInt64() int64 {
- if any.cache == nil {
- any.IterateArray() // trigger first element read
- }
- if len(any.cache) == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayLazyAny) ToUint() uint {
- if any.cache == nil {
- any.IterateArray() // trigger first element read
- }
- if len(any.cache) == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayLazyAny) ToUint32() uint32 {
- if any.cache == nil {
- any.IterateArray() // trigger first element read
- }
- if len(any.cache) == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayLazyAny) ToUint64() uint64 {
- if any.cache == nil {
- any.IterateArray() // trigger first element read
- }
- if len(any.cache) == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayLazyAny) ToFloat32() float32 {
- if any.cache == nil {
- any.IterateArray() // trigger first element read
- }
- if len(any.cache) == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayLazyAny) ToFloat64() float64 {
- if any.cache == nil {
- any.IterateArray() // trigger first element read
- }
- if len(any.cache) == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayLazyAny) ToString() string {
- if len(any.remaining) == len(any.buf) {
- // nothing has been parsed yet
- return *(*string)(unsafe.Pointer(&any.buf))
- } else {
- any.fillCache()
- str, err := MarshalToString(any.cache)
- any.err = err
- return str
- }
- }
- func (any *arrayLazyAny) Get(path ...interface{}) Any {
- if len(path) == 0 {
- return any
- }
- var element Any
- switch firstPath := path[0].(type) {
- case int:
- element = any.fillCacheUntil(firstPath)
- if element == nil {
- element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
- }
- case int32:
- if '*' == firstPath {
- any.fillCache()
- arr := make([]Any, 0, len(any.cache))
- for _, element := range any.cache {
- found := element.Get(path[1:]...)
- if found.ValueType() != Invalid {
- arr = append(arr, found)
- }
- }
- return wrapArray(arr)
- } else {
- element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
- }
- default:
- element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
- }
- if len(path) == 1 {
- return element
- } else {
- return element.Get(path[1:]...)
- }
- }
- func (any *arrayLazyAny) Size() int {
- any.fillCache()
- return len(any.cache)
- }
- func (any *arrayLazyAny) IterateArray() (func() (Any, bool), bool) {
- if any.cache == nil {
- any.cache = make([]Any, 0, 8)
- }
- remaining := any.remaining
- if len(remaining) == len(any.buf) {
- iter := any.Parse()
- iter.head++
- c := iter.nextToken()
- if c != ']' {
- iter.unreadByte()
- v := iter.readAny(iter)
- any.cache = append(any.cache, v)
- remaining = iter.buf[iter.head:]
- any.remaining = remaining
- } else {
- remaining = nil
- any.remaining = nil
- any.err = iter.Error
- return nil, false
- }
- }
- if len(any.cache) == 0 {
- return nil, false
- }
- arr := any.cache
- nextValue := arr[0]
- i := 1
- return func() (Any, bool) {
- value := nextValue
- if i < len(arr) {
- // read from cache
- nextValue = arr[i]
- i++
- return value, true
- } else {
- // read from buffer
- iter := any.iter
- if iter == nil {
- iter = NewIterator()
- any.iter = iter
- }
- iter.ResetBytes(remaining)
- c := iter.nextToken()
- if c == ',' {
- nextValue = iter.readAny(iter)
- any.cache = append(any.cache, nextValue)
- remaining = iter.buf[iter.head:]
- any.remaining = remaining
- any.err = iter.Error
- return value, true
- } else {
- remaining = nil
- any.remaining = nil
- any.err = iter.Error
- return value, false
- }
- }
- }, true
- }
- func (any *arrayLazyAny) GetArray() []Any {
- any.fillCache()
- return any.cache
- }
- func (any *arrayLazyAny) SetArray(newList []Any) bool {
- any.fillCache()
- any.cache = newList
- return true
- }
- func (any *arrayLazyAny) WriteTo(stream *Stream) {
- if len(any.remaining) == len(any.buf) {
- // nothing has been parsed yet
- stream.Write(any.buf)
- } else {
- any.fillCache()
- stream.WriteVal(any.cache)
- }
- }
- func (any *arrayLazyAny) GetInterface() interface{} {
- any.fillCache()
- return any.cache
- }
- type arrayAny struct {
- baseAny
- err error
- cache []Any
- val reflect.Value
- }
- func wrapArray(val interface{}) *arrayAny {
- return &arrayAny{baseAny{}, nil, nil, reflect.ValueOf(val)}
- }
- func (any *arrayAny) ValueType() ValueType {
- return Array
- }
- func (any *arrayAny) Parse() *Iterator {
- return nil
- }
- func (any *arrayAny) LastError() error {
- return any.err
- }
- func (any *arrayAny) ToBool() bool {
- return any.val.Len() != 0
- }
- func (any *arrayAny) ToInt() int {
- if any.val.Len() == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayAny) ToInt32() int32 {
- if any.val.Len() == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayAny) ToInt64() int64 {
- if any.val.Len() == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayAny) ToUint() uint {
- if any.val.Len() == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayAny) ToUint32() uint32 {
- if any.val.Len() == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayAny) ToUint64() uint64 {
- if any.val.Len() == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayAny) ToFloat32() float32 {
- if any.val.Len() == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayAny) ToFloat64() float64 {
- if any.val.Len() == 0 {
- return 0
- }
- return 1
- }
- func (any *arrayAny) ToString() string {
- if len(any.cache) == 0 {
- // nothing has been parsed yet
- str, err := MarshalToString(any.val.Interface())
- any.err = err
- return str
- } else {
- any.fillCache()
- str, err := MarshalToString(any.cache)
- any.err = err
- return str
- }
- }
- func (any *arrayAny) fillCacheUntil(idx int) Any {
- if idx < len(any.cache) {
- return any.cache[idx]
- } else {
- for i := len(any.cache); i < any.val.Len(); i++ {
- element := Wrap(any.val.Index(i).Interface())
- any.cache = append(any.cache, element)
- if idx == i {
- return element
- }
- }
- return nil
- }
- }
- func (any *arrayAny) fillCache() {
- any.cache = make([]Any, any.val.Len())
- for i := 0; i < any.val.Len(); i++ {
- any.cache[i] = Wrap(any.val.Index(i).Interface())
- }
- }
- func (any *arrayAny) Get(path ...interface{}) Any {
- if len(path) == 0 {
- return any
- }
- var element Any
- switch firstPath := path[0].(type) {
- case int:
- element = any.fillCacheUntil(firstPath)
- if element == nil {
- element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
- }
- case int32:
- if '*' == firstPath {
- any.fillCache()
- mappedAll := make([]Any, 0, len(any.cache))
- for _, element := range any.cache {
- mapped := element.Get(path[1:]...)
- if mapped.ValueType() != Invalid {
- mappedAll = append(mappedAll, mapped)
- }
- }
- return wrapArray(mappedAll)
- } else {
- element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
- }
- default:
- element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)}
- }
- if len(path) == 1 {
- return element
- } else {
- return element.Get(path[1:]...)
- }
- }
- func (any *arrayAny) Size() int {
- any.fillCache()
- return len(any.cache)
- }
- func (any *arrayAny) IterateArray() (func() (Any, bool), bool) {
- if any.val.Len() == 0 {
- return nil, false
- }
- i := 0
- return func() (Any, bool) {
- if i == any.val.Len() {
- return nil, false
- }
- if i == len(any.cache) {
- any.cache = append(any.cache, Wrap(any.val.Index(i).Interface()))
- }
- val := any.cache[i]
- i++
- return val, i != any.val.Len()
- }, true
- }
- func (any *arrayAny) GetArray() []Any {
- any.fillCache()
- return any.cache
- }
- func (any *arrayAny) SetArray(newList []Any) bool {
- any.fillCache()
- any.cache = newList
- return true
- }
- func (any *arrayAny) WriteTo(stream *Stream) {
- if len(any.cache) == 0 {
- // nothing has been parsed yet
- stream.WriteVal(any.val)
- } else {
- any.fillCache()
- stream.WriteVal(any.cache)
- }
- }
- func (any *arrayAny) GetInterface() interface{} {
- any.fillCache()
- return any.cache
- }
|