2016-12-04 05:06:38 +02:00
|
|
|
package jsoniter
|
|
|
|
|
|
|
|
import (
|
2017-06-06 17:27:00 +02:00
|
|
|
"encoding"
|
|
|
|
"encoding/json"
|
2016-12-04 06:27:34 +02:00
|
|
|
"fmt"
|
2017-01-05 15:23:08 +02:00
|
|
|
"reflect"
|
2017-06-20 01:39:38 +02:00
|
|
|
"time"
|
2017-06-20 01:39:54 +02:00
|
|
|
"unsafe"
|
2016-12-04 05:06:38 +02:00
|
|
|
)
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
// ValDecoder is an internal type registered to cache as needed.
|
|
|
|
// Don't confuse jsoniter.ValDecoder with json.Decoder.
|
2017-06-11 10:28:31 +02:00
|
|
|
// For json.Decoder's adapter, refer to jsoniter.AdapterDecoder(todo link).
|
2017-06-11 10:30:31 +02:00
|
|
|
//
|
2017-06-11 10:28:31 +02:00
|
|
|
// Reflection on type to create decoders, which is then cached
|
|
|
|
// Reflection on value is avoided as we can, as the reflect.Value itself will allocate, with following exceptions
|
|
|
|
// 1. create instance of new value, for example *int will need a int to be allocated
|
|
|
|
// 2. append to slice, if the existing cap is not enough, allocate will be done using Reflect.New
|
|
|
|
// 3. assignment to map, both key and value will be reflect.Value
|
|
|
|
// For a simple struct binding, it will be reflect.Value free and allocation free
|
2017-06-20 01:59:45 +02:00
|
|
|
type ValDecoder interface {
|
2017-06-20 09:11:01 +02:00
|
|
|
Decode(ptr unsafe.Pointer, iter *Iterator)
|
2016-12-04 05:06:38 +02:00
|
|
|
}
|
2017-06-11 10:30:31 +02:00
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
// ValEncoder is an internal type registered to cache as needed.
|
|
|
|
// Don't confuse jsoniter.ValEncoder with json.Encoder.
|
2017-06-11 10:28:31 +02:00
|
|
|
// For json.Encoder's adapter, refer to jsoniter.AdapterEncoder(todo godoc link).
|
2017-06-20 01:59:45 +02:00
|
|
|
type ValEncoder interface {
|
2017-06-20 09:11:01 +02:00
|
|
|
IsEmpty(ptr unsafe.Pointer) bool
|
|
|
|
Encode(ptr unsafe.Pointer, stream *Stream)
|
|
|
|
EncodeInterface(val interface{}, stream *Stream)
|
2017-01-25 18:25:17 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 11:43:47 +02:00
|
|
|
func WriteToStream(val interface{}, stream *Stream, encoder ValEncoder) {
|
2017-01-25 18:25:17 +02:00
|
|
|
e := (*emptyInterface)(unsafe.Pointer(&val))
|
2017-06-13 03:14:19 +02:00
|
|
|
if e.word == nil {
|
|
|
|
stream.WriteNil()
|
|
|
|
return
|
|
|
|
}
|
2017-01-25 18:25:17 +02:00
|
|
|
if reflect.TypeOf(val).Kind() == reflect.Ptr {
|
2017-06-20 09:11:01 +02:00
|
|
|
encoder.Encode(unsafe.Pointer(&e.word), stream)
|
2017-01-25 18:25:17 +02:00
|
|
|
} else {
|
2017-06-20 09:11:01 +02:00
|
|
|
encoder.Encode(e.word, stream)
|
2017-01-25 18:25:17 +02:00
|
|
|
}
|
2017-01-09 11:47:21 +02:00
|
|
|
}
|
2016-12-04 05:06:38 +02:00
|
|
|
|
2017-01-07 01:49:50 +02:00
|
|
|
type DecoderFunc func(ptr unsafe.Pointer, iter *Iterator)
|
2017-01-25 16:43:57 +02:00
|
|
|
type EncoderFunc func(ptr unsafe.Pointer, stream *Stream)
|
2017-06-20 02:08:59 +02:00
|
|
|
|
2017-06-02 04:21:43 +02:00
|
|
|
var jsonNumberType reflect.Type
|
2017-06-02 04:50:23 +02:00
|
|
|
var jsonRawMessageType reflect.Type
|
2017-06-19 17:10:20 +02:00
|
|
|
var jsoniterRawMessageType reflect.Type
|
2017-01-25 18:25:17 +02:00
|
|
|
var anyType reflect.Type
|
2017-05-24 08:34:00 +02:00
|
|
|
var marshalerType reflect.Type
|
2017-05-24 10:04:11 +02:00
|
|
|
var unmarshalerType reflect.Type
|
2017-06-05 18:09:33 +02:00
|
|
|
var textUnmarshalerType reflect.Type
|
2016-12-04 14:50:55 +02:00
|
|
|
|
2017-01-07 01:49:50 +02:00
|
|
|
func init() {
|
2017-06-02 04:21:43 +02:00
|
|
|
jsonNumberType = reflect.TypeOf((*json.Number)(nil)).Elem()
|
2017-06-02 04:50:23 +02:00
|
|
|
jsonRawMessageType = reflect.TypeOf((*json.RawMessage)(nil)).Elem()
|
2017-06-19 17:10:20 +02:00
|
|
|
jsoniterRawMessageType = reflect.TypeOf((*RawMessage)(nil)).Elem()
|
2017-01-25 18:25:17 +02:00
|
|
|
anyType = reflect.TypeOf((*Any)(nil)).Elem()
|
2017-05-24 08:34:00 +02:00
|
|
|
marshalerType = reflect.TypeOf((*json.Marshaler)(nil)).Elem()
|
2017-05-24 10:04:11 +02:00
|
|
|
unmarshalerType = reflect.TypeOf((*json.Unmarshaler)(nil)).Elem()
|
2017-06-05 18:09:33 +02:00
|
|
|
textUnmarshalerType = reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem()
|
2016-12-07 02:49:52 +02:00
|
|
|
}
|
|
|
|
|
2017-01-07 01:49:50 +02:00
|
|
|
type optionalDecoder struct {
|
|
|
|
valueType reflect.Type
|
2017-06-20 01:59:45 +02:00
|
|
|
valueDecoder ValDecoder
|
2017-01-07 01:49:50 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:11:01 +02:00
|
|
|
func (decoder *optionalDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
2017-01-07 01:49:50 +02:00
|
|
|
if iter.ReadNil() {
|
|
|
|
*((*unsafe.Pointer)(ptr)) = nil
|
|
|
|
} else {
|
|
|
|
if *((*unsafe.Pointer)(ptr)) == nil {
|
|
|
|
// pointer to null, we have to allocate memory to hold the value
|
|
|
|
value := reflect.New(decoder.valueType)
|
2017-06-20 09:11:01 +02:00
|
|
|
decoder.valueDecoder.Decode(unsafe.Pointer(value.Pointer()), iter)
|
2017-01-07 01:49:50 +02:00
|
|
|
*((*uintptr)(ptr)) = value.Pointer()
|
|
|
|
} else {
|
|
|
|
// reuse existing instance
|
2017-06-20 09:11:01 +02:00
|
|
|
decoder.valueDecoder.Decode(*((*unsafe.Pointer)(ptr)), iter)
|
2017-01-07 01:49:50 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-09 14:51:09 +02:00
|
|
|
type optionalEncoder struct {
|
2017-06-20 01:59:45 +02:00
|
|
|
valueEncoder ValEncoder
|
2017-01-09 14:51:09 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:11:01 +02:00
|
|
|
func (encoder *optionalEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
2017-01-09 14:51:09 +02:00
|
|
|
if *((*unsafe.Pointer)(ptr)) == nil {
|
2017-01-18 17:33:40 +02:00
|
|
|
stream.WriteNil()
|
2017-01-09 14:51:09 +02:00
|
|
|
} else {
|
2017-06-20 09:11:01 +02:00
|
|
|
encoder.valueEncoder.Encode(*((*unsafe.Pointer)(ptr)), stream)
|
2017-01-09 14:51:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 09:11:01 +02:00
|
|
|
func (encoder *optionalEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
2017-06-20 11:43:47 +02:00
|
|
|
WriteToStream(val, stream, encoder)
|
2017-01-25 18:25:17 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:11:01 +02:00
|
|
|
func (encoder *optionalEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
2017-03-08 17:38:25 +02:00
|
|
|
if *((*unsafe.Pointer)(ptr)) == nil {
|
|
|
|
return true
|
|
|
|
} else {
|
2017-06-20 09:11:01 +02:00
|
|
|
return encoder.valueEncoder.IsEmpty(*((*unsafe.Pointer)(ptr)))
|
2017-03-08 17:38:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-05 10:51:05 +02:00
|
|
|
type placeholderEncoder struct {
|
2017-06-20 01:39:54 +02:00
|
|
|
cfg *frozenConfig
|
2017-06-20 01:39:38 +02:00
|
|
|
cacheKey reflect.Type
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:11:01 +02:00
|
|
|
func (encoder *placeholderEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
|
|
|
encoder.getRealEncoder().Encode(ptr, stream)
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:11:01 +02:00
|
|
|
func (encoder *placeholderEncoder) EncodeInterface(val interface{}, stream *Stream) {
|
2017-06-20 11:43:47 +02:00
|
|
|
WriteToStream(val, stream, encoder)
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:11:01 +02:00
|
|
|
func (encoder *placeholderEncoder) IsEmpty(ptr unsafe.Pointer) bool {
|
|
|
|
return encoder.getRealEncoder().IsEmpty(ptr)
|
2017-06-20 01:39:38 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func (encoder *placeholderEncoder) getRealEncoder() ValEncoder {
|
2017-06-20 01:39:38 +02:00
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
realDecoder := encoder.cfg.getEncoderFromCache(encoder.cacheKey)
|
|
|
|
_, isPlaceholder := realDecoder.(*placeholderEncoder)
|
|
|
|
if isPlaceholder {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
} else {
|
|
|
|
return realDecoder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("real encoder not found for cache key: %v", encoder.cacheKey))
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
type placeholderDecoder struct {
|
2017-06-20 01:39:54 +02:00
|
|
|
cfg *frozenConfig
|
2017-06-20 01:39:38 +02:00
|
|
|
cacheKey reflect.Type
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 09:11:01 +02:00
|
|
|
func (decoder *placeholderDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
|
2017-06-20 01:39:38 +02:00
|
|
|
for i := 0; i < 30; i++ {
|
|
|
|
realDecoder := decoder.cfg.getDecoderFromCache(decoder.cacheKey)
|
|
|
|
_, isPlaceholder := realDecoder.(*placeholderDecoder)
|
|
|
|
if isPlaceholder {
|
|
|
|
time.Sleep(time.Second)
|
|
|
|
} else {
|
2017-06-20 09:11:01 +02:00
|
|
|
realDecoder.Decode(ptr, iter)
|
2017-06-20 01:39:38 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
panic(fmt.Sprintf("real decoder not found for cache key: %v", decoder.cacheKey))
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 06:27:34 +02:00
|
|
|
// emptyInterface is the header for an interface{} value.
|
|
|
|
type emptyInterface struct {
|
2017-05-26 18:36:21 +02:00
|
|
|
typ unsafe.Pointer
|
|
|
|
word unsafe.Pointer
|
|
|
|
}
|
|
|
|
|
|
|
|
// emptyInterface is the header for an interface with method (not interface{})
|
|
|
|
type nonEmptyInterface struct {
|
|
|
|
// see ../runtime/iface.go:/Itab
|
|
|
|
itab *struct {
|
|
|
|
ityp unsafe.Pointer // static interface type
|
|
|
|
typ unsafe.Pointer // dynamic concrete type
|
|
|
|
link unsafe.Pointer
|
|
|
|
bad int32
|
|
|
|
unused int32
|
|
|
|
fun [100000]unsafe.Pointer // method table
|
|
|
|
}
|
2016-12-04 06:27:34 +02:00
|
|
|
word unsafe.Pointer
|
2016-12-04 05:06:38 +02:00
|
|
|
}
|
|
|
|
|
2017-01-05 15:23:08 +02:00
|
|
|
// Read converts an Iterator instance into go interface, same as json.Unmarshal
|
2017-01-09 11:47:21 +02:00
|
|
|
func (iter *Iterator) ReadVal(obj interface{}) {
|
2017-01-05 15:23:08 +02:00
|
|
|
typ := reflect.TypeOf(obj)
|
|
|
|
cacheKey := typ.Elem()
|
2017-06-20 02:42:25 +02:00
|
|
|
decoder, err := decoderOfType(iter.cfg, cacheKey)
|
|
|
|
if err != nil {
|
|
|
|
iter.Error = err
|
|
|
|
return
|
2016-12-04 05:06:38 +02:00
|
|
|
}
|
2016-12-04 06:27:34 +02:00
|
|
|
e := (*emptyInterface)(unsafe.Pointer(&obj))
|
2017-06-20 09:11:01 +02:00
|
|
|
decoder.Decode(e.word, iter)
|
2016-12-04 06:27:34 +02:00
|
|
|
}
|
|
|
|
|
2017-01-09 11:47:21 +02:00
|
|
|
func (stream *Stream) WriteVal(val interface{}) {
|
2017-01-18 17:33:40 +02:00
|
|
|
if nil == val {
|
|
|
|
stream.WriteNil()
|
|
|
|
return
|
|
|
|
}
|
2017-01-09 11:47:21 +02:00
|
|
|
typ := reflect.TypeOf(val)
|
|
|
|
cacheKey := typ
|
2017-06-20 02:42:25 +02:00
|
|
|
encoder, err := encoderOfType(stream.cfg, cacheKey)
|
|
|
|
if err != nil {
|
|
|
|
stream.Error = err
|
|
|
|
return
|
2017-01-09 11:47:21 +02:00
|
|
|
}
|
2017-06-20 09:11:01 +02:00
|
|
|
encoder.EncodeInterface(val, stream)
|
2017-01-09 11:47:21 +02:00
|
|
|
}
|
|
|
|
|
2016-12-04 06:27:34 +02:00
|
|
|
type prefix string
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func (p prefix) addToDecoder(decoder ValDecoder, err error) (ValDecoder, error) {
|
2016-12-04 06:27:34 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %s", p, err.Error())
|
|
|
|
}
|
|
|
|
return decoder, err
|
2016-12-04 05:06:38 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func (p prefix) addToEncoder(encoder ValEncoder, err error) (ValEncoder, error) {
|
2017-01-09 13:19:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%s: %s", p, err.Error())
|
|
|
|
}
|
|
|
|
return encoder, err
|
|
|
|
}
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func decoderOfType(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
2017-05-05 10:51:05 +02:00
|
|
|
cacheKey := typ
|
2017-06-20 02:42:25 +02:00
|
|
|
decoder := cfg.getDecoderFromCache(cacheKey)
|
|
|
|
if decoder != nil {
|
|
|
|
return decoder, nil
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
2017-06-20 02:42:25 +02:00
|
|
|
decoder = getTypeDecoderFromExtension(typ)
|
|
|
|
if decoder != nil {
|
|
|
|
cfg.addDecoderToCache(cacheKey, decoder)
|
|
|
|
return decoder, nil
|
|
|
|
}
|
|
|
|
decoder = &placeholderDecoder{cfg: cfg, cacheKey: cacheKey}
|
|
|
|
cfg.addDecoderToCache(cacheKey, decoder)
|
|
|
|
decoder, err := createDecoderOfType(cfg, typ)
|
2017-06-20 10:36:22 +02:00
|
|
|
for _, extension := range extensions {
|
|
|
|
decoder = extension.DecorateDecoder(typ, decoder)
|
|
|
|
}
|
2017-06-20 02:42:25 +02:00
|
|
|
cfg.addDecoderToCache(cacheKey, decoder)
|
|
|
|
return decoder, err
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func createDecoderOfType(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
2017-06-26 03:57:45 +02:00
|
|
|
typeName := typ.String()
|
|
|
|
if typeName == "[]uint8" {
|
2017-06-06 03:44:56 +02:00
|
|
|
return &base64Codec{}, nil
|
|
|
|
}
|
2017-06-02 09:43:58 +02:00
|
|
|
if typ.AssignableTo(jsonRawMessageType) {
|
2017-06-02 04:50:23 +02:00
|
|
|
return &jsonRawMessageCodec{}, nil
|
|
|
|
}
|
2017-06-19 17:10:20 +02:00
|
|
|
if typ.AssignableTo(jsoniterRawMessageType) {
|
|
|
|
return &jsoniterRawMessageCodec{}, nil
|
|
|
|
}
|
2017-06-02 09:43:58 +02:00
|
|
|
if typ.AssignableTo(jsonNumberType) {
|
2017-06-02 04:21:43 +02:00
|
|
|
return &jsonNumberCodec{}, nil
|
|
|
|
}
|
2017-05-24 10:04:11 +02:00
|
|
|
if typ.ConvertibleTo(unmarshalerType) {
|
|
|
|
templateInterface := reflect.New(typ).Elem().Interface()
|
2017-06-20 01:59:45 +02:00
|
|
|
var decoder ValDecoder = &unmarshalerDecoder{extractInterface(templateInterface)}
|
2017-06-20 01:23:22 +02:00
|
|
|
if typ.Kind() != reflect.Struct {
|
|
|
|
decoder = &optionalDecoder{typ, decoder}
|
|
|
|
}
|
|
|
|
return decoder, nil
|
2017-05-24 10:04:11 +02:00
|
|
|
}
|
2017-06-02 04:50:23 +02:00
|
|
|
if typ.ConvertibleTo(anyType) {
|
|
|
|
return &anyCodec{}, nil
|
|
|
|
}
|
2017-01-05 15:23:08 +02:00
|
|
|
switch typ.Kind() {
|
2016-12-04 05:06:38 +02:00
|
|
|
case reflect.String:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "string" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*string)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 11:47:21 +02:00
|
|
|
return &stringCodec{}, nil
|
2016-12-04 15:19:54 +02:00
|
|
|
case reflect.Int:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*int)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 11:47:21 +02:00
|
|
|
return &intCodec{}, nil
|
2016-12-04 15:32:43 +02:00
|
|
|
case reflect.Int8:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int8" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*int8)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &int8Codec{}, nil
|
2016-12-04 15:32:43 +02:00
|
|
|
case reflect.Int16:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int16" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*int16)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &int16Codec{}, nil
|
2016-12-04 15:32:43 +02:00
|
|
|
case reflect.Int32:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int32" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*int32)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &int32Codec{}, nil
|
2016-12-04 15:32:43 +02:00
|
|
|
case reflect.Int64:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int64" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*int64)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &int64Codec{}, nil
|
2016-12-04 15:32:43 +02:00
|
|
|
case reflect.Uint:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "uint" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*uint)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uintCodec{}, nil
|
2016-12-04 15:32:43 +02:00
|
|
|
case reflect.Uint8:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "uint8" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*uint8)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uint8Codec{}, nil
|
2016-12-04 15:32:43 +02:00
|
|
|
case reflect.Uint16:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "uint16" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*uint16)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uint16Codec{}, nil
|
2016-12-04 15:32:43 +02:00
|
|
|
case reflect.Uint32:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "uint32" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*uint32)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uint32Codec{}, nil
|
2016-12-04 15:32:43 +02:00
|
|
|
case reflect.Uint64:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "uint64" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*uint64)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uint64Codec{}, nil
|
2016-12-04 16:15:12 +02:00
|
|
|
case reflect.Float32:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "float32" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*float32)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &float32Codec{}, nil
|
2016-12-04 16:15:12 +02:00
|
|
|
case reflect.Float64:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "float64" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*float64)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &float64Codec{}, nil
|
2016-12-04 16:15:12 +02:00
|
|
|
case reflect.Bool:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "bool" {
|
|
|
|
return decoderOfType(cfg, reflect.TypeOf((*bool)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &boolCodec{}, nil
|
2016-12-11 04:04:26 +02:00
|
|
|
case reflect.Interface:
|
2017-01-25 17:25:25 +02:00
|
|
|
if typ.NumMethod() == 0 {
|
2017-05-26 18:36:21 +02:00
|
|
|
return &emptyInterfaceCodec{}, nil
|
2017-01-25 17:25:25 +02:00
|
|
|
} else {
|
2017-05-26 18:36:21 +02:00
|
|
|
return &nonEmptyInterfaceCodec{}, nil
|
2017-01-25 17:25:25 +02:00
|
|
|
}
|
2016-12-04 06:27:34 +02:00
|
|
|
case reflect.Struct:
|
2017-06-26 03:57:45 +02:00
|
|
|
return prefix(fmt.Sprintf("[%s]", typeName)).addToDecoder(decoderOfStruct(cfg, typ))
|
2017-06-19 17:43:28 +02:00
|
|
|
case reflect.Array:
|
|
|
|
return prefix("[array]").addToDecoder(decoderOfArray(cfg, typ))
|
2016-12-04 07:13:38 +02:00
|
|
|
case reflect.Slice:
|
2017-06-13 10:58:53 +02:00
|
|
|
return prefix("[slice]").addToDecoder(decoderOfSlice(cfg, typ))
|
2016-12-10 18:38:07 +02:00
|
|
|
case reflect.Map:
|
2017-06-13 10:58:53 +02:00
|
|
|
return prefix("[map]").addToDecoder(decoderOfMap(cfg, typ))
|
2016-12-04 07:13:38 +02:00
|
|
|
case reflect.Ptr:
|
2017-06-13 10:58:53 +02:00
|
|
|
return prefix("[optional]").addToDecoder(decoderOfOptional(cfg, typ))
|
2016-12-04 07:13:38 +02:00
|
|
|
default:
|
2017-01-05 15:23:08 +02:00
|
|
|
return nil, fmt.Errorf("unsupported type: %v", typ)
|
2016-12-04 07:13:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func encoderOfType(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
2017-05-05 10:51:05 +02:00
|
|
|
cacheKey := typ
|
2017-06-20 02:42:25 +02:00
|
|
|
encoder := cfg.getEncoderFromCache(cacheKey)
|
|
|
|
if encoder != nil {
|
|
|
|
return encoder, nil
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
2017-06-20 02:42:25 +02:00
|
|
|
encoder = getTypeEncoderFromExtension(typ)
|
|
|
|
if encoder != nil {
|
|
|
|
cfg.addEncoderToCache(cacheKey, encoder)
|
|
|
|
return encoder, nil
|
|
|
|
}
|
|
|
|
encoder = &placeholderEncoder{cfg: cfg, cacheKey: cacheKey}
|
|
|
|
cfg.addEncoderToCache(cacheKey, encoder)
|
|
|
|
encoder, err := createEncoderOfType(cfg, typ)
|
2017-06-20 10:36:22 +02:00
|
|
|
for _, extension := range extensions {
|
|
|
|
encoder = extension.DecorateEncoder(typ, encoder)
|
|
|
|
}
|
2017-06-20 02:42:25 +02:00
|
|
|
cfg.addEncoderToCache(cacheKey, encoder)
|
|
|
|
return encoder, err
|
2017-05-05 10:51:05 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
2017-06-26 03:57:45 +02:00
|
|
|
typeName := typ.String()
|
|
|
|
if typeName == "[]uint8" {
|
2017-06-06 03:44:56 +02:00
|
|
|
return &base64Codec{}, nil
|
|
|
|
}
|
2017-06-02 09:43:58 +02:00
|
|
|
if typ.AssignableTo(jsonRawMessageType) {
|
2017-06-02 04:50:23 +02:00
|
|
|
return &jsonRawMessageCodec{}, nil
|
|
|
|
}
|
2017-06-19 17:10:20 +02:00
|
|
|
if typ.AssignableTo(jsoniterRawMessageType) {
|
|
|
|
return &jsoniterRawMessageCodec{}, nil
|
|
|
|
}
|
2017-06-02 09:43:58 +02:00
|
|
|
if typ.AssignableTo(jsonNumberType) {
|
2017-06-02 04:21:43 +02:00
|
|
|
return &jsonNumberCodec{}, nil
|
|
|
|
}
|
2017-05-24 08:34:00 +02:00
|
|
|
if typ.ConvertibleTo(marshalerType) {
|
|
|
|
templateInterface := reflect.New(typ).Elem().Interface()
|
2017-06-20 01:59:45 +02:00
|
|
|
var encoder ValEncoder = &marshalerEncoder{extractInterface(templateInterface)}
|
2017-06-20 01:23:22 +02:00
|
|
|
if typ.Kind() != reflect.Struct {
|
|
|
|
encoder = &optionalEncoder{encoder}
|
|
|
|
}
|
|
|
|
return encoder, nil
|
2017-05-24 08:34:00 +02:00
|
|
|
}
|
2017-06-02 04:50:23 +02:00
|
|
|
if typ.ConvertibleTo(anyType) {
|
|
|
|
return &anyCodec{}, nil
|
|
|
|
}
|
2017-05-26 18:36:21 +02:00
|
|
|
kind := typ.Kind()
|
|
|
|
switch kind {
|
2017-01-09 11:47:21 +02:00
|
|
|
case reflect.String:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "string" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*string)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 11:47:21 +02:00
|
|
|
return &stringCodec{}, nil
|
|
|
|
case reflect.Int:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*int)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 11:47:21 +02:00
|
|
|
return &intCodec{}, nil
|
2017-01-09 13:19:48 +02:00
|
|
|
case reflect.Int8:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int8" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*int8)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &int8Codec{}, nil
|
|
|
|
case reflect.Int16:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int16" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*int16)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &int16Codec{}, nil
|
|
|
|
case reflect.Int32:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int32" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*int32)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &int32Codec{}, nil
|
|
|
|
case reflect.Int64:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "int64" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*int64)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &int64Codec{}, nil
|
|
|
|
case reflect.Uint:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "uint" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*uint)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uintCodec{}, nil
|
|
|
|
case reflect.Uint8:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "uint8" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*uint8)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uint8Codec{}, nil
|
|
|
|
case reflect.Uint16:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "uint16" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*uint16)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uint16Codec{}, nil
|
|
|
|
case reflect.Uint32:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "uint32" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*uint32)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uint32Codec{}, nil
|
|
|
|
case reflect.Uint64:
|
2017-06-26 04:02:45 +02:00
|
|
|
if typeName != "uint64" {
|
2017-06-26 03:57:45 +02:00
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*uint64)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &uint64Codec{}, nil
|
|
|
|
case reflect.Float32:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "float32" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*float32)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &float32Codec{}, nil
|
|
|
|
case reflect.Float64:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "float64" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*float64)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &float64Codec{}, nil
|
|
|
|
case reflect.Bool:
|
2017-06-26 03:57:45 +02:00
|
|
|
if typeName != "bool" {
|
|
|
|
return encoderOfType(cfg, reflect.TypeOf((*bool)(nil)).Elem())
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
return &boolCodec{}, nil
|
2017-01-09 15:00:01 +02:00
|
|
|
case reflect.Interface:
|
2017-05-26 18:36:21 +02:00
|
|
|
if typ.NumMethod() == 0 {
|
|
|
|
return &emptyInterfaceCodec{}, nil
|
|
|
|
} else {
|
|
|
|
return &nonEmptyInterfaceCodec{}, nil
|
|
|
|
}
|
2017-01-09 13:19:48 +02:00
|
|
|
case reflect.Struct:
|
2017-06-26 03:57:45 +02:00
|
|
|
return prefix(fmt.Sprintf("[%s]", typeName)).addToEncoder(encoderOfStruct(cfg, typ))
|
2017-06-19 17:43:28 +02:00
|
|
|
case reflect.Array:
|
|
|
|
return prefix("[array]").addToEncoder(encoderOfArray(cfg, typ))
|
2017-01-09 13:48:57 +02:00
|
|
|
case reflect.Slice:
|
2017-06-13 10:58:53 +02:00
|
|
|
return prefix("[slice]").addToEncoder(encoderOfSlice(cfg, typ))
|
2017-01-09 14:51:09 +02:00
|
|
|
case reflect.Map:
|
2017-06-13 10:58:53 +02:00
|
|
|
return prefix("[map]").addToEncoder(encoderOfMap(cfg, typ))
|
2017-01-09 14:51:09 +02:00
|
|
|
case reflect.Ptr:
|
2017-06-13 10:58:53 +02:00
|
|
|
return prefix("[optional]").addToEncoder(encoderOfOptional(cfg, typ))
|
2017-01-09 11:47:21 +02:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unsupported type: %v", typ)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func decoderOfOptional(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
2017-01-09 14:51:09 +02:00
|
|
|
elemType := typ.Elem()
|
2017-06-13 10:58:53 +02:00
|
|
|
decoder, err := decoderOfType(cfg, elemType)
|
2016-12-04 15:19:54 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-12-04 05:06:38 +02:00
|
|
|
}
|
2017-01-09 14:51:09 +02:00
|
|
|
return &optionalDecoder{elemType, decoder}, nil
|
|
|
|
}
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func encoderOfOptional(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
2017-01-09 14:51:09 +02:00
|
|
|
elemType := typ.Elem()
|
2017-06-13 10:58:53 +02:00
|
|
|
elemEncoder, err := encoderOfType(cfg, elemType)
|
2017-01-09 14:51:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-13 03:14:19 +02:00
|
|
|
encoder := &optionalEncoder{elemEncoder}
|
|
|
|
if elemType.Kind() == reflect.Map {
|
|
|
|
encoder = &optionalEncoder{encoder}
|
|
|
|
}
|
|
|
|
return encoder, nil
|
2016-12-04 05:06:38 +02:00
|
|
|
}
|
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func decoderOfMap(cfg *frozenConfig, typ reflect.Type) (ValDecoder, error) {
|
2017-06-13 10:58:53 +02:00
|
|
|
decoder, err := decoderOfType(cfg, typ.Elem())
|
2016-12-10 18:38:07 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-05 15:23:08 +02:00
|
|
|
mapInterface := reflect.New(typ).Interface()
|
2017-06-05 17:53:48 +02:00
|
|
|
return &mapDecoder{typ, typ.Key(), typ.Elem(), decoder, extractInterface(mapInterface)}, nil
|
2017-05-24 08:34:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func extractInterface(val interface{}) emptyInterface {
|
|
|
|
return *((*emptyInterface)(unsafe.Pointer(&val)))
|
2016-12-10 18:38:07 +02:00
|
|
|
}
|
2017-01-09 14:51:09 +02:00
|
|
|
|
2017-06-20 01:59:45 +02:00
|
|
|
func encoderOfMap(cfg *frozenConfig, typ reflect.Type) (ValEncoder, error) {
|
2017-01-25 18:25:17 +02:00
|
|
|
elemType := typ.Elem()
|
2017-06-13 10:58:53 +02:00
|
|
|
encoder, err := encoderOfType(cfg, elemType)
|
2017-01-09 14:51:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
mapInterface := reflect.New(typ).Elem().Interface()
|
2017-06-16 10:46:30 +02:00
|
|
|
if cfg.sortMapKeys {
|
|
|
|
return &sortKeysMapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
|
|
|
} else {
|
|
|
|
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
|
|
|
|
}
|
2017-01-09 14:51:09 +02:00
|
|
|
}
|