1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-24 08:22:14 +02:00
json-iterator/reflect.go

333 lines
8.4 KiB
Go
Raw Normal View History

2016-12-04 05:06:38 +02:00
package jsoniter
import (
2016-12-04 06:27:34 +02:00
"fmt"
"reflect"
2017-06-20 01:39:54 +02:00
"unsafe"
"github.com/modern-go/reflect2"
2016-12-04 05:06:38 +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
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
// 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).
type ValEncoder interface {
2017-06-20 09:11:01 +02:00
IsEmpty(ptr unsafe.Pointer) bool
Encode(ptr unsafe.Pointer, stream *Stream)
2017-01-25 18:25:17 +02:00
}
type checkIsEmpty interface {
IsEmpty(ptr unsafe.Pointer) bool
}
2018-02-21 11:59:41 +02:00
type ctx struct {
*frozenConfig
prefix string
2018-02-22 04:12:08 +02:00
encoders map[reflect2.Type]ValEncoder
decoders map[reflect2.Type]ValDecoder
2018-02-21 11:59:41 +02:00
}
func (b *ctx) caseSensitive() bool {
if b.frozenConfig == nil {
// default is case-insensitive
return false
}
return b.frozenConfig.caseSensitive
}
2018-02-21 11:59:41 +02:00
func (b *ctx) append(prefix string) *ctx {
return &ctx{
frozenConfig: b.frozenConfig,
prefix: b.prefix + " " + prefix,
encoders: b.encoders,
decoders: b.decoders,
}
}
2017-07-09 10:09:23 +02:00
// ReadVal copy the underlying JSON into go interface, same as json.Unmarshal
2017-01-09 11:47:21 +02:00
func (iter *Iterator) ReadVal(obj interface{}) {
2018-02-22 04:18:27 +02:00
cacheKey := reflect2.RTypeOf(obj)
decoder := iter.cfg.getDecoderFromCache(cacheKey)
if decoder == nil {
typ := reflect2.TypeOf(obj)
if typ.Kind() != reflect.Ptr {
iter.ReportError("ReadVal", "can only unmarshal into pointer")
return
}
decoder = iter.cfg.DecoderOf(typ)
}
ptr := reflect2.PtrOf(obj)
if ptr == nil {
iter.ReportError("ReadVal", "can not read into nil pointer")
return
}
decoder.Decode(ptr, iter)
2016-12-04 06:27:34 +02:00
}
2017-07-09 10:09:23 +02:00
// WriteVal copy the go interface into underlying JSON, same as json.Marshal
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
}
2018-02-22 04:20:19 +02:00
cacheKey := reflect2.RTypeOf(val)
encoder := stream.cfg.getEncoderFromCache(cacheKey)
if encoder == nil {
typ := reflect2.TypeOf(val)
encoder = stream.cfg.EncoderOf(typ)
}
2018-02-18 16:57:01 +02:00
encoder.Encode(reflect2.PtrOf(val), stream)
2017-01-09 11:47:21 +02:00
}
2018-02-22 04:12:08 +02:00
func (cfg *frozenConfig) DecoderOf(typ reflect2.Type) ValDecoder {
cacheKey := typ.RType()
decoder := cfg.getDecoderFromCache(cacheKey)
if decoder != nil {
return decoder
2017-05-05 10:51:05 +02:00
}
2018-02-21 11:59:41 +02:00
ctx := &ctx{
frozenConfig: cfg,
prefix: "",
2018-02-22 04:12:08 +02:00
decoders: map[reflect2.Type]ValDecoder{},
encoders: map[reflect2.Type]ValEncoder{},
2018-02-21 11:59:41 +02:00
}
2018-02-22 04:12:08 +02:00
ptrType := typ.(*reflect2.UnsafePtrType)
decoder = decoderOfType(ctx, ptrType.Elem())
2018-02-19 16:53:42 +02:00
cfg.addDecoderToCache(cacheKey, decoder)
return decoder
}
2018-02-22 04:12:08 +02:00
func decoderOfType(ctx *ctx, typ reflect2.Type) ValDecoder {
2018-02-21 11:59:41 +02:00
decoder := getTypeDecoderFromExtension(ctx, typ)
if decoder != nil {
return decoder
}
2018-02-21 11:59:41 +02:00
decoder = createDecoderOfType(ctx, typ)
2017-06-20 10:36:22 +02:00
for _, extension := range extensions {
decoder = extension.DecorateDecoder(typ, decoder)
}
2018-07-22 05:51:51 +02:00
decoder = ctx.decoderExtension.DecorateDecoder(typ, decoder)
for _, extension := range ctx.extraExtensions {
2017-11-22 18:09:35 +02:00
decoder = extension.DecorateDecoder(typ, decoder)
}
return decoder
2017-05-05 10:51:05 +02:00
}
2018-02-22 04:12:08 +02:00
func createDecoderOfType(ctx *ctx, typ reflect2.Type) ValDecoder {
2018-02-21 11:59:41 +02:00
decoder := ctx.decoders[typ]
2018-02-20 16:38:35 +02:00
if decoder != nil {
return decoder
2017-06-19 17:10:20 +02:00
}
2018-02-21 11:59:41 +02:00
placeholder := &placeholderDecoder{}
ctx.decoders[typ] = placeholder
decoder = _createDecoderOfType(ctx, typ)
placeholder.decoder = decoder
return decoder
}
2018-02-22 04:12:08 +02:00
func _createDecoderOfType(ctx *ctx, typ reflect2.Type) ValDecoder {
2018-02-21 11:59:41 +02:00
decoder := createDecoderOfJsonRawMessage(ctx, typ)
if decoder != nil {
return decoder
}
decoder = createDecoderOfJsonNumber(ctx, typ)
2018-02-19 17:13:33 +02:00
if decoder != nil {
return decoder
}
2018-02-21 11:59:41 +02:00
decoder = createDecoderOfMarshaler(ctx, typ)
2018-02-19 16:53:42 +02:00
if decoder != nil {
return decoder
2017-06-28 18:14:55 +02:00
}
2018-02-21 11:59:41 +02:00
decoder = createDecoderOfAny(ctx, typ)
2018-02-20 17:04:04 +02:00
if decoder != nil {
return decoder
2017-06-02 04:50:23 +02:00
}
2018-02-21 11:59:41 +02:00
decoder = createDecoderOfNative(ctx, typ)
2018-02-20 16:55:31 +02:00
if decoder != nil {
return decoder
}
switch typ.Kind() {
2016-12-11 04:04:26 +02:00
case reflect.Interface:
2018-02-22 04:12:08 +02:00
ifaceType, isIFace := typ.(*reflect2.UnsafeIFaceType)
if isIFace {
return &ifaceDecoder{valType: ifaceType}
2017-01-25 17:25:25 +02:00
}
return &efaceDecoder{}
2016-12-04 06:27:34 +02:00
case reflect.Struct:
2018-02-21 11:59:41 +02:00
return decoderOfStruct(ctx, typ)
2017-06-19 17:43:28 +02:00
case reflect.Array:
2018-02-21 11:59:41 +02:00
return decoderOfArray(ctx, typ)
2016-12-04 07:13:38 +02:00
case reflect.Slice:
2018-02-21 11:59:41 +02:00
return decoderOfSlice(ctx, typ)
2016-12-10 18:38:07 +02:00
case reflect.Map:
2018-02-21 11:59:41 +02:00
return decoderOfMap(ctx, typ)
2016-12-04 07:13:38 +02:00
case reflect.Ptr:
2018-02-21 11:59:41 +02:00
return decoderOfOptional(ctx, typ)
2016-12-04 07:13:38 +02:00
default:
2018-02-21 11:59:41 +02:00
return &lazyErrorDecoder{err: fmt.Errorf("%s%s is unsupported type", ctx.prefix, typ.String())}
2016-12-04 07:13:38 +02:00
}
}
2018-02-22 04:12:08 +02:00
func (cfg *frozenConfig) EncoderOf(typ reflect2.Type) ValEncoder {
cacheKey := typ.RType()
encoder := cfg.getEncoderFromCache(cacheKey)
if encoder != nil {
return encoder
2017-05-05 10:51:05 +02:00
}
2018-02-21 11:59:41 +02:00
ctx := &ctx{
frozenConfig: cfg,
prefix: "",
2018-02-22 04:12:08 +02:00
decoders: map[reflect2.Type]ValDecoder{},
encoders: map[reflect2.Type]ValEncoder{},
2018-02-21 11:59:41 +02:00
}
encoder = encoderOfType(ctx, typ)
2018-02-22 04:12:08 +02:00
if typ.LikePtr() {
2018-02-14 09:04:23 +02:00
encoder = &onePtrEncoder{encoder}
}
cfg.addEncoderToCache(cacheKey, encoder)
return encoder
}
type onePtrEncoder struct {
encoder ValEncoder
}
func (encoder *onePtrEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return encoder.encoder.IsEmpty(unsafe.Pointer(&ptr))
}
func (encoder *onePtrEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
encoder.encoder.Encode(unsafe.Pointer(&ptr), stream)
}
2018-02-22 04:12:08 +02:00
func encoderOfType(ctx *ctx, typ reflect2.Type) ValEncoder {
2018-02-21 11:59:41 +02:00
encoder := getTypeEncoderFromExtension(ctx, typ)
if encoder != nil {
return encoder
}
2018-02-21 11:59:41 +02:00
encoder = createEncoderOfType(ctx, typ)
2017-06-20 10:36:22 +02:00
for _, extension := range extensions {
encoder = extension.DecorateEncoder(typ, encoder)
}
2018-07-22 05:51:51 +02:00
encoder = ctx.encoderExtension.DecorateEncoder(typ, encoder)
for _, extension := range ctx.extraExtensions {
2017-11-22 18:09:35 +02:00
encoder = extension.DecorateEncoder(typ, encoder)
}
return encoder
2017-05-05 10:51:05 +02:00
}
2018-02-22 04:12:08 +02:00
func createEncoderOfType(ctx *ctx, typ reflect2.Type) ValEncoder {
2018-02-21 11:59:41 +02:00
encoder := ctx.encoders[typ]
2018-02-20 16:38:35 +02:00
if encoder != nil {
return encoder
2017-06-19 17:10:20 +02:00
}
2018-02-21 11:59:41 +02:00
placeholder := &placeholderEncoder{}
ctx.encoders[typ] = placeholder
encoder = _createEncoderOfType(ctx, typ)
placeholder.encoder = encoder
return encoder
}
2018-02-22 04:12:08 +02:00
func _createEncoderOfType(ctx *ctx, typ reflect2.Type) ValEncoder {
2018-02-21 11:59:41 +02:00
encoder := createEncoderOfJsonRawMessage(ctx, typ)
2018-02-19 17:13:33 +02:00
if encoder != nil {
return encoder
}
2018-02-21 11:59:41 +02:00
encoder = createEncoderOfJsonNumber(ctx, typ)
2018-02-19 08:30:01 +02:00
if encoder != nil {
return encoder
2017-06-28 18:14:55 +02:00
}
2018-02-21 11:59:41 +02:00
encoder = createEncoderOfMarshaler(ctx, typ)
2018-02-20 17:04:04 +02:00
if encoder != nil {
return encoder
2018-02-18 16:57:01 +02:00
}
2018-02-21 11:59:41 +02:00
encoder = createEncoderOfAny(ctx, typ)
if encoder != nil {
return encoder
}
encoder = createEncoderOfNative(ctx, typ)
2018-02-20 17:04:04 +02:00
if encoder != nil {
return encoder
}
kind := typ.Kind()
switch kind {
case reflect.Interface:
2018-02-22 04:12:08 +02:00
return &dynamicEncoder{typ}
2018-02-20 17:04:04 +02:00
case reflect.Struct:
2018-02-21 11:59:41 +02:00
return encoderOfStruct(ctx, typ)
2018-02-20 17:04:04 +02:00
case reflect.Array:
2018-02-21 11:59:41 +02:00
return encoderOfArray(ctx, typ)
2018-02-20 17:04:04 +02:00
case reflect.Slice:
2018-02-21 11:59:41 +02:00
return encoderOfSlice(ctx, typ)
2018-02-20 17:04:04 +02:00
case reflect.Map:
2018-02-21 11:59:41 +02:00
return encoderOfMap(ctx, typ)
2018-02-20 17:04:04 +02:00
case reflect.Ptr:
2018-02-21 11:59:41 +02:00
return encoderOfOptional(ctx, typ)
2018-02-20 17:04:04 +02:00
default:
2018-02-21 11:59:41 +02:00
return &lazyErrorEncoder{err: fmt.Errorf("%s%s is unsupported type", ctx.prefix, typ.String())}
2017-06-02 04:50:23 +02:00
}
}
type lazyErrorDecoder struct {
err error
}
func (decoder *lazyErrorDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
if iter.WhatIsNext() != NilValue {
if iter.Error == nil {
iter.Error = decoder.err
}
} else {
iter.Skip()
2017-01-09 14:51:09 +02:00
}
}
type lazyErrorEncoder struct {
err error
}
func (encoder *lazyErrorEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
if ptr == nil {
stream.WriteNil()
} else if stream.Error == nil {
stream.Error = encoder.err
}
2016-12-04 05:06:38 +02:00
}
func (encoder *lazyErrorEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return false
2017-05-24 08:34:00 +02:00
}
2018-02-21 11:59:41 +02:00
type placeholderDecoder struct {
decoder ValDecoder
}
func (decoder *placeholderDecoder) Decode(ptr unsafe.Pointer, iter *Iterator) {
decoder.decoder.Decode(ptr, iter)
}
type placeholderEncoder struct {
encoder ValEncoder
}
func (encoder *placeholderEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
encoder.encoder.Encode(ptr, stream)
}
func (encoder *placeholderEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return encoder.encoder.IsEmpty(ptr)
}