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
|
|
|
)
|
|
|
|
|
2017-12-15 10:13:11 +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
|
|
|
}
|
|
|
|
|
2017-12-15 10:13:11 +08:00
|
|
|
func encoderOfArray(cfg *frozenConfig, prefix string, typ reflect.Type) ValEncoder {
|
2018-01-22 14:03:50 -08:00
|
|
|
if typ.Len() == 0 {
|
|
|
|
return emptyArrayEncoder{}
|
|
|
|
}
|
2017-12-15 10:13:11 +08:00
|
|
|
encoder := encoderOfType(cfg, prefix+"[array]->", typ.Elem())
|
2017-04-16 14:05:08 +08:00
|
|
|
if typ.Elem().Kind() == reflect.Map {
|
2017-11-08 11:41:45 +08:00
|
|
|
encoder = &OptionalEncoder{encoder}
|
2017-04-16 14:05:08 +08:00
|
|
|
}
|
2017-12-15 10:13:11 +08:00
|
|
|
return &arrayEncoder{typ, typ.Elem(), encoder}
|
2017-01-09 19:48:57 +08:00
|
|
|
}
|
|
|
|
|
2018-01-22 14:03:50 -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
|
2017-06-20 07:59:45 +08:00
|
|
|
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) {
|
2017-06-26 22:37:24 +08:00
|
|
|
// special optimization for interface{}
|
|
|
|
e := (*emptyInterface)(unsafe.Pointer(&val))
|
|
|
|
if e.word == nil {
|
|
|
|
stream.WriteArrayStart()
|
|
|
|
stream.WriteNil()
|
|
|
|
stream.WriteArrayEnd()
|
|
|
|
return
|
|
|
|
}
|
2017-06-29 18:58:40 +08:00
|
|
|
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) {
|
2017-06-29 18:58:40 +08:00
|
|
|
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
|
2017-06-20 07:59:45 +08:00
|
|
|
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)
|
2017-07-17 09:09:00 +08:00
|
|
|
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)
|
2017-07-17 09:09:00 +08:00
|
|
|
offset += decoder.elemType.Size()
|
2017-06-19 23:43:28 +08:00
|
|
|
} else {
|
|
|
|
iter.Skip()
|
2017-01-07 07:49:50 +08:00
|
|
|
}
|
2017-07-17 09:09:00 +08:00
|
|
|
return true
|
|
|
|
})
|
2017-06-06 23:27:00 +08:00
|
|
|
}
|