feature_iter_skip.go 5.0 KB

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