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

59 lines
1.2 KiB
Go
Raw Normal View History

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