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

remove mapInterfaceEncoder

This commit is contained in:
Tao Wen 2017-06-05 23:56:37 +08:00
parent af4982b22c
commit 5fd09f0e02
2 changed files with 3 additions and 52 deletions

View File

@ -274,7 +274,6 @@ func (iter *Iterator) ReadVal(obj interface{}) {
cachedDecoder.decode(e.word, iter)
}
func (stream *Stream) WriteVal(val interface{}) {
if nil == val {
stream.WriteNil()
@ -320,7 +319,7 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
if typ.Kind() == reflect.Ptr {
typeDecoder := typeDecoders[typ.Elem().String()]
if typeDecoder != nil {
return &optionalDecoder{typ.Elem(),typeDecoder}, nil
return &optionalDecoder{typ.Elem(), typeDecoder}, nil
}
}
cacheKey := typ
@ -501,7 +500,7 @@ func encoderOfOptional(typ reflect.Type) (Encoder, error) {
if err != nil {
return nil, err
}
return &optionalEncoder{ decoder}, nil
return &optionalEncoder{decoder}, nil
}
func decoderOfMap(typ reflect.Type) (Decoder, error) {
@ -524,9 +523,5 @@ func encoderOfMap(typ reflect.Type) (Encoder, error) {
return nil, err
}
mapInterface := reflect.New(typ).Elem().Interface()
if elemType.Kind() == reflect.Interface && elemType.NumMethod() == 0 {
return &mapInterfaceEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
} else {
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
}
return &mapEncoder{typ, elemType, encoder, *((*emptyInterface)(unsafe.Pointer(&mapInterface)))}, nil
}

View File

@ -130,49 +130,5 @@ func (encoder *mapEncoder) isEmpty(ptr unsafe.Pointer) bool {
mapInterface.word = ptr
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
realVal := reflect.ValueOf(*realInterface)
return realVal.Len() == 0
}
type mapInterfaceEncoder struct {
mapType reflect.Type
elemType reflect.Type
elemEncoder Encoder
mapInterface emptyInterface
}
func (encoder *mapInterfaceEncoder) encode(ptr unsafe.Pointer, stream *Stream) {
mapInterface := encoder.mapInterface
mapInterface.word = ptr
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
realVal := reflect.ValueOf(*realInterface)
stream.WriteObjectStart()
for i, key := range realVal.MapKeys() {
if i != 0 {
stream.WriteMore()
}
switch key.Interface().(type) {
case string:
stream.WriteObjectField(key.String())
default:
stream.Error = &json.UnsupportedTypeError{key.Type()}
return
}
val := realVal.MapIndex(key).Interface()
encoder.elemEncoder.encode(unsafe.Pointer(&val), stream)
}
stream.WriteObjectEnd()
}
func (encoder *mapInterfaceEncoder) encodeInterface(val interface{}, stream *Stream) {
writeToStream(val, stream, encoder)
}
func (encoder *mapInterfaceEncoder) isEmpty(ptr unsafe.Pointer) bool {
mapInterface := encoder.mapInterface
mapInterface.word = ptr
realInterface := (*interface{})(unsafe.Pointer(&mapInterface))
realVal := reflect.ValueOf(*realInterface)
return realVal.Len() == 0
}