diff --git a/feature_reflect.go b/feature_reflect.go index 48b8dfa..e7309d7 100644 --- a/feature_reflect.go +++ b/feature_reflect.go @@ -391,7 +391,7 @@ func createEncoderOfType(cfg *frozenConfig, typ reflect.Type) (Encoder, error) { } if typ.ConvertibleTo(marshalerType) { templateInterface := reflect.New(typ).Elem().Interface() - return &marshalerEncoder{extractInterface(templateInterface)}, nil + return &optionalEncoder{&marshalerEncoder{extractInterface(templateInterface)}}, nil } if typ.ConvertibleTo(anyType) { return &anyCodec{}, nil diff --git a/jsoniter_map_test.go b/jsoniter_map_test.go index 794bcb1..f05191f 100644 --- a/jsoniter_map_test.go +++ b/jsoniter_map_test.go @@ -120,3 +120,23 @@ func Test_encode_map_with_sorted_keys(t *testing.T) { should.Nil(err) should.Equal(string(bytes), output) } + +func Test_decode_map_of_raw_message(t *testing.T) { + should := require.New(t) + type RawMap map[string]*json.RawMessage + b := []byte("{\"test\":[{\"key\":\"value\"}]}") + var rawMap RawMap + should.Nil(Unmarshal(b, &rawMap)) + should.Equal(`[{"key":"value"}]`, string(*rawMap["test"])) +} + +func Test_encode_map_of_raw_message(t *testing.T) { + should := require.New(t) + type RawMap map[string]*json.RawMessage + value := json.RawMessage("[]") + rawMap := RawMap{"hello": &value} + output, err := MarshalToString(rawMap) + should.Nil(err) + should.Equal(`{"hello":[]}`, output) +} +