1
0
mirror of https://github.com/json-iterator/go.git synced 2024-12-09 08:55:35 +02:00
json-iterator/iter_array.go

65 lines
1.4 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 == '[' {
2019-10-08 17:17:01 +02:00
if !iter.incrementDepth() {
return false
}
2017-01-20 07:54:51 +02:00
c = iter.nextToken()
if c != ']' {
iter.unreadByte()
if !callback(iter) {
2019-10-08 17:17:01 +02:00
iter.decrementDepth()
2017-01-20 07:54:51 +02:00
return false
}
c = iter.nextToken()
for c == ',' {
2017-01-20 07:54:51 +02:00
if !callback(iter) {
2019-10-08 17:17:01 +02:00
iter.decrementDepth()
2017-01-20 07:54:51 +02:00
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}))
2019-10-08 17:17:01 +02:00
iter.decrementDepth()
return false
2017-01-20 07:54:51 +02:00
}
2019-10-08 17:17:01 +02:00
return iter.decrementDepth()
2017-01-20 07:54:51 +02:00
}
2019-10-08 17:17:01 +02:00
return iter.decrementDepth()
2017-01-20 07:54:51 +02:00
}
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
}