6 "github.com/modern-go/reflect2"
13 // Any generic object representation.
14 // The lazy json implementation holds []byte and parse lazily.
29 ToVal(val interface{})
30 Get(path ...interface{}) Any
33 GetInterface() interface{}
34 WriteTo(stream *Stream)
39 func (any *baseAny) Get(path ...interface{}) Any {
40 return &invalidAny{baseAny{}, fmt.Errorf("GetIndex %v from simple value", path)}
43 func (any *baseAny) Size() int {
47 func (any *baseAny) Keys() []string {
51 func (any *baseAny) ToVal(obj interface{}) {
52 panic("not implemented")
55 // WrapInt32 turn int32 into Any interface
56 func WrapInt32(val int32) Any {
57 return &int32Any{baseAny{}, val}
60 // WrapInt64 turn int64 into Any interface
61 func WrapInt64(val int64) Any {
62 return &int64Any{baseAny{}, val}
65 // WrapUint32 turn uint32 into Any interface
66 func WrapUint32(val uint32) Any {
67 return &uint32Any{baseAny{}, val}
70 // WrapUint64 turn uint64 into Any interface
71 func WrapUint64(val uint64) Any {
72 return &uint64Any{baseAny{}, val}
75 // WrapFloat64 turn float64 into Any interface
76 func WrapFloat64(val float64) Any {
77 return &floatAny{baseAny{}, val}
80 // WrapString turn string into Any interface
81 func WrapString(val string) Any {
82 return &stringAny{baseAny{}, val}
85 // Wrap turn a go object into Any interface
86 func Wrap(val interface{}) Any {
90 asAny, isAny := val.(Any)
94 typ := reflect2.TypeOf(val)
99 return wrapStruct(val)
103 return WrapString(val.(string))
105 if strconv.IntSize == 32 {
106 return WrapInt32(int32(val.(int)))
108 return WrapInt64(int64(val.(int)))
110 return WrapInt32(int32(val.(int8)))
112 return WrapInt32(int32(val.(int16)))
114 return WrapInt32(val.(int32))
116 return WrapInt64(val.(int64))
118 if strconv.IntSize == 32 {
119 return WrapUint32(uint32(val.(uint)))
121 return WrapUint64(uint64(val.(uint)))
122 case reflect.Uintptr:
124 return WrapUint32(uint32(val.(uintptr)))
126 return WrapUint64(uint64(val.(uintptr)))
128 return WrapUint32(uint32(val.(uint8)))
130 return WrapUint32(uint32(val.(uint16)))
132 return WrapUint32(uint32(val.(uint32)))
134 return WrapUint64(val.(uint64))
135 case reflect.Float32:
136 return WrapFloat64(float64(val.(float32)))
137 case reflect.Float64:
138 return WrapFloat64(val.(float64))
140 if val.(bool) == true {
145 return &invalidAny{baseAny{}, fmt.Errorf("unsupported type: %v", typ)}
148 // ReadAny read next JSON element as an Any object. It is a better json.RawMessage.
149 func (iter *Iterator) ReadAny() Any {
150 return iter.readAny()
153 func (iter *Iterator) readAny() Any {
154 c := iter.nextToken()
158 return &stringAny{baseAny{}, iter.ReadString()}
160 iter.skipThreeBytes('u', 'l', 'l') // null
163 iter.skipThreeBytes('r', 'u', 'e') // true
166 iter.skipFourBytes('a', 'l', 's', 'e') // false
169 return iter.readObjectAny()
171 return iter.readArrayAny()
173 return iter.readNumberAny(false)
175 return &invalidAny{baseAny{}, errors.New("input is empty")}
177 return iter.readNumberAny(true)
181 func (iter *Iterator) readNumberAny(positive bool) Any {
182 iter.startCapture(iter.head - 1)
184 lazyBuf := iter.stopCapture()
185 return &numberLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
188 func (iter *Iterator) readObjectAny() Any {
189 iter.startCapture(iter.head - 1)
191 lazyBuf := iter.stopCapture()
192 return &objectLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
195 func (iter *Iterator) readArrayAny() Any {
196 iter.startCapture(iter.head - 1)
198 lazyBuf := iter.stopCapture()
199 return &arrayLazyAny{baseAny{}, iter.cfg, lazyBuf, nil}
202 func locateObjectField(iter *Iterator, target string) []byte {
204 iter.ReadObjectCB(func(iter *Iterator, field string) bool {
206 found = iter.SkipAndReturnBytes()
215 func locateArrayElement(iter *Iterator, target int) []byte {
218 iter.ReadArrayCB(func(iter *Iterator) bool {
220 found = iter.SkipAndReturnBytes()
230 func locatePath(iter *Iterator, path []interface{}) Any {
231 for i, pathKeyObj := range path {
232 switch pathKey := pathKeyObj.(type) {
234 valueBytes := locateObjectField(iter, pathKey)
235 if valueBytes == nil {
236 return newInvalidAny(path[i:])
238 iter.ResetBytes(valueBytes)
240 valueBytes := locateArrayElement(iter, pathKey)
241 if valueBytes == nil {
242 return newInvalidAny(path[i:])
244 iter.ResetBytes(valueBytes)
247 return iter.readAny().Get(path[i:]...)
249 return newInvalidAny(path[i:])
251 return newInvalidAny(path[i:])
254 if iter.Error != nil && iter.Error != io.EOF {
255 return &invalidAny{baseAny{}, iter.Error}
257 return iter.readAny()
260 var anyType = reflect2.TypeOfPtr((*Any)(nil)).Elem()
262 func createDecoderOfAny(ctx *ctx, typ reflect2.Type) ValDecoder {
264 return &directAnyCodec{}
266 if typ.Implements(anyType) {
274 func createEncoderOfAny(ctx *ctx, typ reflect2.Type) ValEncoder {
276 return &directAnyCodec{}
278 if typ.Implements(anyType) {
286 type anyCodec struct {
287 valType reflect2.Type
290 func (codec *anyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
291 panic("not implemented")
294 func (codec *anyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
295 obj := codec.valType.UnsafeIndirect(ptr)
300 func (codec *anyCodec) IsEmpty(ptr unsafe.Pointer) bool {
301 obj := codec.valType.UnsafeIndirect(ptr)
303 return any.Size() == 0
306 type directAnyCodec struct {
309 func (codec *directAnyCodec) Decode(ptr unsafe.Pointer, iter *Iterator) {
310 *(*Any)(ptr) = iter.readAny()
313 func (codec *directAnyCodec) Encode(ptr unsafe.Pointer, stream *Stream) {
322 func (codec *directAnyCodec) IsEmpty(ptr unsafe.Pointer) bool {
324 return any.Size() == 0