feature_any.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. package jsoniter
  2. import (
  3. "errors"
  4. "fmt"
  5. "io"
  6. "reflect"
  7. )
  8. // Any generic object representation.
  9. // The lazy json implementation holds []byte and parse lazily.
  10. type Any interface {
  11. LastError() error
  12. ValueType() ValueType
  13. MustBeValid() Any
  14. ToBool() bool
  15. ToInt() int
  16. ToInt32() int32
  17. ToInt64() int64
  18. ToUint() uint
  19. ToUint32() uint32
  20. ToUint64() uint64
  21. ToFloat32() float32
  22. ToFloat64() float64
  23. ToString() string
  24. ToVal(val interface{})
  25. Get(path ...interface{}) Any
  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. case 0:
  145. return &invalidAny{baseAny{}, errors.New("input is empty")}
  146. default:
  147. return iter.readNumberAny(true)
  148. }
  149. }
  150. func (iter *Iterator) readNumberAny(positive bool) Any {
  151. iter.startCapture(iter.head - 1)
  152. iter.skipNumber()
  153. lazyBuf := iter.stopCapture()
  154. return &numberLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
  155. }
  156. func (iter *Iterator) readObjectAny() Any {
  157. iter.startCapture(iter.head - 1)
  158. iter.skipObject()
  159. lazyBuf := iter.stopCapture()
  160. return &objectLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
  161. }
  162. func (iter *Iterator) readArrayAny() Any {
  163. iter.startCapture(iter.head - 1)
  164. iter.skipArray()
  165. lazyBuf := iter.stopCapture()
  166. return &arrayLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
  167. }
  168. func locateObjectField(iter *Iterator, target string) []byte {
  169. var found []byte
  170. iter.ReadObjectCB(func(iter *Iterator, field string) bool {
  171. if field == target {
  172. found = iter.SkipAndReturnBytes()
  173. return false
  174. }
  175. iter.Skip()
  176. return true
  177. })
  178. return found
  179. }
  180. func locateArrayElement(iter *Iterator, target int) []byte {
  181. var found []byte
  182. n := 0
  183. iter.ReadArrayCB(func(iter *Iterator) bool {
  184. if n == target {
  185. found = iter.SkipAndReturnBytes()
  186. return false
  187. }
  188. iter.Skip()
  189. n++
  190. return true
  191. })
  192. return found
  193. }
  194. func locatePath(iter *Iterator, path []interface{}) Any {
  195. for i, pathKeyObj := range path {
  196. switch pathKey := pathKeyObj.(type) {
  197. case string:
  198. valueBytes := locateObjectField(iter, pathKey)
  199. if valueBytes == nil {
  200. return newInvalidAny(path[i:])
  201. }
  202. iter.ResetBytes(valueBytes)
  203. case int:
  204. valueBytes := locateArrayElement(iter, pathKey)
  205. if valueBytes == nil {
  206. return newInvalidAny(path[i:])
  207. }
  208. iter.ResetBytes(valueBytes)
  209. case int32:
  210. if '*' == pathKey {
  211. return iter.readAny().Get(path[i:]...)
  212. }
  213. return newInvalidAny(path[i:])
  214. default:
  215. return newInvalidAny(path[i:])
  216. }
  217. }
  218. if iter.Error != nil && iter.Error != io.EOF {
  219. return &invalidAny{baseAny{}, iter.Error}
  220. }
  221. return iter.readAny()
  222. }