iter_object.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // ReadObject read one field from object.
  7. // If object ended, returns empty string.
  8. // Otherwise, returns the field name.
  9. func (iter *Iterator) ReadObject() (ret string) {
  10. c := iter.nextToken()
  11. switch c {
  12. case 'n':
  13. iter.skipThreeBytes('u', 'l', 'l')
  14. return "" // null
  15. case '{':
  16. c = iter.nextToken()
  17. if c == '"' {
  18. iter.unreadByte()
  19. field := iter.ReadString()
  20. c = iter.nextToken()
  21. if c != ':' {
  22. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  23. }
  24. return field
  25. }
  26. if c == '}' {
  27. return "" // end of object
  28. }
  29. iter.ReportError("ReadObject", `expect " after {, but found `+string([]byte{c}))
  30. return
  31. case ',':
  32. field := iter.ReadString()
  33. c = iter.nextToken()
  34. if c != ':' {
  35. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  36. }
  37. return field
  38. case '}':
  39. return "" // end of object
  40. default:
  41. iter.ReportError("ReadObject", fmt.Sprintf(`expect { or , or } or n, but found %s`, string([]byte{c})))
  42. return
  43. }
  44. }
  45. // CaseInsensitive
  46. func (iter *Iterator) readFieldHash() int64 {
  47. hash := int64(0x811c9dc5)
  48. c := iter.nextToken()
  49. if c != '"' {
  50. iter.ReportError("readFieldHash", `expect ", but found `+string([]byte{c}))
  51. return 0
  52. }
  53. for {
  54. for i := iter.head; i < iter.tail; i++ {
  55. // require ascii string and no escape
  56. b := iter.buf[i]
  57. if b == '\\' {
  58. iter.head = i
  59. for _, b := range iter.readStringSlowPath() {
  60. if 'A' <= b && b <= 'Z' && !iter.cfg.caseSensitive {
  61. b += 'a' - 'A'
  62. }
  63. hash ^= int64(b)
  64. hash *= 0x1000193
  65. }
  66. c = iter.nextToken()
  67. if c != ':' {
  68. iter.ReportError("readFieldHash", `expect :, but found `+string([]byte{c}))
  69. return 0
  70. }
  71. return hash
  72. }
  73. if b == '"' {
  74. iter.head = i + 1
  75. c = iter.nextToken()
  76. if c != ':' {
  77. iter.ReportError("readFieldHash", `expect :, but found `+string([]byte{c}))
  78. return 0
  79. }
  80. return hash
  81. }
  82. if 'A' <= b && b <= 'Z' && !iter.cfg.caseSensitive {
  83. b += 'a' - 'A'
  84. }
  85. hash ^= int64(b)
  86. hash *= 0x1000193
  87. }
  88. if !iter.loadMore() {
  89. iter.ReportError("readFieldHash", `incomplete field name`)
  90. return 0
  91. }
  92. }
  93. }
  94. func calcHash(str string, caseSensitive bool) int64 {
  95. if !caseSensitive {
  96. str = strings.ToLower(str)
  97. }
  98. hash := int64(0x811c9dc5)
  99. for _, b := range []byte(str) {
  100. hash ^= int64(b)
  101. hash *= 0x1000193
  102. }
  103. return int64(hash)
  104. }
  105. // ReadObjectCB read object with callback, the key is ascii only and field name not copied
  106. func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
  107. c := iter.nextToken()
  108. var field string
  109. if c == '{' {
  110. if !iter.incrementDepth() {
  111. return false
  112. }
  113. c = iter.nextToken()
  114. if c == '"' {
  115. iter.unreadByte()
  116. field = iter.ReadString()
  117. c = iter.nextToken()
  118. if c != ':' {
  119. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  120. }
  121. if !callback(iter, field) {
  122. iter.decrementDepth()
  123. return false
  124. }
  125. c = iter.nextToken()
  126. for c == ',' {
  127. field = iter.ReadString()
  128. c = iter.nextToken()
  129. if c != ':' {
  130. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  131. }
  132. if !callback(iter, field) {
  133. iter.decrementDepth()
  134. return false
  135. }
  136. c = iter.nextToken()
  137. }
  138. if c != '}' {
  139. iter.ReportError("ReadObjectCB", `object not ended with }`)
  140. iter.decrementDepth()
  141. return false
  142. }
  143. return iter.decrementDepth()
  144. }
  145. if c == '}' {
  146. return iter.decrementDepth()
  147. }
  148. iter.ReportError("ReadObjectCB", `expect " after {, but found `+string([]byte{c}))
  149. iter.decrementDepth()
  150. return false
  151. }
  152. if c == 'n' {
  153. iter.skipThreeBytes('u', 'l', 'l')
  154. return true // null
  155. }
  156. iter.ReportError("ReadObjectCB", `expect { or n, but found `+string([]byte{c}))
  157. return false
  158. }
  159. // ReadMapCB read map with callback, the key can be any string
  160. func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
  161. c := iter.nextToken()
  162. if c == '{' {
  163. if !iter.incrementDepth() {
  164. return false
  165. }
  166. c = iter.nextToken()
  167. if c == '"' {
  168. iter.unreadByte()
  169. field := iter.ReadString()
  170. if iter.nextToken() != ':' {
  171. iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
  172. iter.decrementDepth()
  173. return false
  174. }
  175. if !callback(iter, field) {
  176. iter.decrementDepth()
  177. return false
  178. }
  179. c = iter.nextToken()
  180. for c == ',' {
  181. field = iter.ReadString()
  182. if iter.nextToken() != ':' {
  183. iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
  184. iter.decrementDepth()
  185. return false
  186. }
  187. if !callback(iter, field) {
  188. iter.decrementDepth()
  189. return false
  190. }
  191. c = iter.nextToken()
  192. }
  193. if c != '}' {
  194. iter.ReportError("ReadMapCB", `object not ended with }`)
  195. iter.decrementDepth()
  196. return false
  197. }
  198. return iter.decrementDepth()
  199. }
  200. if c == '}' {
  201. return iter.decrementDepth()
  202. }
  203. iter.ReportError("ReadMapCB", `expect " after {, but found `+string([]byte{c}))
  204. iter.decrementDepth()
  205. return false
  206. }
  207. if c == 'n' {
  208. iter.skipThreeBytes('u', 'l', 'l')
  209. return true // null
  210. }
  211. iter.ReportError("ReadMapCB", `expect { or n, but found `+string([]byte{c}))
  212. return false
  213. }
  214. func (iter *Iterator) readObjectStart() bool {
  215. c := iter.nextToken()
  216. if c == '{' {
  217. c = iter.nextToken()
  218. if c == '}' {
  219. return false
  220. }
  221. iter.unreadByte()
  222. return true
  223. } else if c == 'n' {
  224. iter.skipThreeBytes('u', 'l', 'l')
  225. return false
  226. }
  227. iter.ReportError("readObjectStart", "expect { or n, but found "+string([]byte{c}))
  228. return false
  229. }
  230. func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
  231. str := iter.ReadStringAsSlice()
  232. if iter.skipWhitespacesWithoutLoadMore() {
  233. if ret == nil {
  234. ret = make([]byte, len(str))
  235. copy(ret, str)
  236. }
  237. if !iter.loadMore() {
  238. return
  239. }
  240. }
  241. if iter.buf[iter.head] != ':' {
  242. iter.ReportError("readObjectFieldAsBytes", "expect : after object field, but found "+string([]byte{iter.buf[iter.head]}))
  243. return
  244. }
  245. iter.head++
  246. if iter.skipWhitespacesWithoutLoadMore() {
  247. if ret == nil {
  248. ret = make([]byte, len(str))
  249. copy(ret, str)
  250. }
  251. if !iter.loadMore() {
  252. return
  253. }
  254. }
  255. if ret == nil {
  256. return str
  257. }
  258. return ret
  259. }