feature_any.go 5.4 KB

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