feature_any.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "reflect"
  5. )
  6. type Any interface {
  7. LastError() error
  8. ValueType() ValueType
  9. ToBool() bool
  10. ToInt() int
  11. ToInt32() int32
  12. ToInt64() int64
  13. ToUint() uint
  14. ToUint32() uint32
  15. ToUint64() uint64
  16. ToFloat32() float32
  17. ToFloat64() float64
  18. ToString() string
  19. Get(path ...interface{}) Any
  20. Size() int
  21. Keys() []string
  22. IterateObject() (func() (string, Any, bool), bool)
  23. IterateArray() (func() (Any, bool), bool)
  24. GetArray() []Any
  25. SetArray(newList []Any) bool
  26. GetObject() map[string]Any
  27. SetObject(map[string]Any) bool
  28. GetInterface() interface{}
  29. WriteTo(stream *Stream)
  30. Parse() *Iterator
  31. }
  32. type baseAny struct{}
  33. func (any *baseAny) Get(path ...interface{}) Any {
  34. return &invalidAny{baseAny{}, fmt.Errorf("Get %v from simple value", path)}
  35. }
  36. func (any *baseAny) Size() int {
  37. return 0
  38. }
  39. func (any *baseAny) Keys() []string {
  40. return []string{}
  41. }
  42. func (any *baseAny) IterateObject() (func() (string, Any, bool), bool) {
  43. return nil, false
  44. }
  45. func (any *baseAny) IterateArray() (func() (Any, bool), bool) {
  46. return nil, false
  47. }
  48. func (any *baseAny) GetArray() []Any {
  49. return []Any{}
  50. }
  51. func (any *baseAny) SetArray(newList []Any) bool {
  52. return false
  53. }
  54. func (any *baseAny) GetObject() map[string]Any {
  55. return map[string]Any{}
  56. }
  57. func (any *baseAny) SetObject(map[string]Any) bool {
  58. return false
  59. }
  60. func WrapInt32(val int32) Any {
  61. return &int32Any{baseAny{}, val}
  62. }
  63. func WrapInt64(val int64) Any {
  64. return &int64Any{baseAny{}, val}
  65. }
  66. func WrapUint32(val uint32) Any {
  67. return &uint32Any{baseAny{}, val}
  68. }
  69. func WrapUint64(val uint64) Any {
  70. return &uint64Any{baseAny{}, val}
  71. }
  72. func WrapFloat64(val float64) Any {
  73. return &floatAny{baseAny{}, val}
  74. }
  75. func WrapString(val string) Any {
  76. return &stringAny{baseAny{}, nil, val}
  77. }
  78. func Wrap(val interface{}) Any {
  79. if val == nil {
  80. return &nilAny{}
  81. }
  82. asAny, isAny := val.(Any)
  83. if isAny {
  84. return asAny
  85. }
  86. type_ := reflect.TypeOf(val)
  87. switch type_.Kind() {
  88. case reflect.Slice:
  89. return wrapArray(val)
  90. case reflect.Struct:
  91. return wrapStruct(val)
  92. case reflect.Map:
  93. return wrapMap(val)
  94. case reflect.String:
  95. return WrapString(val.(string))
  96. case reflect.Int:
  97. return WrapInt64(int64(val.(int)))
  98. case reflect.Int8:
  99. return WrapInt32(int32(val.(int8)))
  100. case reflect.Int16:
  101. return WrapInt32(int32(val.(int16)))
  102. case reflect.Int32:
  103. return WrapInt32(val.(int32))
  104. case reflect.Int64:
  105. return WrapInt64(val.(int64))
  106. case reflect.Uint:
  107. return WrapUint64(uint64(val.(uint)))
  108. case reflect.Uint8:
  109. return WrapUint32(uint32(val.(uint8)))
  110. case reflect.Uint16:
  111. return WrapUint32(uint32(val.(uint16)))
  112. case reflect.Uint32:
  113. return WrapUint32(uint32(val.(uint32)))
  114. case reflect.Uint64:
  115. return WrapUint64(val.(uint64))
  116. case reflect.Float32:
  117. return WrapFloat64(float64(val.(float32)))
  118. case reflect.Float64:
  119. return WrapFloat64(val.(float64))
  120. case reflect.Bool:
  121. if val.(bool) == true {
  122. return &trueAny{}
  123. } else {
  124. return &falseAny{}
  125. }
  126. }
  127. return &invalidAny{baseAny{}, fmt.Errorf("unsupported type: %v", type_)}
  128. }
  129. func (iter *Iterator) ReadAny() Any {
  130. return iter.readAny(nil)
  131. }
  132. func (iter *Iterator) readAny(reusableIter *Iterator) Any {
  133. c := iter.nextToken()
  134. switch c {
  135. case '"':
  136. return iter.readStringAny(reusableIter)
  137. case 'n':
  138. iter.skipFixedBytes(3) // null
  139. return &nilAny{}
  140. case 't':
  141. iter.skipFixedBytes(3) // true
  142. return &trueAny{}
  143. case 'f':
  144. iter.skipFixedBytes(4) // false
  145. return &falseAny{}
  146. case '{':
  147. return iter.readObjectAny(reusableIter)
  148. case '[':
  149. return iter.readArrayAny(reusableIter)
  150. default:
  151. return iter.readNumberAny(reusableIter, c)
  152. }
  153. }
  154. func (iter *Iterator) readNumberAny(reusableIter *Iterator, firstByte byte) Any {
  155. dotFound := false
  156. lazyBuf := make([]byte, 1, 8)
  157. lazyBuf[0] = firstByte
  158. for {
  159. for i := iter.head; i < iter.tail; i++ {
  160. c := iter.buf[i]
  161. if c == '.' {
  162. dotFound = true
  163. continue
  164. }
  165. switch c {
  166. case ' ', '\n', '\r', '\t', ',', '}', ']':
  167. lazyBuf = append(lazyBuf, iter.buf[iter.head:i]...)
  168. iter.head = i
  169. if dotFound {
  170. return &float64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  171. } else {
  172. if firstByte == '-' {
  173. return &int64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  174. } else {
  175. return &uint64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  176. }
  177. }
  178. }
  179. }
  180. lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
  181. if !iter.loadMore() {
  182. iter.head = iter.tail
  183. if dotFound {
  184. return &float64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  185. } else {
  186. if firstByte == '-' {
  187. return &int64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  188. } else {
  189. return &uint64LazyAny{baseAny{}, lazyBuf, reusableIter, nil, 0}
  190. }
  191. }
  192. }
  193. }
  194. }
  195. func (iter *Iterator) readStringAny(reusableIter *Iterator) Any {
  196. lazyBuf := make([]byte, 1, 8)
  197. lazyBuf[0] = '"'
  198. for {
  199. end, escaped := iter.findStringEnd()
  200. if end == -1 {
  201. lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
  202. if !iter.loadMore() {
  203. iter.reportError("readStringAny", "incomplete string")
  204. return &invalidAny{}
  205. }
  206. if escaped {
  207. iter.head = 1 // skip the first char as last char read is \
  208. }
  209. } else {
  210. lazyBuf = append(lazyBuf, iter.buf[iter.head:end]...)
  211. iter.head = end
  212. return &stringLazyAny{baseAny{}, lazyBuf, reusableIter, nil, ""}
  213. }
  214. }
  215. }
  216. func (iter *Iterator) readObjectAny(reusableIter *Iterator) Any {
  217. level := 1
  218. lazyBuf := make([]byte, 1, 32)
  219. lazyBuf[0] = '{'
  220. for {
  221. start := iter.head
  222. for i := iter.head; i < iter.tail; i++ {
  223. switch iter.buf[i] {
  224. case '"': // If inside string, skip it
  225. iter.head = i + 1
  226. iter.skipString()
  227. i = iter.head - 1 // it will be i++ soon
  228. case '{': // If open symbol, increase level
  229. level++
  230. case '}': // If close symbol, increase level
  231. level--
  232. // If we have returned to the original level, we're done
  233. if level == 0 {
  234. iter.head = i + 1
  235. lazyBuf = append(lazyBuf, iter.buf[start:iter.head]...)
  236. return &objectLazyAny{baseAny{}, lazyBuf, reusableIter, nil, nil, lazyBuf}
  237. }
  238. }
  239. }
  240. lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
  241. if !iter.loadMore() {
  242. iter.reportError("skipObject", "incomplete object")
  243. return &invalidAny{}
  244. }
  245. }
  246. }
  247. func (iter *Iterator) readArrayAny(reusableIter *Iterator) Any {
  248. level := 1
  249. lazyBuf := make([]byte, 1, 32)
  250. lazyBuf[0] = '['
  251. for {
  252. start := iter.head
  253. for i := iter.head; i < iter.tail; i++ {
  254. switch iter.buf[i] {
  255. case '"': // If inside string, skip it
  256. iter.head = i + 1
  257. iter.skipString()
  258. i = iter.head - 1 // it will be i++ soon
  259. case '[': // If open symbol, increase level
  260. level++
  261. case ']': // If close symbol, increase level
  262. level--
  263. // If we have returned to the original level, we're done
  264. if level == 0 {
  265. iter.head = i + 1
  266. lazyBuf = append(lazyBuf, iter.buf[start:iter.head]...)
  267. return &arrayLazyAny{baseAny{}, lazyBuf, reusableIter, nil, nil, lazyBuf}
  268. }
  269. }
  270. }
  271. lazyBuf = append(lazyBuf, iter.buf[iter.head:iter.tail]...)
  272. if !iter.loadMore() {
  273. iter.reportError("skipArray", "incomplete array")
  274. return &invalidAny{}
  275. }
  276. }
  277. }