Permalink
Browse files

test array of interface

  • Loading branch information...
1 parent 1f0a0e9 commit a57c8c620260e72775debaf29514fc2b9e7fe513 @taowen taowen committed Jan 9, 2017
Showing with 23 additions and 3 deletions.
  1. +3 −1 feature_reflect.go
  2. +6 −2 feature_reflect_native.go
  3. +14 −0 jsoniter_interface_test.go
View
@@ -398,7 +398,7 @@ func decoderOfType(typ reflect.Type) (Decoder, error) {
case reflect.Bool:
return &boolCodec{}, nil
case reflect.Interface:
- return &interfaceDecoder{}, nil
+ return &interfaceCodec{}, nil
case reflect.Struct:
return prefix(fmt.Sprintf("[%s]", typeName)).addToDecoder(decoderOfStruct(typ))
case reflect.Slice:
@@ -443,6 +443,8 @@ func encoderOfType(typ reflect.Type) (Encoder, error) {
return &float64Codec{}, nil
case reflect.Bool:
return &boolCodec{}, nil
+ case reflect.Interface:
+ return &interfaceCodec{}, nil
case reflect.Struct:
return prefix(fmt.Sprintf("[%s]", typeName)).addToEncoder(encoderOfStruct(typ))
case reflect.Slice:
@@ -156,13 +156,17 @@ func (codec *boolCodec) encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteBool(*((*bool)(ptr)))
}
-type interfaceDecoder struct {
+type interfaceCodec struct {
}
-func (decoder *interfaceDecoder) decode(ptr unsafe.Pointer, iter *Iterator) {
+func (codec *interfaceCodec) decode(ptr unsafe.Pointer, iter *Iterator) {
*((*interface{})(ptr)) = iter.ReadAny().Get()
}
+func (codec *interfaceCodec) encode(ptr unsafe.Pointer, stream *Stream) {
+ stream.WriteVal(*((*interface{})(ptr)))
+}
+
type anyDecoder struct {
}
@@ -0,0 +1,14 @@
+package jsoniter
+
+import (
+ "testing"
+ "github.com/json-iterator/go/require"
+)
+
+func Test_write_array_of_interface(t *testing.T) {
+ should := require.New(t)
+ array := []interface{}{"hello"}
+ str, err := MarshalToString(array)
+ should.Nil(err)
+ should.Equal(`["hello"]`, str)
+}

0 comments on commit a57c8c6

Please sign in to comment.