1
0
mirror of https://github.com/json-iterator/go.git synced 2025-04-04 21:34:16 +02:00

#61 remove internal buffer from arrayAny

This commit is contained in:
Tao Wen 2017-06-18 22:40:18 +08:00
parent 9f9ca4c9fc
commit a84cdaa694
4 changed files with 43 additions and 109 deletions

View File

@ -71,7 +71,7 @@ func WrapFloat64(val float64) Any {
} }
func WrapString(val string) Any { func WrapString(val string) Any {
return &stringAny{baseAny{}, nil, val} return &stringAny{baseAny{}, val}
} }
func Wrap(val interface{}) Any { func Wrap(val interface{}) Any {
@ -135,7 +135,7 @@ func (iter *Iterator) readAny() Any {
switch c { switch c {
case '"': case '"':
iter.unreadByte() iter.unreadByte()
return &stringAny{baseAny{}, nil, iter.ReadString()} return &stringAny{baseAny{}, iter.ReadString()}
case 'n': case 'n':
iter.skipFixedBytes(3) // null iter.skipFixedBytes(3) // null
return &nilAny{} return &nilAny{}
@ -222,6 +222,12 @@ func locatePath(iter *Iterator, path []interface{}) Any {
case int: case int:
valueBytes := locateArrayElement(iter, pathKey) valueBytes := locateArrayElement(iter, pathKey)
iter.ResetBytes(valueBytes) iter.ResetBytes(valueBytes)
case int32:
if '*' == pathKey {
return iter.readAny().Get(path[i:]...)
} else {
return newInvalidAny(path)
}
default: default:
return newInvalidAny(path[i:]) return newInvalidAny(path[i:])
} }

View File

@ -1,7 +1,6 @@
package jsoniter package jsoniter
import ( import (
"fmt"
"reflect" "reflect"
"unsafe" "unsafe"
) )
@ -166,25 +165,19 @@ func (any *arrayLazyAny) GetInterface() interface{} {
type arrayAny struct { type arrayAny struct {
baseAny baseAny
err error
cache []Any
val reflect.Value val reflect.Value
} }
func wrapArray(val interface{}) *arrayAny { func wrapArray(val interface{}) *arrayAny {
return &arrayAny{baseAny{}, nil, nil, reflect.ValueOf(val)} return &arrayAny{baseAny{}, reflect.ValueOf(val)}
} }
func (any *arrayAny) ValueType() ValueType { func (any *arrayAny) ValueType() ValueType {
return Array return Array
} }
func (any *arrayAny) Parse() *Iterator {
return nil
}
func (any *arrayAny) LastError() error { func (any *arrayAny) LastError() error {
return any.err return nil
} }
func (any *arrayAny) ToBool() bool { func (any *arrayAny) ToBool() bool {
@ -248,115 +241,54 @@ func (any *arrayAny) ToFloat64() float64 {
} }
func (any *arrayAny) ToString() string { func (any *arrayAny) ToString() string {
if len(any.cache) == 0 { str, _ := MarshalToString(any.val.Interface())
// nothing has been parsed yet return str
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 { func (any *arrayAny) Get(path ...interface{}) Any {
if len(path) == 0 { if len(path) == 0 {
return any return any
} }
var element Any
switch firstPath := path[0].(type) { switch firstPath := path[0].(type) {
case int: case int:
element = any.fillCacheUntil(firstPath) if firstPath < 0 || firstPath >= any.val.Len() {
if element == nil { return newInvalidAny(path)
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", firstPath, any.cache)}
} }
return Wrap(any.val.Index(firstPath).Interface())
case int32: case int32:
if '*' == firstPath { if '*' == firstPath {
any.fillCache() mappedAll := make([]Any, 0)
mappedAll := make([]Any, 0, len(any.cache)) for i := 0; i < any.val.Len(); i++ {
for _, element := range any.cache { mapped := Wrap(any.val.Index(i).Interface()).Get(path[1:]...)
mapped := element.Get(path[1:]...)
if mapped.ValueType() != Invalid { if mapped.ValueType() != Invalid {
mappedAll = append(mappedAll, mapped) mappedAll = append(mappedAll, mapped)
} }
} }
return wrapArray(mappedAll) return wrapArray(mappedAll)
} else { } else {
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)} return newInvalidAny(path)
} }
default: default:
element = &invalidAny{baseAny{}, fmt.Errorf("%v not found in %v", path[0], any.cache)} return newInvalidAny(path)
}
if len(path) == 1 {
return element
} else {
return element.Get(path[1:]...)
} }
} }
func (any *arrayAny) Size() int { func (any *arrayAny) Size() int {
any.fillCache() return any.val.Len()
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 { func (any *arrayAny) GetArray() []Any {
any.fillCache() elements := make([]Any, any.val.Len())
return any.cache for i := 0; i < any.val.Len(); i++ {
elements[i] = Wrap(any.val.Index(i).Interface())
}
return elements
} }
func (any *arrayAny) WriteTo(stream *Stream) { func (any *arrayAny) WriteTo(stream *Stream) {
if len(any.cache) == 0 { stream.WriteVal(any.val)
// nothing has been parsed yet
stream.WriteVal(any.val)
} else {
any.fillCache()
stream.WriteVal(any.cache)
}
} }
func (any *arrayAny) GetInterface() interface{} { func (any *arrayAny) GetInterface() interface{} {
any.fillCache() return any.val.Interface()
return any.cache
} }

View File

@ -6,7 +6,6 @@ import (
type stringAny struct { type stringAny struct {
baseAny baseAny
err error
val string val string
} }
@ -19,7 +18,7 @@ func (any *stringAny) ValueType() ValueType {
} }
func (any *stringAny) LastError() error { func (any *stringAny) LastError() error {
return any.err return nil
} }
func (any *stringAny) ToBool() bool { func (any *stringAny) ToBool() bool {
@ -38,50 +37,42 @@ func (any *stringAny) ToBool() bool {
} }
func (any *stringAny) ToInt() int { func (any *stringAny) ToInt() int {
parsed, err := strconv.ParseInt(any.val, 10, 64) parsed, _ := strconv.ParseInt(any.val, 10, 64)
any.err = err
return int(parsed) return int(parsed)
} }
func (any *stringAny) ToInt32() int32 { func (any *stringAny) ToInt32() int32 {
parsed, err := strconv.ParseInt(any.val, 10, 32) parsed, _ := strconv.ParseInt(any.val, 10, 32)
any.err = err
return int32(parsed) return int32(parsed)
} }
func (any *stringAny) ToInt64() int64 { func (any *stringAny) ToInt64() int64 {
parsed, err := strconv.ParseInt(any.val, 10, 64) parsed, _ := strconv.ParseInt(any.val, 10, 64)
any.err = err
return parsed return parsed
} }
func (any *stringAny) ToUint() uint { func (any *stringAny) ToUint() uint {
parsed, err := strconv.ParseUint(any.val, 10, 64) parsed, _ := strconv.ParseUint(any.val, 10, 64)
any.err = err
return uint(parsed) return uint(parsed)
} }
func (any *stringAny) ToUint32() uint32 { func (any *stringAny) ToUint32() uint32 {
parsed, err := strconv.ParseUint(any.val, 10, 32) parsed, _ := strconv.ParseUint(any.val, 10, 32)
any.err = err
return uint32(parsed) return uint32(parsed)
} }
func (any *stringAny) ToUint64() uint64 { func (any *stringAny) ToUint64() uint64 {
parsed, err := strconv.ParseUint(any.val, 10, 64) parsed, _ := strconv.ParseUint(any.val, 10, 64)
any.err = err
return parsed return parsed
} }
func (any *stringAny) ToFloat32() float32 { func (any *stringAny) ToFloat32() float32 {
parsed, err := strconv.ParseFloat(any.val, 32) parsed, _ := strconv.ParseFloat(any.val, 32)
any.err = err
return float32(parsed) return float32(parsed)
} }
func (any *stringAny) ToFloat64() float64 { func (any *stringAny) ToFloat64() float64 {
parsed, err := strconv.ParseFloat(any.val, 64) parsed, _ := strconv.ParseFloat(any.val, 64)
any.err = err
return parsed return parsed
} }

View File

@ -60,6 +60,8 @@ func Test_array_lazy_any_get_all(t *testing.T) {
should := require.New(t) should := require.New(t)
any := Get([]byte("[[1],[2],[3,4]]")) any := Get([]byte("[[1],[2],[3,4]]"))
should.Equal("[1,2,3]", any.Get('*', 0).ToString()) should.Equal("[1,2,3]", any.Get('*', 0).ToString())
any = Get([]byte("[[[1],[2],[3,4]]]"), 0, '*', 0)
should.Equal("[1,2,3]", any.ToString())
} }
func Test_array_wrapper_any_get_all(t *testing.T) { func Test_array_wrapper_any_get_all(t *testing.T) {
@ -70,6 +72,9 @@ func Test_array_wrapper_any_get_all(t *testing.T) {
{5, 6}, {5, 6},
}) })
should.Equal("[1,3,5]", any.Get('*', 0).ToString()) should.Equal("[1,3,5]", any.Get('*', 0).ToString())
should.Equal(Array, any.ValueType())
should.True(any.ToBool())
should.Equal(1, any.Get(0, 0).ToInt())
} }
func Test_array_lazy_any_get_invalid(t *testing.T) { func Test_array_lazy_any_get_invalid(t *testing.T) {