mirror of
https://github.com/json-iterator/go.git
synced 2025-03-23 21:09:11 +02:00
#63 support *json.RawMessage
This commit is contained in:
parent
3b883aeffc
commit
50583f6bae
@ -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
|
||||
|
@ -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)
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user