feature_iter_skip.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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.skipFixedBytes(3) // null
  9. return true
  10. }
  11. iter.unreadByte()
  12. return false
  13. }
  14. // ReadBool reads a json object as Bool
  15. func (iter *Iterator) ReadBool() (ret bool) {
  16. c := iter.nextToken()
  17. if c == 't' {
  18. iter.skipFixedBytes(3)
  19. return true
  20. }
  21. if c == 'f' {
  22. iter.skipFixedBytes(4)
  23. return false
  24. }
  25. iter.reportError("ReadBool", "expect t or f")
  26. return
  27. }
  28. func (iter *Iterator) SkipAndReturnBytes() []byte {
  29. if iter.reader != nil {
  30. panic("reader input does not support this api")
  31. }
  32. before := iter.head
  33. iter.Skip()
  34. after := iter.head
  35. return iter.buf[before:after]
  36. }
  37. // Skip skips a json object and positions to relatively the next json object
  38. func (iter *Iterator) Skip() {
  39. c := iter.nextToken()
  40. switch c {
  41. case '"':
  42. iter.skipString()
  43. case 'n', 't':
  44. iter.skipFixedBytes(3) // null or true
  45. case 'f':
  46. iter.skipFixedBytes(4) // false
  47. case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  48. iter.skipUntilBreak()
  49. case '[':
  50. iter.skipArray()
  51. case '{':
  52. iter.skipObject()
  53. default:
  54. iter.reportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
  55. return
  56. }
  57. }
  58. func (iter *Iterator) skipString() {
  59. for {
  60. end, escaped := iter.findStringEnd()
  61. if end == -1 {
  62. if !iter.loadMore() {
  63. iter.reportError("skipString", "incomplete string")
  64. return
  65. }
  66. if escaped {
  67. iter.head = 1 // skip the first char as last char read is \
  68. }
  69. } else {
  70. iter.head = end
  71. return
  72. }
  73. }
  74. }
  75. // adapted from: https://github.com/buger/jsonparser/blob/master/parser.go
  76. // Tries to find the end of string
  77. // Support if string contains escaped quote symbols.
  78. func (iter *Iterator) findStringEnd() (int, bool) {
  79. escaped := false
  80. for i := iter.head; i < iter.tail; i++ {
  81. c := iter.buf[i]
  82. if c == '"' {
  83. if !escaped {
  84. return i + 1, false
  85. }
  86. j := i - 1
  87. for {
  88. if j < iter.head || iter.buf[j] != '\\' {
  89. // even number of backslashes
  90. // either end of buffer, or " found
  91. return i + 1, true
  92. }
  93. j--
  94. if j < iter.head || iter.buf[j] != '\\' {
  95. // odd number of backslashes
  96. // it is \" or \\\"
  97. break
  98. }
  99. j--
  100. }
  101. } else if c == '\\' {
  102. escaped = true
  103. }
  104. }
  105. j := iter.tail - 1
  106. for {
  107. if j < iter.head || iter.buf[j] != '\\' {
  108. // even number of backslashes
  109. // either end of buffer, or " found
  110. return -1, false // do not end with \
  111. }
  112. j--
  113. if j < iter.head || iter.buf[j] != '\\' {
  114. // odd number of backslashes
  115. // it is \" or \\\"
  116. break
  117. }
  118. j--
  119. }
  120. return -1, true // end with \
  121. }
  122. func (iter *Iterator) skipArray() {
  123. level := 1
  124. for {
  125. for i := iter.head; i < iter.tail; i++ {
  126. switch iter.buf[i] {
  127. case '"': // If inside string, skip it
  128. iter.head = i + 1
  129. iter.skipString()
  130. i = iter.head - 1 // it will be i++ soon
  131. case '[': // If open symbol, increase level
  132. level++
  133. case ']': // If close symbol, increase level
  134. level--
  135. // If we have returned to the original level, we're done
  136. if level == 0 {
  137. iter.head = i + 1
  138. return
  139. }
  140. }
  141. }
  142. if !iter.loadMore() {
  143. iter.reportError("skipObject", "incomplete array")
  144. return
  145. }
  146. }
  147. }
  148. func (iter *Iterator) skipObject() {
  149. level := 1
  150. for {
  151. for i := iter.head; i < iter.tail; i++ {
  152. switch iter.buf[i] {
  153. case '"': // If inside string, skip it
  154. iter.head = i + 1
  155. iter.skipString()
  156. i = iter.head - 1 // it will be i++ soon
  157. case '{': // If open symbol, increase level
  158. level++
  159. case '}': // If close symbol, increase level
  160. level--
  161. // If we have returned to the original level, we're done
  162. if level == 0 {
  163. iter.head = i + 1
  164. return
  165. }
  166. }
  167. }
  168. if !iter.loadMore() {
  169. iter.reportError("skipObject", "incomplete object")
  170. return
  171. }
  172. }
  173. }
  174. func (iter *Iterator) skipUntilBreak() {
  175. // true, false, null, number
  176. for {
  177. for i := iter.head; i < iter.tail; i++ {
  178. c := iter.buf[i]
  179. switch c {
  180. case ' ', '\n', '\r', '\t', ',', '}', ']':
  181. iter.head = i
  182. return
  183. }
  184. }
  185. if !iter.loadMore() {
  186. return
  187. }
  188. }
  189. }
  190. func (iter *Iterator) skipFixedBytes(n int) {
  191. iter.head += n
  192. if iter.head >= iter.tail {
  193. more := iter.head - iter.tail
  194. if !iter.loadMore() {
  195. if more > 0 {
  196. iter.reportError("skipFixedBytes", "unexpected end")
  197. }
  198. return
  199. }
  200. iter.head += more
  201. }
  202. }