iter_skip.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package jsoniter
  2. import "fmt"
  3. // ReadNil reads a json object as nil and
  4. // returns whether it's a nil or not
  5. func (iter *Iterator) ReadNil() (ret bool) {
  6. c := iter.nextToken()
  7. if c == 'n' {
  8. iter.skipThreeBytes('u', 'l', 'l') // null
  9. return true
  10. }
  11. iter.unreadByte()
  12. return false
  13. }
  14. // ReadBool reads a json object as BoolValue
  15. func (iter *Iterator) ReadBool() (ret bool) {
  16. c := iter.nextToken()
  17. if c == 't' {
  18. iter.skipThreeBytes('r', 'u', 'e')
  19. return true
  20. }
  21. if c == 'f' {
  22. iter.skipFourBytes('a', 'l', 's', 'e')
  23. return false
  24. }
  25. iter.ReportError("ReadBool", "expect t or f, but found "+string([]byte{c}))
  26. return
  27. }
  28. // SkipAndReturnBytes skip next JSON element, and return its content as []byte.
  29. // The []byte can be kept, it is a copy of data.
  30. func (iter *Iterator) SkipAndReturnBytes() []byte {
  31. iter.startCapture(iter.head)
  32. iter.Skip()
  33. return iter.stopCapture()
  34. }
  35. type captureBuffer struct {
  36. startedAt int
  37. captured []byte
  38. }
  39. func (iter *Iterator) startCapture(captureStartedAt int) {
  40. if iter.captured != nil {
  41. panic("already in capture mode")
  42. }
  43. iter.captureStartedAt = captureStartedAt
  44. iter.captured = make([]byte, 0, 32)
  45. }
  46. func (iter *Iterator) stopCapture() []byte {
  47. if iter.captured == nil {
  48. panic("not in capture mode")
  49. }
  50. captured := iter.captured
  51. remaining := iter.buf[iter.captureStartedAt:iter.head]
  52. iter.captureStartedAt = -1
  53. iter.captured = nil
  54. if len(captured) == 0 {
  55. copied := make([]byte, len(remaining))
  56. copy(copied, remaining)
  57. return copied
  58. }
  59. captured = append(captured, remaining...)
  60. return captured
  61. }
  62. // Skip skips a json object and positions to relatively the next json object
  63. func (iter *Iterator) Skip() {
  64. c := iter.nextToken()
  65. switch c {
  66. case '"':
  67. iter.skipString()
  68. case 'n':
  69. iter.skipThreeBytes('u', 'l', 'l') // null
  70. case 't':
  71. iter.skipThreeBytes('r', 'u', 'e') // true
  72. case 'f':
  73. iter.skipFourBytes('a', 'l', 's', 'e') // false
  74. case '0':
  75. iter.unreadByte()
  76. iter.ReadFloat32()
  77. case '-', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  78. iter.skipNumber()
  79. case '[':
  80. iter.skipArray()
  81. case '{':
  82. iter.skipObject()
  83. default:
  84. iter.ReportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
  85. return
  86. }
  87. }
  88. func (iter *Iterator) skipFourBytes(b1, b2, b3, b4 byte) {
  89. if iter.readByte() != b1 {
  90. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  91. return
  92. }
  93. if iter.readByte() != b2 {
  94. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  95. return
  96. }
  97. if iter.readByte() != b3 {
  98. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  99. return
  100. }
  101. if iter.readByte() != b4 {
  102. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  103. return
  104. }
  105. }
  106. func (iter *Iterator) skipThreeBytes(b1, b2, b3 byte) {
  107. if iter.readByte() != b1 {
  108. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  109. return
  110. }
  111. if iter.readByte() != b2 {
  112. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  113. return
  114. }
  115. if iter.readByte() != b3 {
  116. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  117. return
  118. }
  119. }