iter_object.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "unicode"
  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' {
  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' {
  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) int64 {
  95. hash := int64(0x811c9dc5)
  96. for _, b := range str {
  97. hash ^= int64(unicode.ToLower(b))
  98. hash *= 0x1000193
  99. }
  100. return int64(hash)
  101. }
  102. // ReadObjectCB read object with callback, the key is ascii only and field name not copied
  103. func (iter *Iterator) ReadObjectCB(callback func(*Iterator, string) bool) bool {
  104. c := iter.nextToken()
  105. var field string
  106. if c == '{' {
  107. c = iter.nextToken()
  108. if c == '"' {
  109. iter.unreadByte()
  110. field = iter.ReadString()
  111. c = iter.nextToken()
  112. if c != ':' {
  113. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  114. }
  115. if !callback(iter, field) {
  116. return false
  117. }
  118. c = iter.nextToken()
  119. for c == ',' {
  120. field = iter.ReadString()
  121. c = iter.nextToken()
  122. if c != ':' {
  123. iter.ReportError("ReadObject", "expect : after object field, but found "+string([]byte{c}))
  124. }
  125. if !callback(iter, field) {
  126. return false
  127. }
  128. c = iter.nextToken()
  129. }
  130. if c != '}' {
  131. iter.ReportError("ReadObjectCB", `object not ended with }`)
  132. return false
  133. }
  134. return true
  135. }
  136. if c == '}' {
  137. return true
  138. }
  139. iter.ReportError("ReadObjectCB", `expect " after }, but found `+string([]byte{c}))
  140. return false
  141. }
  142. if c == 'n' {
  143. iter.skipThreeBytes('u', 'l', 'l')
  144. return true // null
  145. }
  146. iter.ReportError("ReadObjectCB", `expect { or n, but found `+string([]byte{c}))
  147. return false
  148. }
  149. // ReadMapCB read map with callback, the key can be any string
  150. func (iter *Iterator) ReadMapCB(callback func(*Iterator, string) bool) bool {
  151. c := iter.nextToken()
  152. if c == '{' {
  153. c = iter.nextToken()
  154. if c == '"' {
  155. iter.unreadByte()
  156. field := iter.ReadString()
  157. if iter.nextToken() != ':' {
  158. iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
  159. return false
  160. }
  161. if !callback(iter, field) {
  162. return false
  163. }
  164. c = iter.nextToken()
  165. for c == ',' {
  166. field = iter.ReadString()
  167. if iter.nextToken() != ':' {
  168. iter.ReportError("ReadMapCB", "expect : after object field, but found "+string([]byte{c}))
  169. return false
  170. }
  171. if !callback(iter, field) {
  172. return false
  173. }
  174. c = iter.nextToken()
  175. }
  176. if c != '}' {
  177. iter.ReportError("ReadMapCB", `object not ended with }`)
  178. return false
  179. }
  180. return true
  181. }
  182. if c == '}' {
  183. return true
  184. }
  185. iter.ReportError("ReadMapCB", `expect " after }, but found `+string([]byte{c}))
  186. return false
  187. }
  188. if c == 'n' {
  189. iter.skipThreeBytes('u', 'l', 'l')
  190. return true // null
  191. }
  192. iter.ReportError("ReadMapCB", `expect { or n, but found `+string([]byte{c}))
  193. return false
  194. }
  195. func (iter *Iterator) readObjectStart() bool {
  196. c := iter.nextToken()
  197. if c == '{' {
  198. c = iter.nextToken()
  199. if c == '}' {
  200. return false
  201. }
  202. iter.unreadByte()
  203. return true
  204. } else if c == 'n' {
  205. iter.skipThreeBytes('u', 'l', 'l')
  206. return false
  207. }
  208. iter.ReportError("readObjectStart", "expect { or n, but found "+string([]byte{c}))
  209. return false
  210. }
  211. func (iter *Iterator) readObjectFieldAsBytes() (ret []byte) {
  212. str := iter.ReadStringAsSlice()
  213. if iter.skipWhitespacesWithoutLoadMore() {
  214. if ret == nil {
  215. ret = make([]byte, len(str))
  216. copy(ret, str)
  217. }
  218. if !iter.loadMore() {
  219. return
  220. }
  221. }
  222. if iter.buf[iter.head] != ':' {
  223. iter.ReportError("readObjectFieldAsBytes", "expect : after object field, but found "+string([]byte{iter.buf[iter.head]}))
  224. return
  225. }
  226. iter.head++
  227. if iter.skipWhitespacesWithoutLoadMore() {
  228. if ret == nil {
  229. ret = make([]byte, len(str))
  230. copy(ret, str)
  231. }
  232. if !iter.loadMore() {
  233. return
  234. }
  235. }
  236. if ret == nil {
  237. return str
  238. }
  239. return ret
  240. }