iter_array.go 1.4 KB

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