diff --git a/feature_reflect.go b/feature_reflect.go index af4fcc5..2e07e0a 100644 --- a/feature_reflect.go +++ b/feature_reflect.go @@ -361,6 +361,9 @@ func (p prefix) addToEncoder(encoder Encoder, err error) (Encoder, error) { } func decoderOfType(typ reflect.Type) (Decoder, error) { + if typ.ConvertibleTo(anyType) { + return &anyCodec{}, nil + } typeName := typ.String() typeDecoder := typeDecoders[typeName] if typeDecoder != nil { diff --git a/jsoniter_demo_test.go b/jsoniter_demo_test.go index 8dcf37d..ad152dd 100644 --- a/jsoniter_demo_test.go +++ b/jsoniter_demo_test.go @@ -36,4 +36,17 @@ func Test_iterator_and_bind_api(t *testing.T) { iter.ReadVal(&user) iter.ReadArray() // array end fmt.Println(user) -} \ No newline at end of file +} + +type TaskBidLog struct { + age int +} + +func Test2(t *testing.T) { + rawString :=` + {"id":0,"bidId":"bid01492692440885","impId":"imp0","taskId":"1024","bidPrice":80,"winPrice":0,"isWon":0,"createTime":1492692440885,"updateTime":null,"device":"","age":30,"gender":"","location":"[中国, 山西, , ]","conType":"0","os":"iOS","osv":"","brand":"","geo":"","ip":"1.68.4.193","idfa":"","waxUserid":""}` + var log TaskBidLog + err := UnmarshalFromString(rawString, &log) + fmt.Println(err) + fmt.Println(log.age) +} diff --git a/jsoniter_reflect_struct_test.go b/jsoniter_reflect_struct_test.go index 78459f3..7c720c8 100644 --- a/jsoniter_reflect_struct_test.go +++ b/jsoniter_reflect_struct_test.go @@ -171,4 +171,17 @@ func Test_omit_empty(t *testing.T) { str, err := MarshalToString(&obj) should.Nil(err) should.Equal(`{"field-2":"hello"}`, str) +} + +func Test_any_within_struct(t *testing.T) { + should := require.New(t) + type TestObject struct { + Field1 Any + Field2 Any + } + obj := TestObject{} + err := UnmarshalFromString(`{"Field1": "hello", "Field2": [1,2,3]}`, &obj) + should.Nil(err) + should.Equal("hello", obj.Field1.ToString()) + should.Equal("[1,2,3]", obj.Field2.ToString()) } \ No newline at end of file