1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-23 18:54:21 +02:00
json-iterator/feature_iter_array.go

59 lines
1.2 KiB
Go
Raw Normal View History

2017-01-20 13:54:51 +08:00
package jsoniter
2017-07-09 16:09:23 +08:00
// ReadArray read array element, tells if the array has more element to read.
2017-01-20 13:54:51 +08:00
func (iter *Iterator) ReadArray() (ret bool) {
c := iter.nextToken()
switch c {
case 'n':
2017-07-10 15:23:35 +08:00
iter.skipThreeBytes('u', 'l', 'l')
2017-01-20 13:54:51 +08:00
return false // null
case '[':
c = iter.nextToken()
if c != ']' {
iter.unreadByte()
return true
}
return false
case ']':
return false
case ',':
return true
default:
2017-06-20 15:11:01 +08:00
iter.ReportError("ReadArray", "expect [ or , or ] or n, but found: "+string([]byte{c}))
2017-01-20 13:54:51 +08:00
return
}
}
2017-07-09 16:09:23 +08:00
// ReadArrayCB read array with callback
2017-01-20 13:54:51 +08:00
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
}
c = iter.nextToken()
for c == ',' {
2017-01-20 13:54:51 +08:00
if !callback(iter) {
return false
}
c = iter.nextToken()
}
if c != ']' {
iter.ReportError("ReadArrayCB", "expect ] in the end")
return false
2017-01-20 13:54:51 +08:00
}
return true
}
return true
}
if c == 'n' {
2017-07-10 15:23:35 +08:00
iter.skipThreeBytes('u', 'l', 'l')
2017-01-20 13:54:51 +08:00
return true // null
}
2017-06-20 15:11:01 +08:00
iter.ReportError("ReadArrayCB", "expect [ or n, but found: "+string([]byte{c}))
2017-01-20 13:54:51 +08:00
return false
2017-06-06 23:27:00 +08:00
}