2017-01-24 16:36:16 +02:00
|
|
|
package jsoniter
|
|
|
|
|
2017-01-25 18:25:17 +02:00
|
|
|
import (
|
|
|
|
"unsafe"
|
2017-01-26 10:24:01 +02:00
|
|
|
"fmt"
|
2017-01-25 18:25:17 +02:00
|
|
|
)
|
|
|
|
|
2017-01-24 16:36:16 +02:00
|
|
|
type arrayLazyAny struct {
|
|
|
|
baseAny
|
|
|
|
buf []byte
|
|
|
|
iter *Iterator
|
|
|
|
err error
|
|
|
|
cache []Any
|
|
|
|
remaining []byte
|
|
|
|
}
|
|
|
|
|
2017-01-26 10:24:01 +02:00
|
|
|
func (any *arrayLazyAny) ValueType() ValueType {
|
|
|
|
return Array
|
|
|
|
}
|
|
|
|
|
2017-01-26 09:40:38 +02:00
|
|
|
func (any *arrayLazyAny) Parse() *Iterator {
|
2017-01-24 16:36:16 +02:00
|
|
|
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]
|
|
|
|
}
|
2017-01-24 17:13:58 +02:00
|
|
|
if any.cache == nil {
|
|
|
|
any.cache = make([]Any, 0, 8)
|
|
|
|
}
|
2017-01-24 16:36:16 +02:00
|
|
|
i := len(any.cache)
|
|
|
|
if target < i {
|
|
|
|
return any.cache[target]
|
|
|
|
}
|
2017-01-26 09:40:38 +02:00
|
|
|
iter := any.Parse()
|
2017-01-24 16:36:16 +02:00
|
|
|
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:]
|
2017-01-26 09:40:38 +02:00
|
|
|
any.err = iter.Error
|
2017-01-24 16:36:16 +02:00
|
|
|
return element
|
|
|
|
}
|
|
|
|
i = 1
|
|
|
|
} else {
|
|
|
|
any.remaining = nil
|
2017-01-26 09:40:38 +02:00
|
|
|
any.err = iter.Error
|
2017-01-24 16:36:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for iter.nextToken() == ',' {
|
|
|
|
element := iter.readAny(iter)
|
|
|
|
any.cache = append(any.cache, element)
|
|
|
|
if i == target {
|
|
|
|
any.remaining = iter.buf[iter.head:]
|
2017-01-26 09:40:38 +02:00
|
|
|
any.err = iter.Error
|
2017-01-24 16:36:16 +02:00
|
|
|
return element
|
|
|
|
}
|
|
|
|
i++
|
2017-01-26 09:40:38 +02:00
|
|
|
}
|
2017-01-24 16:36:16 +02:00
|
|
|
any.remaining = nil
|
2017-01-26 09:40:38 +02:00
|
|
|
any.err = iter.Error
|
2017-01-24 16:36:16 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) fillCache() {
|
|
|
|
if any.remaining == nil {
|
|
|
|
return
|
|
|
|
}
|
2017-01-24 17:13:58 +02:00
|
|
|
if any.cache == nil {
|
|
|
|
any.cache = make([]Any, 0, 8)
|
|
|
|
}
|
2017-01-26 09:40:38 +02:00
|
|
|
iter := any.Parse()
|
2017-01-24 16:36:16 +02:00
|
|
|
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
|
2017-01-26 09:40:38 +02:00
|
|
|
any.err = iter.Error
|
2017-01-24 16:36:16 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for iter.nextToken() == ',' {
|
|
|
|
any.cache = append(any.cache, iter.readAny(iter))
|
|
|
|
}
|
|
|
|
any.remaining = nil
|
2017-01-26 09:40:38 +02:00
|
|
|
any.err = iter.Error
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) LastError() error {
|
2017-01-26 08:56:31 +02:00
|
|
|
return any.err
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) ToBool() bool {
|
2017-01-24 17:13:58 +02:00
|
|
|
if any.cache == nil {
|
|
|
|
any.IterateArray() // trigger first element read
|
|
|
|
}
|
|
|
|
return len(any.cache) != 0
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) ToInt() int {
|
2017-01-24 17:13:58 +02:00
|
|
|
if any.cache == nil {
|
|
|
|
any.IterateArray() // trigger first element read
|
|
|
|
}
|
|
|
|
if len(any.cache) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) ToInt32() int32 {
|
2017-01-24 17:13:58 +02:00
|
|
|
if any.cache == nil {
|
|
|
|
any.IterateArray() // trigger first element read
|
|
|
|
}
|
|
|
|
if len(any.cache) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) ToInt64() int64 {
|
2017-01-24 17:13:58 +02:00
|
|
|
if any.cache == nil {
|
|
|
|
any.IterateArray() // trigger first element read
|
|
|
|
}
|
|
|
|
if len(any.cache) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) ToFloat32() float32 {
|
2017-01-24 17:13:58 +02:00
|
|
|
if any.cache == nil {
|
|
|
|
any.IterateArray() // trigger first element read
|
|
|
|
}
|
|
|
|
if len(any.cache) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) ToFloat64() float64 {
|
2017-01-24 17:13:58 +02:00
|
|
|
if any.cache == nil {
|
|
|
|
any.IterateArray() // trigger first element read
|
|
|
|
}
|
|
|
|
if len(any.cache) == 0 {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return 1
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) ToString() string {
|
2017-01-26 08:56:31 +02:00
|
|
|
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
|
|
|
|
}
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) Get(path ...interface{}) Any {
|
2017-01-24 16:56:18 +02:00
|
|
|
if len(path) == 0 {
|
|
|
|
return any
|
|
|
|
}
|
2017-01-26 10:24:01 +02:00
|
|
|
var element Any
|
|
|
|
idx, ok := path[0].(int)
|
|
|
|
if ok {
|
|
|
|
element = any.fillCacheUntil(idx)
|
|
|
|
if element == nil {
|
|
|
|
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", idx, any.cache)}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", idx, any.cache)}
|
|
|
|
}
|
2017-01-24 16:56:18 +02:00
|
|
|
if len(path) == 1 {
|
2017-01-26 10:24:01 +02:00
|
|
|
return element
|
2017-01-24 16:56:18 +02:00
|
|
|
} else {
|
2017-01-26 10:24:01 +02:00
|
|
|
return element.Get(path[1:]...)
|
2017-01-24 16:56:18 +02:00
|
|
|
}
|
2017-01-24 16:36:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) Size() int {
|
|
|
|
any.fillCache()
|
|
|
|
return len(any.cache)
|
|
|
|
}
|
2017-01-24 16:47:56 +02:00
|
|
|
|
|
|
|
func (any *arrayLazyAny) IterateArray() (func() (Any, bool), bool) {
|
2017-01-24 17:13:58 +02:00
|
|
|
if any.cache == nil {
|
|
|
|
any.cache = make([]Any, 0, 8)
|
|
|
|
}
|
2017-01-24 16:47:56 +02:00
|
|
|
remaining := any.remaining
|
|
|
|
if len(remaining) == len(any.buf) {
|
2017-01-26 09:40:38 +02:00
|
|
|
iter := any.Parse()
|
2017-01-24 16:47:56 +02:00
|
|
|
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
|
2017-01-26 09:40:38 +02:00
|
|
|
any.err = iter.Error
|
2017-01-24 16:47:56 +02:00
|
|
|
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
|
2017-01-26 09:40:38 +02:00
|
|
|
any.err = iter.Error
|
2017-01-24 16:47:56 +02:00
|
|
|
return value, true
|
|
|
|
} else {
|
|
|
|
remaining = nil
|
|
|
|
any.remaining = nil
|
2017-01-26 09:40:38 +02:00
|
|
|
any.err = iter.Error
|
2017-01-24 16:47:56 +02:00
|
|
|
return value, false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, true
|
|
|
|
}
|
2017-01-25 18:25:17 +02:00
|
|
|
|
|
|
|
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
|
2017-01-26 08:56:31 +02:00
|
|
|
stream.Write(any.buf)
|
2017-01-25 18:25:17 +02:00
|
|
|
} else {
|
|
|
|
any.fillCache()
|
|
|
|
stream.WriteVal(any.cache)
|
|
|
|
}
|
2017-01-26 09:44:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (any *arrayLazyAny) GetInterface() interface{} {
|
|
|
|
any.fillCache()
|
|
|
|
return any.cache
|
2017-01-25 18:25:17 +02:00
|
|
|
}
|