feature_any.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. package jsoniter
  2. import "fmt"
  3. type Any interface {
  4. LastError() error
  5. ValueType() ValueType
  6. ToBool() bool
  7. ToInt() int
  8. ToInt32() int32
  9. ToInt64() int64
  10. ToFloat32() float32
  11. ToFloat64() float64
  12. ToString() string
  13. Get(path ...interface{}) Any
  14. Size() int
  15. Keys() []string
  16. IterateObject() (func() (string, Any, bool), bool)
  17. IterateArray() (func() (Any, bool), bool)
  18. GetArray() []Any
  19. SetArray(newList []Any) bool
  20. GetObject() map[string]Any
  21. SetObject(map[string]Any) bool
  22. GetInterface() interface{}
  23. WriteTo(stream *Stream)
  24. Parse() *Iterator
  25. }
  26. type baseAny struct{}
  27. func (any *baseAny) Get(path ...interface{}) Any {
  28. return &invalidAny{baseAny{}, fmt.Errorf("Get %v from simple value", path)}
  29. }
  30. func (any *baseAny) Size() int {
  31. return 0
  32. }
  33. func (any *baseAny) Keys() []string {
  34. return []string{}
  35. }
  36. func (any *baseAny) IterateObject() (func() (string, Any, bool), bool) {
  37. return nil, false
  38. }
  39. func (any *baseAny) IterateArray() (func() (Any, bool), bool) {
  40. return nil, false
  41. }
  42. func (any *baseAny) GetArray() []Any {
  43. return []Any{}
  44. }
  45. func (any *baseAny) SetArray(newList []Any) bool {
  46. return false
  47. }
  48. func (any *baseAny) GetObject() map[string]Any {
  49. return map[string]Any{}
  50. }
  51. func (any *baseAny) SetObject(map[string]Any) bool {
  52. return false
  53. }
  54. func WrapInt64(val int64) Any {
  55. return &intAny{baseAny{}, nil, val}
  56. }
  57. func (iter *Iterator) ReadAny() Any {
  58. return iter.readAny(nil)
  59. }
  60. func (iter *Iterator) readAny(reusableIter *Iterator) Any {
  61. c := iter.nextToken()
  62. switch c {
  63. case '"':
  64. return iter.readStringAny(reusableIter)
  65. case 'n':
  66. iter.skipFixedBytes(3) // null
  67. return &nilAny{}
  68. case 't':
  69. iter.skipFixedBytes(3) // true
  70. return &trueAny{}
  71. case 'f':
  72. iter.skipFixedBytes(4) // false
  73. return &falseAny{}
  74. case '{':
  75. return iter.readObjectAny(reusableIter)
  76. case '[':
  77. return iter.readArrayAny(reusableIter)
  78. default:
  79. iter.unreadByte()
  80. return iter.readNumberAny(reusableIter)
  81. }
  82. }
  83. func (iter *Iterator) readNumberAny(reusableIter *Iterator) Any {
  84. dotFound := false
  85. var lazyBuf []byte
  86. for {
  87. for i := iter.head; i < iter.tail; i++ {
  88. c := iter.buf[i]
  89. if c == '.' {
  90. dotFound = true
  91. continue
  92. }
  93. switch c {
  94. case ' ', '\n', '\r', '\t', ',', '}', ']':
  95. lazyBuf = append(lazyBuf, iter.buf[iter.head:i]...)
  96. iter.head = i
  97. if dotFound {
  98. return &floatLazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  99. } else {
  100. return &intLazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  101. }
  102. }
  103. }
  104. lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
  105. if !iter.loadMore() {
  106. iter.head = iter.tail
  107. if dotFound {
  108. return &floatLazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  109. } else {
  110. return &intLazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  111. }
  112. }
  113. }
  114. }
  115. func (iter *Iterator) readStringAny(reusableIter *Iterator) Any {
  116. lazyBuf := make([]byte, 1, 8)
  117. lazyBuf[0] = '"'
  118. for {
  119. end, escaped := iter.findStringEnd()
  120. if end == -1 {
  121. lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
  122. if !iter.loadMore() {
  123. iter.reportError("readStringAny", "incomplete string")
  124. return &invalidAny{}
  125. }
  126. if escaped {
  127. iter.head = 1 // skip the first char as last char read is \
  128. }
  129. } else {
  130. lazyBuf = append(lazyBuf, iter.buf[iter.head:end]...)
  131. iter.head = end
  132. return &stringLazyAny{baseAny{}, lazyBuf, reusableIter, nil, ""}
  133. }
  134. }
  135. }
  136. func (iter *Iterator) readObjectAny(reusableIter *Iterator) Any {
  137. level := 1
  138. lazyBuf := make([]byte, 1, 32)
  139. lazyBuf[0] = '{'
  140. for {
  141. start := iter.head
  142. for i := iter.head; i < iter.tail; i++ {
  143. switch iter.buf[i] {
  144. case '"': // If inside string, skip it
  145. iter.head = i + 1
  146. iter.skipString()
  147. i = iter.head - 1 // it will be i++ soon
  148. case '{': // If open symbol, increase level
  149. level++
  150. case '}': // If close symbol, increase level
  151. level--
  152. // If we have returned to the original level, we're done
  153. if level == 0 {
  154. iter.head = i + 1
  155. lazyBuf = append(lazyBuf, iter.buf[start:iter.head]...)
  156. return &objectLazyAny{baseAny{}, lazyBuf, reusableIter, nil, nil, lazyBuf}
  157. }
  158. }
  159. }
  160. lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
  161. if !iter.loadMore() {
  162. iter.reportError("skipObject", "incomplete object")
  163. return &invalidAny{}
  164. }
  165. }
  166. }
  167. func (iter *Iterator) readArrayAny(reusableIter *Iterator) Any {
  168. level := 1
  169. lazyBuf := make([]byte, 1, 32)
  170. lazyBuf[0] = '['
  171. for {
  172. start := iter.head
  173. for i := iter.head; i < iter.tail; i++ {
  174. switch iter.buf[i] {
  175. case '"': // If inside string, skip it
  176. iter.head = i + 1
  177. iter.skipString()
  178. i = iter.head - 1 // it will be i++ soon
  179. case '[': // If open symbol, increase level
  180. level++
  181. case ']': // If close symbol, increase level
  182. level--
  183. // If we have returned to the original level, we're done
  184. if level == 0 {
  185. iter.head = i + 1
  186. lazyBuf = append(lazyBuf, iter.buf[start:iter.head]...)
  187. return &arrayLazyAny{baseAny{}, lazyBuf, reusableIter, nil, nil, lazyBuf}
  188. }
  189. }
  190. }
  191. lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
  192. if !iter.loadMore() {
  193. iter.reportError("skipArray", "incomplete array")
  194. return &invalidAny{}
  195. }
  196. }
  197. }