feature_iter_skip.go 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  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 Bool
  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")
  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. return remaining
  56. }
  57. captured = append(captured, remaining...)
  58. return captured
  59. }
  60. // Skip skips a json object and positions to relatively the next json object
  61. func (iter *Iterator) Skip() {
  62. c := iter.nextToken()
  63. switch c {
  64. case '"':
  65. iter.skipString()
  66. case 'n':
  67. iter.skipThreeBytes('u', 'l', 'l') // null
  68. case 't':
  69. iter.skipThreeBytes('r', 'u', 'e') // true
  70. case 'f':
  71. iter.skipFourBytes('a', 'l', 's', 'e') // false
  72. case '-', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9':
  73. iter.skipNumber()
  74. case '[':
  75. iter.skipArray()
  76. case '{':
  77. iter.skipObject()
  78. default:
  79. iter.ReportError("Skip", fmt.Sprintf("do not know how to skip: %v", c))
  80. return
  81. }
  82. }
  83. func (iter *Iterator) skipString() {
  84. for {
  85. end, escaped := iter.findStringEnd()
  86. if end == -1 {
  87. if !iter.loadMore() {
  88. iter.ReportError("skipString", "incomplete string")
  89. return
  90. }
  91. if escaped {
  92. iter.head = 1 // skip the first char as last char read is \
  93. }
  94. } else {
  95. iter.head = end
  96. return
  97. }
  98. }
  99. }
  100. // adapted from: https://github.com/buger/jsonparser/blob/master/parser.go
  101. // Tries to find the end of string
  102. // Support if string contains escaped quote symbols.
  103. func (iter *Iterator) findStringEnd() (int, bool) {
  104. escaped := false
  105. for i := iter.head; i < iter.tail; i++ {
  106. c := iter.buf[i]
  107. if c == '"' {
  108. if !escaped {
  109. return i + 1, false
  110. }
  111. j := i - 1
  112. for {
  113. if j < iter.head || iter.buf[j] != '\\' {
  114. // even number of backslashes
  115. // either end of buffer, or " found
  116. return i + 1, true
  117. }
  118. j--
  119. if j < iter.head || iter.buf[j] != '\\' {
  120. // odd number of backslashes
  121. // it is \" or \\\"
  122. break
  123. }
  124. j--
  125. }
  126. } else if c == '\\' {
  127. escaped = true
  128. }
  129. }
  130. j := iter.tail - 1
  131. for {
  132. if j < iter.head || iter.buf[j] != '\\' {
  133. // even number of backslashes
  134. // either end of buffer, or " found
  135. return -1, false // do not end with \
  136. }
  137. j--
  138. if j < iter.head || iter.buf[j] != '\\' {
  139. // odd number of backslashes
  140. // it is \" or \\\"
  141. break
  142. }
  143. j--
  144. }
  145. return -1, true // end with \
  146. }
  147. func (iter *Iterator) skipArray() {
  148. level := 1
  149. for {
  150. for i := iter.head; i < iter.tail; i++ {
  151. switch iter.buf[i] {
  152. case '"': // If inside string, skip it
  153. iter.head = i + 1
  154. iter.skipString()
  155. i = iter.head - 1 // it will be i++ soon
  156. case '[': // If open symbol, increase level
  157. level++
  158. case ']': // If close symbol, increase level
  159. level--
  160. // If we have returned to the original level, we're done
  161. if level == 0 {
  162. iter.head = i + 1
  163. return
  164. }
  165. }
  166. }
  167. if !iter.loadMore() {
  168. iter.ReportError("skipObject", "incomplete array")
  169. return
  170. }
  171. }
  172. }
  173. func (iter *Iterator) skipObject() {
  174. level := 1
  175. for {
  176. for i := iter.head; i < iter.tail; i++ {
  177. switch iter.buf[i] {
  178. case '"': // If inside string, skip it
  179. iter.head = i + 1
  180. iter.skipString()
  181. i = iter.head - 1 // it will be i++ soon
  182. case '{': // If open symbol, increase level
  183. level++
  184. case '}': // If close symbol, increase level
  185. level--
  186. // If we have returned to the original level, we're done
  187. if level == 0 {
  188. iter.head = i + 1
  189. return
  190. }
  191. }
  192. }
  193. if !iter.loadMore() {
  194. iter.ReportError("skipObject", "incomplete object")
  195. return
  196. }
  197. }
  198. }
  199. func (iter *Iterator) skipNumber() {
  200. for {
  201. for i := iter.head; i < iter.tail; i++ {
  202. c := iter.buf[i]
  203. switch c {
  204. case ' ', '\n', '\r', '\t', ',', '}', ']':
  205. iter.head = i
  206. return
  207. }
  208. }
  209. if !iter.loadMore() {
  210. return
  211. }
  212. }
  213. }
  214. func (iter *Iterator) skipFourBytes(b1, b2, b3, b4 byte) {
  215. if iter.readByte() != b1 {
  216. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  217. return
  218. }
  219. if iter.readByte() != b2 {
  220. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  221. return
  222. }
  223. if iter.readByte() != b3 {
  224. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  225. return
  226. }
  227. if iter.readByte() != b4 {
  228. iter.ReportError("skipFourBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3, b4})))
  229. return
  230. }
  231. }
  232. func (iter *Iterator) skipThreeBytes(b1, b2, b3 byte) {
  233. if iter.readByte() != b1 {
  234. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  235. return
  236. }
  237. if iter.readByte() != b2 {
  238. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  239. return
  240. }
  241. if iter.readByte() != b3 {
  242. iter.ReportError("skipThreeBytes", fmt.Sprintf("expect %s", string([]byte{b1, b2, b3})))
  243. return
  244. }
  245. }