1
0
mirror of https://github.com/json-iterator/go.git synced 2025-06-03 22:27:26 +02:00

ReadArrayCB

This commit is contained in:
Tao Wen 2017-01-20 13:54:51 +08:00
parent 80c86e63b1
commit 928bc4ce72
4 changed files with 69 additions and 47 deletions

51
feature_iter_array.go Normal file
View File

@ -0,0 +1,51 @@
package jsoniter
func (iter *Iterator) ReadArray() (ret bool) {
c := iter.nextToken()
switch c {
case 'n':
iter.skipFixedBytes(3)
return false // null
case '[':
c = iter.nextToken()
if c != ']' {
iter.unreadByte()
return true
}
return false
case ']':
return false
case ',':
return true
default:
iter.reportError("ReadArray", "expect [ or , or ] or n, but found: " + string([]byte{c}))
return
}
}
func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) {
c := iter.nextToken()
if c == '[' {
c = iter.nextToken()
if c != ']' {
iter.unreadByte()
if !callback(iter) {
return false
}
for iter.nextToken() == ',' {
if !callback(iter) {
return false
}
}
return true
}
return true
}
if c == 'n' {
iter.skipFixedBytes(3)
return true // null
}
iter.reportError("ReadArrayCB", "expect [ or n, but found: " + string([]byte{c}))
return false
}

View File

@ -37,8 +37,7 @@ func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
if !callback(iter, field) { if !callback(iter, field) {
return false return false
} }
c = iter.nextToken() for iter.nextToken() == ',' {
for c == ',' {
field := string(iter.readObjectFieldAsBytes()) field := string(iter.readObjectFieldAsBytes())
if !callback(iter, field) { if !callback(iter, field) {
return false return false

View File

@ -223,36 +223,6 @@ func (iter *Iterator) unreadByte() {
return return
} }
// ReadArray reads a json object as Array
func (iter *Iterator) ReadArray() (ret bool) {
c := iter.nextToken()
if iter.Error != nil {
return
}
switch c {
case 'n':
iter.skipFixedBytes(3)
return false // null
case '[':
c = iter.nextToken()
if iter.Error != nil {
return
}
if c == ']' {
return false
}
iter.unreadByte()
return true
case ']':
return false
case ',':
return true
default:
iter.reportError("ReadArray", "expect [ or , or ] or n, but found: " + string([]byte{c}))
return
}
}
// ReadBase64 reads a json object as Base64 in byte slice // ReadBase64 reads a json object as Base64 in byte slice

View File

@ -8,26 +8,28 @@ import (
) )
func Test_empty_array(t *testing.T) { func Test_empty_array(t *testing.T) {
should := require.New(t)
iter := ParseString(`[]`) iter := ParseString(`[]`)
cont := iter.ReadArray() cont := iter.ReadArray()
if cont != false { should.False(cont)
t.FailNow() iter = ParseString(`[]`)
} iter.ReadArrayCB(func(iter *Iterator) bool {
should.FailNow("should not call")
return true
})
} }
func Test_one_element(t *testing.T) { func Test_one_element(t *testing.T) {
should := require.New(t)
iter := ParseString(`[1]`) iter := ParseString(`[1]`)
cont := iter.ReadArray() should.True(iter.ReadArray())
if cont != true { should.Equal(1, iter.ReadInt())
t.FailNow() should.False(iter.ReadArray())
} iter = ParseString(`[1]`)
if iter.ReadInt64() != 1 { iter.ReadArrayCB(func(iter *Iterator) bool {
t.FailNow() should.Equal(1, iter.ReadInt())
} return true
cont = iter.ReadArray() })
if cont != false {
t.FailNow()
}
} }
func Test_two_elements(t *testing.T) { func Test_two_elements(t *testing.T) {
@ -136,7 +138,7 @@ func Test_write_array(t *testing.T) {
func Test_write_val_array(t *testing.T) { func Test_write_val_array(t *testing.T) {
should := require.New(t) should := require.New(t)
val := []int{1,2,3} val := []int{1, 2, 3}
str, err := MarshalToString(val) str, err := MarshalToString(val)
should.Nil(err) should.Nil(err)
should.Equal("[1,2,3]", str) should.Equal("[1,2,3]", str)