1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00
json-iterator/any_array.go

279 lines
4.7 KiB
Go
Raw Normal View History

2017-01-24 16:36:16 +02:00
package jsoniter
2017-01-25 18:25:17 +02:00
import (
2017-01-28 15:11:36 +02:00
"reflect"
2017-06-06 17:27:00 +02:00
"unsafe"
2017-01-25 18:25:17 +02:00
)
2017-01-24 16:36:16 +02:00
type arrayLazyAny struct {
baseAny
2017-06-19 17:43:53 +02:00
cfg *frozenConfig
buf []byte
err error
2017-01-24 16:36:16 +02:00
}
2017-01-26 10:24:01 +02:00
func (any *arrayLazyAny) ValueType() ValueType {
return ArrayValue
2017-01-26 10:24:01 +02:00
}
2017-06-19 15:21:20 +02:00
func (any *arrayLazyAny) MustBeValid() Any {
return any
}
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 {
iter := any.cfg.BorrowIterator(any.buf)
defer any.cfg.ReturnIterator(iter)
return iter.ReadArray()
2017-01-24 16:36:16 +02:00
}
func (any *arrayLazyAny) ToInt() int {
if any.ToBool() {
return 1
2017-01-24 17:13:58 +02:00
}
2017-07-09 10:15:45 +02:00
return 0
2017-01-24 16:36:16 +02:00
}
func (any *arrayLazyAny) ToInt32() int32 {
if any.ToBool() {
return 1
2017-01-24 17:13:58 +02:00
}
2017-07-09 10:15:45 +02:00
return 0
2017-01-24 16:36:16 +02:00
}
func (any *arrayLazyAny) ToInt64() int64 {
if any.ToBool() {
return 1
2017-01-24 17:13:58 +02:00
}
2017-07-09 10:15:45 +02:00
return 0
2017-01-24 16:36:16 +02:00
}
2017-01-29 10:55:32 +02:00
func (any *arrayLazyAny) ToUint() uint {
if any.ToBool() {
return 1
2017-01-29 10:55:32 +02:00
}
2017-07-09 10:15:45 +02:00
return 0
2017-01-29 10:55:32 +02:00
}
func (any *arrayLazyAny) ToUint32() uint32 {
if any.ToBool() {
return 1
2017-01-29 10:55:32 +02:00
}
2017-07-09 10:15:45 +02:00
return 0
2017-01-29 10:55:32 +02:00
}
func (any *arrayLazyAny) ToUint64() uint64 {
if any.ToBool() {
return 1
2017-01-29 10:55:32 +02:00
}
2017-07-09 10:15:45 +02:00
return 0
2017-01-29 10:55:32 +02:00
}
2017-01-24 16:36:16 +02:00
func (any *arrayLazyAny) ToFloat32() float32 {
if any.ToBool() {
return 1
2017-01-24 17:13:58 +02:00
}
2017-07-09 10:15:45 +02:00
return 0
2017-01-24 16:36:16 +02:00
}
func (any *arrayLazyAny) ToFloat64() float64 {
if any.ToBool() {
return 1
2017-01-24 17:13:58 +02:00
}
2017-07-09 10:15:45 +02:00
return 0
2017-01-24 16:36:16 +02:00
}
func (any *arrayLazyAny) ToString() string {
return *(*string)(unsafe.Pointer(&any.buf))
2017-01-24 16:36:16 +02:00
}
2017-06-19 09:40:00 +02:00
func (any *arrayLazyAny) ToVal(val interface{}) {
iter := any.cfg.BorrowIterator(any.buf)
defer any.cfg.ReturnIterator(iter)
iter.ReadVal(val)
}
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-31 14:10:16 +02:00
switch firstPath := path[0].(type) {
case int:
iter := any.cfg.BorrowIterator(any.buf)
defer any.cfg.ReturnIterator(iter)
valueBytes := locateArrayElement(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:10:16 +02:00
case int32:
if '*' == firstPath {
iter := any.cfg.BorrowIterator(any.buf)
defer any.cfg.ReturnIterator(iter)
arr := make([]Any, 0)
iter.ReadArrayCB(func(iter *Iterator) bool {
found := iter.readAny().Get(path[1:]...)
if found.ValueType() != InvalidValue {
2017-01-31 14:10:16 +02:00
arr = append(arr, found)
}
return true
})
2017-01-31 14:10:16 +02:00
return wrapArray(arr)
}
2017-07-09 10:15:45 +02:00
return newInvalidAny(path)
2017-01-31 14:10:16 +02:00
default:
return newInvalidAny(path)
2017-01-24 16:56:18 +02:00
}
2017-01-24 16:36:16 +02:00
}
func (any *arrayLazyAny) Size() int {
size := 0
iter := any.cfg.BorrowIterator(any.buf)
defer any.cfg.ReturnIterator(iter)
iter.ReadArrayCB(func(iter *Iterator) bool {
size++
iter.Skip()
return true
})
return size
2017-01-24 16:47:56 +02:00
}
2017-01-25 18:25:17 +02:00
func (any *arrayLazyAny) WriteTo(stream *Stream) {
stream.Write(any.buf)
2017-01-26 09:44:10 +02:00
}
func (any *arrayLazyAny) GetInterface() interface{} {
iter := any.cfg.BorrowIterator(any.buf)
defer any.cfg.ReturnIterator(iter)
return iter.Read()
2017-01-28 15:11:36 +02:00
}
type arrayAny struct {
baseAny
2017-06-19 17:43:53 +02:00
val reflect.Value
2017-01-28 15:11:36 +02:00
}
func wrapArray(val interface{}) *arrayAny {
return &arrayAny{baseAny{}, reflect.ValueOf(val)}
2017-01-28 15:11:36 +02:00
}
func (any *arrayAny) ValueType() ValueType {
return ArrayValue
2017-01-28 15:11:36 +02:00
}
2017-06-19 15:21:20 +02:00
func (any *arrayAny) MustBeValid() Any {
return any
}
2017-01-28 15:11:36 +02:00
func (any *arrayAny) LastError() error {
return nil
2017-01-28 15:11:36 +02:00
}
func (any *arrayAny) ToBool() bool {
2017-01-29 10:55:32 +02:00
return any.val.Len() != 0
2017-01-28 15:11:36 +02:00
}
func (any *arrayAny) ToInt() int {
2017-01-29 10:55:32 +02:00
if any.val.Len() == 0 {
2017-01-28 15:11:36 +02:00
return 0
}
return 1
}
func (any *arrayAny) ToInt32() int32 {
2017-01-29 10:55:32 +02:00
if any.val.Len() == 0 {
2017-01-28 15:11:36 +02:00
return 0
}
return 1
}
func (any *arrayAny) ToInt64() int64 {
2017-01-29 10:55:32 +02:00
if any.val.Len() == 0 {
return 0
2017-01-28 15:11:36 +02:00
}
2017-01-29 10:55:32 +02:00
return 1
}
func (any *arrayAny) ToUint() uint {
if any.val.Len() == 0 {
2017-01-28 15:11:36 +02:00
return 0
}
return 1
}
2017-01-29 10:55:32 +02:00
func (any *arrayAny) ToUint32() uint32 {
if any.val.Len() == 0 {
return 0
2017-01-28 15:11:36 +02:00
}
2017-01-29 10:55:32 +02:00
return 1
}
func (any *arrayAny) ToUint64() uint64 {
if any.val.Len() == 0 {
2017-01-28 15:11:36 +02:00
return 0
}
return 1
}
2017-01-29 10:55:32 +02:00
func (any *arrayAny) ToFloat32() float32 {
if any.val.Len() == 0 {
return 0
2017-01-28 15:11:36 +02:00
}
2017-01-29 10:55:32 +02:00
return 1
}
func (any *arrayAny) ToFloat64() float64 {
if any.val.Len() == 0 {
2017-01-28 15:11:36 +02:00
return 0
}
return 1
}
func (any *arrayAny) ToString() string {
str, _ := MarshalToString(any.val.Interface())
return str
2017-01-28 15:11:36 +02:00
}
func (any *arrayAny) Get(path ...interface{}) Any {
if len(path) == 0 {
return any
}
2017-01-31 14:10:16 +02:00
switch firstPath := path[0].(type) {
case int:
if firstPath < 0 || firstPath >= any.val.Len() {
return newInvalidAny(path)
2017-01-28 15:11:36 +02:00
}
return Wrap(any.val.Index(firstPath).Interface())
2017-01-31 14:10:16 +02:00
case int32:
if '*' == firstPath {
mappedAll := make([]Any, 0)
for i := 0; i < any.val.Len(); i++ {
mapped := Wrap(any.val.Index(i).Interface()).Get(path[1:]...)
if mapped.ValueType() != InvalidValue {
2017-01-31 14:26:35 +02:00
mappedAll = append(mappedAll, mapped)
2017-01-31 14:10:16 +02:00
}
}
2017-01-31 14:26:35 +02:00
return wrapArray(mappedAll)
2017-01-31 14:10:16 +02:00
}
2017-07-09 10:15:45 +02:00
return newInvalidAny(path)
2017-01-31 14:10:16 +02:00
default:
return newInvalidAny(path)
2017-01-28 15:11:36 +02:00
}
}
func (any *arrayAny) Size() int {
return any.val.Len()
2017-01-28 15:11:36 +02:00
}
func (any *arrayAny) WriteTo(stream *Stream) {
stream.WriteVal(any.val)
2017-01-28 15:11:36 +02:00
}
func (any *arrayAny) GetInterface() interface{} {
return any.val.Interface()
2017-06-06 17:27:00 +02:00
}