1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00
json-iterator/feature_reflect_array.go

85 lines
2.0 KiB
Go
Raw Normal View History

2017-01-07 01:49:50 +02:00
package jsoniter
import (
2017-06-19 17:43:53 +02:00
"fmt"
"io"
2017-06-06 17:27:00 +02:00
"reflect"
"unsafe"
2017-01-07 01:49:50 +02:00
)
2017-06-19 17:43:28 +02:00
func decoderOfArray(cfg *frozenConfig, typ reflect.Type) (Decoder, error) {
2017-06-13 10:58:53 +02:00
decoder, err := decoderOfType(cfg, typ.Elem())
2017-01-09 13:48:57 +02:00
if err != nil {
return nil, err
}
2017-06-19 17:43:28 +02:00
return &arrayDecoder{typ, typ.Elem(), decoder}, nil
2017-01-09 13:48:57 +02:00
}
2017-06-19 17:43:28 +02:00
func encoderOfArray(cfg *frozenConfig, typ reflect.Type) (Encoder, error) {
2017-06-13 10:58:53 +02:00
encoder, err := encoderOfType(cfg, typ.Elem())
2017-01-09 13:48:57 +02:00
if err != nil {
return nil, err
}
2017-04-16 08:05:08 +02:00
if typ.Elem().Kind() == reflect.Map {
2017-06-06 17:27:00 +02:00
encoder = &optionalEncoder{encoder}
2017-04-16 08:05:08 +02:00
}
2017-06-19 17:43:28 +02:00
return &arrayEncoder{typ, typ.Elem(), encoder}, nil
2017-01-09 13:48:57 +02:00
}
2017-06-19 17:43:28 +02:00
type arrayEncoder struct {
arrayType reflect.Type
2017-01-09 13:48:57 +02:00
elemType reflect.Type
elemEncoder Encoder
}
2017-06-19 17:43:28 +02:00
func (encoder *arrayEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
if ptr == nil {
stream.WriteNil()
return
}
2017-01-09 13:48:57 +02:00
stream.WriteArrayStart()
2017-06-19 17:43:28 +02:00
elemPtr := uintptr(ptr)
2017-01-09 13:48:57 +02:00
encoder.elemEncoder.encode(unsafe.Pointer(elemPtr), stream)
2017-06-19 17:43:28 +02:00
for i := 1; i < encoder.arrayType.Len(); i++ {
2017-01-09 13:48:57 +02:00
stream.WriteMore()
elemPtr += encoder.elemType.Size()
encoder.elemEncoder.encode(unsafe.Pointer(elemPtr), stream)
}
stream.WriteArrayEnd()
if stream.Error != nil && stream.Error != io.EOF {
2017-06-19 17:43:28 +02:00
stream.Error = fmt.Errorf("%v: %s", encoder.arrayType, stream.Error.Error())
2017-01-09 13:48:57 +02:00
}
}
2017-06-19 17:43:28 +02:00
func (encoder *arrayEncoder) encodeInterface(val interface{}, stream *Stream) {
writeToStream(val, stream, encoder)
2017-01-25 18:25:17 +02:00
}
2017-06-19 17:43:28 +02:00
func (encoder *arrayEncoder) isEmpty(ptr unsafe.Pointer) bool {
return false
2017-03-08 17:38:25 +02:00
}
2017-06-19 17:43:28 +02:00
type arrayDecoder struct {
arrayType reflect.Type
2017-01-07 01:49:50 +02:00
elemType reflect.Type
elemDecoder Decoder
}
2017-06-19 17:43:28 +02:00
func (decoder *arrayDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
2017-01-07 01:49:50 +02:00
decoder.doDecode(ptr, iter)
if iter.Error != nil && iter.Error != io.EOF {
2017-06-19 17:43:28 +02:00
iter.Error = fmt.Errorf("%v: %s", decoder.arrayType, iter.Error.Error())
2017-01-07 01:49:50 +02:00
}
}
2017-06-19 17:43:28 +02:00
func (decoder *arrayDecoder) doDecode(ptr unsafe.Pointer, iter *Iterator) {
2017-01-07 01:49:50 +02:00
offset := uintptr(0)
2017-06-19 17:43:28 +02:00
for ; iter.ReadArray(); offset += decoder.elemType.Size() {
if offset < decoder.arrayType.Size() {
decoder.elemDecoder.decode(unsafe.Pointer(uintptr(ptr)+offset), iter)
} else {
iter.Skip()
2017-01-07 01:49:50 +02:00
}
}
2017-06-06 17:27:00 +02:00
}