1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-23 18:54:21 +02:00
json-iterator/feature_reflect_array.go

111 lines
2.9 KiB
Go
Raw Normal View History

2017-01-07 07:49:50 +08:00
package jsoniter
import (
2017-06-19 23:43:53 +08:00
"fmt"
"io"
2017-06-06 23:27:00 +08:00
"reflect"
"unsafe"
2017-01-07 07:49:50 +08:00
)
func decoderOfArray(cfg *frozenConfig, prefix string, typ reflect.Type) ValDecoder {
decoder := decoderOfType(cfg, prefix+"[array]->", typ.Elem())
return &arrayDecoder{typ, typ.Elem(), decoder}
2017-01-09 19:48:57 +08:00
}
func encoderOfArray(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
if typ.Len() == 0 {
return emptyArrayEncoder{}
}
encoder := encoderOfType(cfg, prefix+"[array]->", typ.Elem())
2017-04-16 14:05:08 +08:00
if typ.Elem().Kind() == reflect.Map {
encoder = &OptionalEncoder{encoder}
2017-04-16 14:05:08 +08:00
}
return &arrayEncoder{typ, typ.Elem(), encoder}
2017-01-09 19:48:57 +08:00
}
type emptyArrayEncoder struct{}
func (encoder emptyArrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteEmptyArray()
}
func (encoder emptyArrayEncoder) EncodeInterface(val interface{}, stream *Stream) {
stream.WriteEmptyArray()
}
func (encoder emptyArrayEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return true
}
2017-06-19 23:43:28 +08:00
type arrayEncoder struct {
arrayType reflect.Type
2017-01-09 19:48:57 +08:00
elemType reflect.Type
elemEncoder ValEncoder
2017-01-09 19:48:57 +08:00
}
2017-06-20 15:11:01 +08:00
func (encoder *arrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
2017-01-09 19:48:57 +08:00
stream.WriteArrayStart()
2017-07-09 11:24:26 +08:00
elemPtr := unsafe.Pointer(ptr)
encoder.elemEncoder.Encode(elemPtr, stream)
2017-06-19 23:43:28 +08:00
for i := 1; i < encoder.arrayType.Len(); i++ {
2017-01-09 19:48:57 +08:00
stream.WriteMore()
2017-07-09 11:24:26 +08:00
elemPtr = unsafe.Pointer(uintptr(elemPtr) + encoder.elemType.Size())
2017-06-20 15:11:01 +08:00
encoder.elemEncoder.Encode(unsafe.Pointer(elemPtr), stream)
2017-01-09 19:48:57 +08:00
}
stream.WriteArrayEnd()
if stream.Error != nil && stream.Error != io.EOF {
2017-06-19 23:43:28 +08:00
stream.Error = fmt.Errorf("%v: %s", encoder.arrayType, stream.Error.Error())
2017-01-09 19:48:57 +08:00
}
}
2017-06-20 15:11:01 +08:00
func (encoder *arrayEncoder) EncodeInterface(val interface{}, stream *Stream) {
// special optimization for interface{}
e := (*emptyInterface)(unsafe.Pointer(&val))
if e.word == nil {
stream.WriteArrayStart()
stream.WriteNil()
stream.WriteArrayEnd()
return
}
elemType := encoder.arrayType.Elem()
2017-06-29 20:40:25 +08:00
if encoder.arrayType.Len() == 1 && (elemType.Kind() == reflect.Ptr || elemType.Kind() == reflect.Map) {
ptr := uintptr(e.word)
e.word = unsafe.Pointer(&ptr)
}
if reflect.TypeOf(val).Kind() == reflect.Ptr {
encoder.Encode(unsafe.Pointer(&e.word), stream)
} else {
encoder.Encode(e.word, stream)
}
2017-01-26 00:25:17 +08:00
}
2017-06-20 15:11:01 +08:00
func (encoder *arrayEncoder) IsEmpty(ptr unsafe.Pointer) bool {
2017-06-19 23:43:28 +08:00
return false
2017-03-08 07:38:25 -08:00
}
2017-06-19 23:43:28 +08:00
type arrayDecoder struct {
arrayType reflect.Type
2017-01-07 07:49:50 +08:00
elemType reflect.Type
elemDecoder ValDecoder
2017-01-07 07:49:50 +08:00
}
2017-06-20 15:11:01 +08:00
func (decoder *arrayDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
2017-01-07 07:49:50 +08:00
decoder.doDecode(ptr, iter)
if iter.Error != nil && iter.Error != io.EOF {
2017-06-19 23:43:28 +08:00
iter.Error = fmt.Errorf("%v: %s", decoder.arrayType, iter.Error.Error())
2017-01-07 07:49:50 +08:00
}
}
2017-06-19 23:43:28 +08:00
func (decoder *arrayDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
2017-01-07 07:49:50 +08:00
offset := uintptr(0)
iter.ReadArrayCB(func(iter *Iterator) bool {
2017-06-19 23:43:28 +08:00
if offset < decoder.arrayType.Size() {
2017-06-20 15:11:01 +08:00
decoder.elemDecoder.Decode(unsafe.Pointer(uintptr(ptr)+offset), iter)
offset += decoder.elemType.Size()
2017-06-19 23:43:28 +08:00
} else {
iter.Skip()
2017-01-07 07:49:50 +08:00
}
return true
})
2017-06-06 23:27:00 +08:00
}