另客网go项目公用的代码库

iter.go 7.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. package jsoniter
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "io"
  6. )
  7. // ValueType the type for JSON element
  8. type ValueType int
  9. const (
  10. // InvalidValue invalid JSON element
  11. InvalidValue ValueType = iota
  12. // StringValue JSON element "string"
  13. StringValue
  14. // NumberValue JSON element 100 or 0.10
  15. NumberValue
  16. // NilValue JSON element null
  17. NilValue
  18. // BoolValue JSON element true or false
  19. BoolValue
  20. // ArrayValue JSON element []
  21. ArrayValue
  22. // ObjectValue JSON element {}
  23. ObjectValue
  24. )
  25. var hexDigits []byte
  26. var valueTypes []ValueType
  27. func init() {
  28. hexDigits = make([]byte, 256)
  29. for i := 0; i < len(hexDigits); i++ {
  30. hexDigits[i] = 255
  31. }
  32. for i := '0'; i <= '9'; i++ {
  33. hexDigits[i] = byte(i - '0')
  34. }
  35. for i := 'a'; i <= 'f'; i++ {
  36. hexDigits[i] = byte((i - 'a') + 10)
  37. }
  38. for i := 'A'; i <= 'F'; i++ {
  39. hexDigits[i] = byte((i - 'A') + 10)
  40. }
  41. valueTypes = make([]ValueType, 256)
  42. for i := 0; i < len(valueTypes); i++ {
  43. valueTypes[i] = InvalidValue
  44. }
  45. valueTypes['"'] = StringValue
  46. valueTypes['-'] = NumberValue
  47. valueTypes['0'] = NumberValue
  48. valueTypes['1'] = NumberValue
  49. valueTypes['2'] = NumberValue
  50. valueTypes['3'] = NumberValue
  51. valueTypes['4'] = NumberValue
  52. valueTypes['5'] = NumberValue
  53. valueTypes['6'] = NumberValue
  54. valueTypes['7'] = NumberValue
  55. valueTypes['8'] = NumberValue
  56. valueTypes['9'] = NumberValue
  57. valueTypes['t'] = BoolValue
  58. valueTypes['f'] = BoolValue
  59. valueTypes['n'] = NilValue
  60. valueTypes['['] = ArrayValue
  61. valueTypes['{'] = ObjectValue
  62. }
  63. // Iterator is a io.Reader like object, with JSON specific read functions.
  64. // Error is not returned as return value, but stored as Error member on this iterator instance.
  65. type Iterator struct {
  66. cfg *frozenConfig
  67. reader io.Reader
  68. buf []byte
  69. head int
  70. tail int
  71. captureStartedAt int
  72. captured []byte
  73. Error error
  74. Attachment interface{} // open for customized decoder
  75. }
  76. // NewIterator creates an empty Iterator instance
  77. func NewIterator(cfg API) *Iterator {
  78. return &Iterator{
  79. cfg: cfg.(*frozenConfig),
  80. reader: nil,
  81. buf: nil,
  82. head: 0,
  83. tail: 0,
  84. }
  85. }
  86. // Parse creates an Iterator instance from io.Reader
  87. func Parse(cfg API, reader io.Reader, bufSize int) *Iterator {
  88. return &Iterator{
  89. cfg: cfg.(*frozenConfig),
  90. reader: reader,
  91. buf: make([]byte, bufSize),
  92. head: 0,
  93. tail: 0,
  94. }
  95. }
  96. // ParseBytes creates an Iterator instance from byte array
  97. func ParseBytes(cfg API, input []byte) *Iterator {
  98. return &Iterator{
  99. cfg: cfg.(*frozenConfig),
  100. reader: nil,
  101. buf: input,
  102. head: 0,
  103. tail: len(input),
  104. }
  105. }
  106. // ParseString creates an Iterator instance from string
  107. func ParseString(cfg API, input string) *Iterator {
  108. return ParseBytes(cfg, []byte(input))
  109. }
  110. // Pool returns a pool can provide more iterator with same configuration
  111. func (iter *Iterator) Pool() IteratorPool {
  112. return iter.cfg
  113. }
  114. // Reset reuse iterator instance by specifying another reader
  115. func (iter *Iterator) Reset(reader io.Reader) *Iterator {
  116. iter.reader = reader
  117. iter.head = 0
  118. iter.tail = 0
  119. return iter
  120. }
  121. // ResetBytes reuse iterator instance by specifying another byte array as input
  122. func (iter *Iterator) ResetBytes(input []byte) *Iterator {
  123. iter.reader = nil
  124. iter.buf = input
  125. iter.head = 0
  126. iter.tail = len(input)
  127. return iter
  128. }
  129. // WhatIsNext gets ValueType of relatively next json element
  130. func (iter *Iterator) WhatIsNext() ValueType {
  131. valueType := valueTypes[iter.nextToken()]
  132. iter.unreadByte()
  133. return valueType
  134. }
  135. func (iter *Iterator) skipWhitespacesWithoutLoadMore() bool {
  136. for i := iter.head; i < iter.tail; i++ {
  137. c := iter.buf[i]
  138. switch c {
  139. case ' ', '\n', '\t', '\r':
  140. continue
  141. }
  142. iter.head = i
  143. return false
  144. }
  145. return true
  146. }
  147. func (iter *Iterator) isObjectEnd() bool {
  148. c := iter.nextToken()
  149. if c == ',' {
  150. return false
  151. }
  152. if c == '}' {
  153. return true
  154. }
  155. iter.ReportError("isObjectEnd", "object ended prematurely, unexpected char "+string([]byte{c}))
  156. return true
  157. }
  158. func (iter *Iterator) nextToken() byte {
  159. // a variation of skip whitespaces, returning the next non-whitespace token
  160. for {
  161. for i := iter.head; i < iter.tail; i++ {
  162. c := iter.buf[i]
  163. switch c {
  164. case ' ', '\n', '\t', '\r':
  165. continue
  166. }
  167. iter.head = i + 1
  168. return c
  169. }
  170. if !iter.loadMore() {
  171. return 0
  172. }
  173. }
  174. }
  175. // ReportError record a error in iterator instance with current position.
  176. func (iter *Iterator) ReportError(operation string, msg string) {
  177. if iter.Error != nil {
  178. if iter.Error != io.EOF {
  179. return
  180. }
  181. }
  182. peekStart := iter.head - 10
  183. if peekStart < 0 {
  184. peekStart = 0
  185. }
  186. peekEnd := iter.head + 10
  187. if peekEnd > iter.tail {
  188. peekEnd = iter.tail
  189. }
  190. parsing := string(iter.buf[peekStart:peekEnd])
  191. contextStart := iter.head - 50
  192. if contextStart < 0 {
  193. contextStart = 0
  194. }
  195. contextEnd := iter.head + 50
  196. if contextEnd > iter.tail {
  197. contextEnd = iter.tail
  198. }
  199. context := string(iter.buf[contextStart:contextEnd])
  200. iter.Error = fmt.Errorf("%s: %s, error found in #%v byte of ...|%s|..., bigger context ...|%s|...",
  201. operation, msg, iter.head-peekStart, parsing, context)
  202. }
  203. // CurrentBuffer gets current buffer as string for debugging purpose
  204. func (iter *Iterator) CurrentBuffer() string {
  205. peekStart := iter.head - 10
  206. if peekStart < 0 {
  207. peekStart = 0
  208. }
  209. return fmt.Sprintf("parsing #%v byte, around ...|%s|..., whole buffer ...|%s|...", iter.head,
  210. string(iter.buf[peekStart:iter.head]), string(iter.buf[0:iter.tail]))
  211. }
  212. func (iter *Iterator) readByte() (ret byte) {
  213. if iter.head == iter.tail {
  214. if iter.loadMore() {
  215. ret = iter.buf[iter.head]
  216. iter.head++
  217. return ret
  218. }
  219. return 0
  220. }
  221. ret = iter.buf[iter.head]
  222. iter.head++
  223. return ret
  224. }
  225. func (iter *Iterator) loadMore() bool {
  226. if iter.reader == nil {
  227. if iter.Error == nil {
  228. iter.head = iter.tail
  229. iter.Error = io.EOF
  230. }
  231. return false
  232. }
  233. if iter.captured != nil {
  234. iter.captured = append(iter.captured,
  235. iter.buf[iter.captureStartedAt:iter.tail]...)
  236. iter.captureStartedAt = 0
  237. }
  238. for {
  239. n, err := iter.reader.Read(iter.buf)
  240. if n == 0 {
  241. if err != nil {
  242. if iter.Error == nil {
  243. iter.Error = err
  244. }
  245. return false
  246. }
  247. } else {
  248. iter.head = 0
  249. iter.tail = n
  250. return true
  251. }
  252. }
  253. }
  254. func (iter *Iterator) unreadByte() {
  255. if iter.Error != nil {
  256. return
  257. }
  258. iter.head--
  259. return
  260. }
  261. // Read read the next JSON element as generic interface{}.
  262. func (iter *Iterator) Read() interface{} {
  263. valueType := iter.WhatIsNext()
  264. switch valueType {
  265. case StringValue:
  266. return iter.ReadString()
  267. case NumberValue:
  268. if iter.cfg.configBeforeFrozen.UseNumber {
  269. return json.Number(iter.readNumberAsString())
  270. }
  271. return iter.ReadFloat64()
  272. case NilValue:
  273. iter.skipFourBytes('n', 'u', 'l', 'l')
  274. return nil
  275. case BoolValue:
  276. return iter.ReadBool()
  277. case ArrayValue:
  278. arr := []interface{}{}
  279. iter.ReadArrayCB(func(iter *Iterator) bool {
  280. var elem interface{}
  281. iter.ReadVal(&elem)
  282. arr = append(arr, elem)
  283. return true
  284. })
  285. return arr
  286. case ObjectValue:
  287. obj := map[string]interface{}{}
  288. iter.ReadMapCB(func(Iter *Iterator, field string) bool {
  289. var elem interface{}
  290. iter.ReadVal(&elem)
  291. obj[field] = elem
  292. return true
  293. })
  294. return obj
  295. default:
  296. iter.ReportError("Read", fmt.Sprintf("unexpected value type: %v", valueType))
  297. return nil
  298. }
  299. }