Browse Source

#63 support *json.RawMessage

Tao Wen 8 years ago
parent
commit
50583f6bae
2 changed files with 21 additions and 1 deletions
  1. 1 1
      feature_reflect.go
  2. 20 0
      jsoniter_map_test.go

+ 1 - 1
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

+ 20 - 0
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)
+}
+