khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 1 | package jsoniter |
| 2 | |
| 3 | // ReadArray read array element, tells if the array has more element to read. |
| 4 | func (iter *Iterator) ReadArray() (ret bool) { |
| 5 | c := iter.nextToken() |
| 6 | switch c { |
| 7 | case 'n': |
| 8 | iter.skipThreeBytes('u', 'l', 'l') |
| 9 | return false // null |
| 10 | case '[': |
| 11 | c = iter.nextToken() |
| 12 | if c != ']' { |
| 13 | iter.unreadByte() |
| 14 | return true |
| 15 | } |
| 16 | return false |
| 17 | case ']': |
| 18 | return false |
| 19 | case ',': |
| 20 | return true |
| 21 | default: |
| 22 | iter.ReportError("ReadArray", "expect [ or , or ] or n, but found "+string([]byte{c})) |
| 23 | return |
| 24 | } |
| 25 | } |
| 26 | |
| 27 | // ReadArrayCB read array with callback |
| 28 | func (iter *Iterator) ReadArrayCB(callback func(*Iterator) bool) (ret bool) { |
| 29 | c := iter.nextToken() |
| 30 | if c == '[' { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 31 | if !iter.incrementDepth() { |
| 32 | return false |
| 33 | } |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 34 | c = iter.nextToken() |
| 35 | if c != ']' { |
| 36 | iter.unreadByte() |
| 37 | if !callback(iter) { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 38 | iter.decrementDepth() |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 39 | return false |
| 40 | } |
| 41 | c = iter.nextToken() |
| 42 | for c == ',' { |
| 43 | if !callback(iter) { |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 44 | iter.decrementDepth() |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 45 | return false |
| 46 | } |
| 47 | c = iter.nextToken() |
| 48 | } |
| 49 | if c != ']' { |
| 50 | iter.ReportError("ReadArrayCB", "expect ] in the end, but found "+string([]byte{c})) |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 51 | iter.decrementDepth() |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 52 | return false |
| 53 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 54 | return iter.decrementDepth() |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 55 | } |
khenaidoo | 2672188 | 2021-08-11 17:42:52 -0400 | [diff] [blame] | 56 | return iter.decrementDepth() |
khenaidoo | 59ce9dd | 2019-11-11 13:05:32 -0500 | [diff] [blame] | 57 | } |
| 58 | if c == 'n' { |
| 59 | iter.skipThreeBytes('u', 'l', 'l') |
| 60 | return true // null |
| 61 | } |
| 62 | iter.ReportError("ReadArrayCB", "expect [ or n, but found "+string([]byte{c})) |
| 63 | return false |
| 64 | } |