iter_array.go 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. c = iter.nextToken()
  30. if c != ']' {
  31. iter.unreadByte()
  32. if !callback(iter) {
  33. return false
  34. }
  35. c = iter.nextToken()
  36. for c == ',' {
  37. if !callback(iter) {
  38. return false
  39. }
  40. c = iter.nextToken()
  41. }
  42. if c != ']' {
  43. iter.ReportError("ReadArrayCB", "expect ] in the end, but found "+string([]byte{c}))
  44. return false
  45. }
  46. return true
  47. }
  48. return true
  49. }
  50. if c == 'n' {
  51. iter.skipThreeBytes('u', 'l', 'l')
  52. return true // null
  53. }
  54. iter.ReportError("ReadArrayCB", "expect [ or n, but found "+string([]byte{c}))
  55. return false
  56. }