mirror of
https://github.com/json-iterator/go.git
synced 2024-11-24 08:22:14 +02:00
expose Parse from Any
This commit is contained in:
parent
4d7e181f9f
commit
97472ecd96
@ -19,6 +19,7 @@ type Any interface {
|
||||
GetObject() map[string]Any
|
||||
SetObject(map[string]Any) bool
|
||||
WriteTo(stream *Stream)
|
||||
Parse() *Iterator
|
||||
}
|
||||
|
||||
type baseAny struct{}
|
||||
|
@ -13,7 +13,7 @@ type arrayLazyAny struct {
|
||||
remaining []byte
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) parse() *Iterator {
|
||||
func (any *arrayLazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
@ -37,7 +37,7 @@ func (any *arrayLazyAny) fillCacheUntil(target int) Any {
|
||||
if target < i {
|
||||
return any.cache[target]
|
||||
}
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
if (len(any.remaining) == len(any.buf)) {
|
||||
iter.head++
|
||||
c := iter.nextToken()
|
||||
@ -47,11 +47,13 @@ func (any *arrayLazyAny) fillCacheUntil(target int) Any {
|
||||
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
|
||||
}
|
||||
}
|
||||
@ -60,11 +62,13 @@ func (any *arrayLazyAny) fillCacheUntil(target int) Any {
|
||||
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
|
||||
}
|
||||
|
||||
@ -75,7 +79,7 @@ func (any *arrayLazyAny) fillCache() {
|
||||
if any.cache == nil {
|
||||
any.cache = make([]Any, 0, 8)
|
||||
}
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
if len(any.remaining) == len(any.buf) {
|
||||
iter.head++
|
||||
c := iter.nextToken()
|
||||
@ -84,6 +88,7 @@ func (any *arrayLazyAny) fillCache() {
|
||||
any.cache = append(any.cache, iter.readAny(iter))
|
||||
} else {
|
||||
any.remaining = nil
|
||||
any.err = iter.Error
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -91,7 +96,7 @@ func (any *arrayLazyAny) fillCache() {
|
||||
any.cache = append(any.cache, iter.readAny(iter))
|
||||
}
|
||||
any.remaining = nil
|
||||
return
|
||||
any.err = iter.Error
|
||||
}
|
||||
|
||||
func (any *arrayLazyAny) LastError() error {
|
||||
@ -185,14 +190,13 @@ func (any *arrayLazyAny) Size() int {
|
||||
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 := any.Parse()
|
||||
iter.head++
|
||||
c := iter.nextToken()
|
||||
if c != ']' {
|
||||
@ -204,6 +208,7 @@ func (any *arrayLazyAny) IterateArray() (func() (Any, bool), bool) {
|
||||
} else {
|
||||
remaining = nil
|
||||
any.remaining = nil
|
||||
any.err = iter.Error
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
@ -234,18 +239,18 @@ func (any *arrayLazyAny) IterateArray() (func() (Any, bool), bool) {
|
||||
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
|
||||
|
@ -40,6 +40,10 @@ func (any *trueAny) WriteTo(stream *Stream) {
|
||||
stream.WriteTrue()
|
||||
}
|
||||
|
||||
func (any *trueAny) Parse() *Iterator {
|
||||
return nil
|
||||
}
|
||||
|
||||
type falseAny struct {
|
||||
baseAny
|
||||
}
|
||||
@ -79,3 +83,7 @@ func (any *falseAny) ToString() string {
|
||||
func (any *falseAny) WriteTo(stream *Stream) {
|
||||
stream.WriteFalse()
|
||||
}
|
||||
|
||||
func (any *falseAny) Parse() *Iterator {
|
||||
return nil
|
||||
}
|
||||
|
@ -13,15 +13,20 @@ type floatLazyAny struct {
|
||||
cache float64
|
||||
}
|
||||
|
||||
func (any *floatLazyAny) fillCache() {
|
||||
if any.err != nil {
|
||||
return
|
||||
}
|
||||
func (any *floatLazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
}
|
||||
iter.ResetBytes(any.buf)
|
||||
return iter
|
||||
}
|
||||
|
||||
func (any *floatLazyAny) fillCache() {
|
||||
if any.err != nil {
|
||||
return
|
||||
}
|
||||
iter := any.Parse()
|
||||
any.cache = iter.ReadFloat64()
|
||||
if iter.Error != io.EOF {
|
||||
iter.reportError("floatLazyAny", "there are bytes left")
|
||||
|
@ -14,15 +14,20 @@ type intLazyAny struct {
|
||||
cache int64
|
||||
}
|
||||
|
||||
func (any *intLazyAny) fillCache() {
|
||||
if any.err != nil {
|
||||
return
|
||||
}
|
||||
func (any *intLazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
}
|
||||
iter.ResetBytes(any.buf)
|
||||
return iter
|
||||
}
|
||||
|
||||
func (any *intLazyAny) fillCache() {
|
||||
if any.err != nil {
|
||||
return
|
||||
}
|
||||
iter := any.Parse()
|
||||
any.cache = iter.ReadInt64()
|
||||
if iter.Error != io.EOF {
|
||||
iter.reportError("intLazyAny", "there are bytes left")
|
||||
@ -111,4 +116,8 @@ func (any *intAny) ToString() string {
|
||||
|
||||
func (any *intAny) WriteTo(stream *Stream) {
|
||||
stream.WriteInt64(any.val)
|
||||
}
|
||||
|
||||
func (any *intAny) Parse() *Iterator {
|
||||
return nil
|
||||
}
|
@ -42,3 +42,7 @@ func (any *invalidAny) WriteTo(stream *Stream) {
|
||||
func (any *invalidAny) Get(path ...interface{}) Any {
|
||||
return any
|
||||
}
|
||||
|
||||
func (any *invalidAny) Parse() *Iterator {
|
||||
return nil
|
||||
}
|
||||
|
@ -38,4 +38,8 @@ func (any *nilAny) ToString() string {
|
||||
|
||||
func (any *nilAny) WriteTo(stream *Stream) {
|
||||
stream.WriteNil()
|
||||
}
|
||||
|
||||
func (any *nilAny) Parse() *Iterator {
|
||||
return nil
|
||||
}
|
@ -13,7 +13,7 @@ type objectLazyAny struct {
|
||||
remaining []byte
|
||||
}
|
||||
|
||||
func (any *objectLazyAny) parse() *Iterator {
|
||||
func (any *objectLazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
@ -34,7 +34,7 @@ func (any *objectLazyAny) fillCacheUntil(target string) Any {
|
||||
if val != nil {
|
||||
return val
|
||||
}
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
if len(any.remaining) == len(any.buf) {
|
||||
iter.head++
|
||||
c := iter.nextToken()
|
||||
@ -45,10 +45,12 @@ func (any *objectLazyAny) fillCacheUntil(target string) Any {
|
||||
any.cache[k] = v
|
||||
if target == k {
|
||||
any.remaining = iter.buf[iter.head:]
|
||||
any.err = iter.Error
|
||||
return v
|
||||
}
|
||||
} else {
|
||||
any.remaining = nil
|
||||
any.err = iter.Error
|
||||
return nil
|
||||
}
|
||||
}
|
||||
@ -58,10 +60,12 @@ func (any *objectLazyAny) fillCacheUntil(target string) Any {
|
||||
any.cache[k] = v
|
||||
if target == k {
|
||||
any.remaining = iter.buf[iter.head:]
|
||||
any.err = iter.Error
|
||||
return v
|
||||
}
|
||||
}
|
||||
any.remaining = nil
|
||||
any.err = iter.Error
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -72,7 +76,7 @@ func (any *objectLazyAny) fillCache() {
|
||||
if any.cache == nil {
|
||||
any.cache = map[string]Any{}
|
||||
}
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
if len(any.remaining) == len(any.buf) {
|
||||
iter.head++
|
||||
c := iter.nextToken()
|
||||
@ -83,6 +87,7 @@ func (any *objectLazyAny) fillCache() {
|
||||
any.cache[k] = v
|
||||
} else {
|
||||
any.remaining = nil
|
||||
any.err = iter.Error
|
||||
return
|
||||
}
|
||||
}
|
||||
@ -92,6 +97,7 @@ func (any *objectLazyAny) fillCache() {
|
||||
any.cache[k] = v
|
||||
}
|
||||
any.remaining = nil
|
||||
any.err = iter.Error
|
||||
return
|
||||
}
|
||||
|
||||
@ -201,7 +207,7 @@ func (any *objectLazyAny) IterateObject() (func() (string, Any, bool), bool) {
|
||||
}
|
||||
remaining := any.remaining
|
||||
if len(remaining) == len(any.buf) {
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
iter.head++
|
||||
c := iter.nextToken()
|
||||
if c != '}' {
|
||||
@ -214,6 +220,7 @@ func (any *objectLazyAny) IterateObject() (func() (string, Any, bool), bool) {
|
||||
} else {
|
||||
remaining = nil
|
||||
any.remaining = nil
|
||||
any.err = iter.Error
|
||||
return nil, false
|
||||
}
|
||||
}
|
||||
@ -253,10 +260,12 @@ func (any *objectLazyAny) IterateObject() (func() (string, Any, bool), bool) {
|
||||
any.cache[nextKey] = nextValue
|
||||
remaining = iter.buf[iter.head:]
|
||||
any.remaining = remaining
|
||||
any.err = iter.Error
|
||||
return key, value, true
|
||||
} else {
|
||||
remaining = nil
|
||||
any.remaining = nil
|
||||
any.err = iter.Error
|
||||
return key, value, false
|
||||
}
|
||||
}
|
||||
|
@ -12,19 +12,7 @@ type stringLazyAny struct{
|
||||
cache string
|
||||
}
|
||||
|
||||
func (any *stringLazyAny) fillCache() {
|
||||
if any.err != nil {
|
||||
return
|
||||
}
|
||||
iter := any.parse()
|
||||
any.cache = iter.ReadString()
|
||||
if iter.Error != io.EOF {
|
||||
iter.reportError("stringLazyAny", "there are bytes left")
|
||||
}
|
||||
any.err = iter.Error
|
||||
}
|
||||
|
||||
func (any *stringLazyAny) parse() *Iterator {
|
||||
func (any *stringLazyAny) Parse() *Iterator {
|
||||
iter := any.iter
|
||||
if iter == nil {
|
||||
iter = NewIterator()
|
||||
@ -34,6 +22,18 @@ func (any *stringLazyAny) parse() *Iterator {
|
||||
return iter
|
||||
}
|
||||
|
||||
func (any *stringLazyAny) fillCache() {
|
||||
if any.err != nil {
|
||||
return
|
||||
}
|
||||
iter := any.Parse()
|
||||
any.cache = iter.ReadString()
|
||||
if iter.Error != io.EOF {
|
||||
iter.reportError("stringLazyAny", "there are bytes left")
|
||||
}
|
||||
any.err = iter.Error
|
||||
}
|
||||
|
||||
func (any *stringLazyAny) LastError() error {
|
||||
return any.err
|
||||
}
|
||||
@ -54,7 +54,7 @@ func (any *stringLazyAny) ToBool() bool {
|
||||
}
|
||||
|
||||
func (any *stringLazyAny) ToInt() int {
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
iter.head++
|
||||
val := iter.ReadInt()
|
||||
any.err = iter.Error
|
||||
@ -62,7 +62,7 @@ func (any *stringLazyAny) ToInt() int {
|
||||
}
|
||||
|
||||
func (any *stringLazyAny) ToInt32() int32 {
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
iter.head++
|
||||
val := iter.ReadInt32()
|
||||
any.err = iter.Error
|
||||
@ -70,7 +70,7 @@ func (any *stringLazyAny) ToInt32() int32 {
|
||||
}
|
||||
|
||||
func (any *stringLazyAny) ToInt64() int64 {
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
iter.head++
|
||||
val := iter.ReadInt64()
|
||||
any.err = iter.Error
|
||||
@ -78,7 +78,7 @@ func (any *stringLazyAny) ToInt64() int64 {
|
||||
}
|
||||
|
||||
func (any *stringLazyAny) ToFloat32() float32 {
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
iter.head++
|
||||
val := iter.ReadFloat32()
|
||||
any.err = iter.Error
|
||||
@ -86,7 +86,7 @@ func (any *stringLazyAny) ToFloat32() float32 {
|
||||
}
|
||||
|
||||
func (any *stringLazyAny) ToFloat64() float64 {
|
||||
iter := any.parse()
|
||||
iter := any.Parse()
|
||||
iter.head++
|
||||
val := iter.ReadFloat64()
|
||||
any.err = iter.Error
|
||||
|
Loading…
Reference in New Issue
Block a user