feature_any.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. package jsoniter
  2. import (
  3. "fmt"
  4. "io"
  5. "reflect"
  6. )
  7. // Any generic object representation.
  8. // The lazy json implementation holds []byte and parse lazily.
  9. type Any interface {
  10. LastError() error
  11. ValueType() ValueType
  12. MustBeValid() Any
  13. ToBool() bool
  14. ToInt() int
  15. ToInt32() int32
  16. ToInt64() int64
  17. ToUint() uint
  18. ToUint32() uint32
  19. ToUint64() uint64
  20. ToFloat32() float32
  21. ToFloat64() float64
  22. ToString() string
  23. ToVal(val interface{})
  24. Get(path ...interface{}) Any
  25. // TODO: add Set
  26. Size() int
  27. Keys() []string
  28. GetInterface() interface{}
  29. WriteTo(stream *Stream)
  30. }
  31. type baseAny struct{}
  32. func (any *baseAny) Get(path ...interface{}) Any {
  33. return &invalidAny{baseAny{}, fmt.Errorf("Get %v from simple value", path)}
  34. }
  35. func (any *baseAny) Size() int {
  36. return 0
  37. }
  38. func (any *baseAny) Keys() []string {
  39. return []string{}
  40. }
  41. func (any *baseAny) ToVal(obj interface{}) {
  42. panic("not implemented")
  43. }
  44. // WrapInt32 turn int32 into Any interface
  45. func WrapInt32(val int32) Any {
  46. return &int32Any{baseAny{}, val}
  47. }
  48. // WrapInt64 turn int64 into Any interface
  49. func WrapInt64(val int64) Any {
  50. return &int64Any{baseAny{}, val}
  51. }
  52. // WrapUint32 turn uint32 into Any interface
  53. func WrapUint32(val uint32) Any {
  54. return &uint32Any{baseAny{}, val}
  55. }
  56. // WrapUint64 turn uint64 into Any interface
  57. func WrapUint64(val uint64) Any {
  58. return &uint64Any{baseAny{}, val}
  59. }
  60. // WrapFloat64 turn float64 into Any interface
  61. func WrapFloat64(val float64) Any {
  62. return &floatAny{baseAny{}, val}
  63. }
  64. // WrapString turn string into Any interface
  65. func WrapString(val string) Any {
  66. return &stringAny{baseAny{}, val}
  67. }
  68. // Wrap turn a go object into Any interface
  69. func Wrap(val interface{}) Any {
  70. if val == nil {
  71. return &nilAny{}
  72. }
  73. asAny, isAny := val.(Any)
  74. if isAny {
  75. return asAny
  76. }
  77. typ := reflect.TypeOf(val)
  78. switch typ.Kind() {
  79. case reflect.Slice:
  80. return wrapArray(val)
  81. case reflect.Struct:
  82. return wrapStruct(val)
  83. case reflect.Map:
  84. return wrapMap(val)
  85. case reflect.String:
  86. return WrapString(val.(string))
  87. case reflect.Int:
  88. return WrapInt64(int64(val.(int)))
  89. case reflect.Int8:
  90. return WrapInt32(int32(val.(int8)))
  91. case reflect.Int16:
  92. return WrapInt32(int32(val.(int16)))
  93. case reflect.Int32:
  94. return WrapInt32(val.(int32))
  95. case reflect.Int64:
  96. return WrapInt64(val.(int64))
  97. case reflect.Uint:
  98. return WrapUint64(uint64(val.(uint)))
  99. case reflect.Uint8:
  100. return WrapUint32(uint32(val.(uint8)))
  101. case reflect.Uint16:
  102. return WrapUint32(uint32(val.(uint16)))
  103. case reflect.Uint32:
  104. return WrapUint32(uint32(val.(uint32)))
  105. case reflect.Uint64:
  106. return WrapUint64(val.(uint64))
  107. case reflect.Float32:
  108. return WrapFloat64(float64(val.(float32)))
  109. case reflect.Float64:
  110. return WrapFloat64(val.(float64))
  111. case reflect.Bool:
  112. if val.(bool) == true {
  113. return &trueAny{}
  114. }
  115. return &falseAny{}
  116. }
  117. return &invalidAny{baseAny{}, fmt.Errorf("unsupported type: %v", typ)}
  118. }
  119. // ReadAny read next JSON element as an Any object. It is a better json.RawMessage.
  120. func (iter *Iterator) ReadAny() Any {
  121. return iter.readAny()
  122. }
  123. func (iter *Iterator) readAny() Any {
  124. c := iter.nextToken()
  125. switch c {
  126. case '"':
  127. iter.unreadByte()
  128. return &stringAny{baseAny{}, iter.ReadString()}
  129. case 'n':
  130. iter.skipThreeBytes('u', 'l', 'l') // null
  131. return &nilAny{}
  132. case 't':
  133. iter.skipThreeBytes('r', 'u', 'e') // true
  134. return &trueAny{}
  135. case 'f':
  136. iter.skipFourBytes('a', 'l', 's', 'e') // false
  137. return &falseAny{}
  138. case '{':
  139. return iter.readObjectAny()
  140. case '[':
  141. return iter.readArrayAny()
  142. case '-':
  143. return iter.readNumberAny(false)
  144. default:
  145. return iter.readNumberAny(true)
  146. }
  147. }
  148. func (iter *Iterator) readNumberAny(positive bool) Any {
  149. iter.startCapture(iter.head - 1)
  150. iter.skipNumber()
  151. lazyBuf := iter.stopCapture()
  152. return &numberLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
  153. }
  154. func (iter *Iterator) readObjectAny() Any {
  155. iter.startCapture(iter.head - 1)
  156. iter.skipObject()
  157. lazyBuf := iter.stopCapture()
  158. return &objectLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
  159. }
  160. func (iter *Iterator) readArrayAny() Any {
  161. iter.startCapture(iter.head - 1)
  162. iter.skipArray()
  163. lazyBuf := iter.stopCapture()
  164. return &arrayLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
  165. }
  166. func locateObjectField(iter *Iterator, target string) []byte {
  167. var found []byte
  168. iter.ReadObjectCB(func(iter *Iterator, field string) bool {
  169. if field == target {
  170. found = iter.SkipAndReturnBytes()
  171. return false
  172. }
  173. iter.Skip()
  174. return true
  175. })
  176. return found
  177. }
  178. func locateArrayElement(iter *Iterator, target int) []byte {
  179. var found []byte
  180. n := 0
  181. iter.ReadArrayCB(func(iter *Iterator) bool {
  182. if n == target {
  183. found = iter.SkipAndReturnBytes()
  184. return false
  185. }
  186. iter.Skip()
  187. n++
  188. return true
  189. })
  190. return found
  191. }
  192. func locatePath(iter *Iterator, path []interface{}) Any {
  193. for i, pathKeyObj := range path {
  194. switch pathKey := pathKeyObj.(type) {
  195. case string:
  196. valueBytes := locateObjectField(iter, pathKey)
  197. if valueBytes == nil {
  198. return newInvalidAny(path[i:])
  199. }
  200. iter.ResetBytes(valueBytes)
  201. case int:
  202. valueBytes := locateArrayElement(iter, pathKey)
  203. if valueBytes == nil {
  204. return newInvalidAny(path[i:])
  205. }
  206. iter.ResetBytes(valueBytes)
  207. case int32:
  208. if '*' == pathKey {
  209. return iter.readAny().Get(path[i:]...)
  210. }
  211. return newInvalidAny(path[i:])
  212. default:
  213. return newInvalidAny(path[i:])
  214. }
  215. }
  216. if iter.Error != nil && iter.Error != io.EOF {
  217. return &invalidAny{baseAny{}, iter.Error}
  218. }
  219. return iter.readAny()
  220. }