Sfoglia il codice sorgente

#16 fix slice of map

Tao Wen 8 anni fa
parent
commit
e5476f70e7
2 ha cambiato i file con 14 aggiunte e 0 eliminazioni
  1. 3 0
      feature_reflect_array.go
  2. 11 0
      jsoniter_map_test.go

+ 3 - 0
feature_reflect_array.go

@@ -20,6 +20,9 @@ func encoderOfSlice(typ reflect.Type) (Encoder, error) {
 	if err != nil {
 		return nil, err
 	}
+	if typ.Elem().Kind() == reflect.Map {
+		encoder = &optionalEncoder{typ.Elem(), encoder}
+	}
 	return &sliceEncoder{typ, typ.Elem(), encoder}, nil
 }
 

+ 11 - 0
jsoniter_map_test.go

@@ -56,3 +56,14 @@ func Test_write_val_map(t *testing.T) {
 	should.Nil(err)
 	should.Equal(`{"1":"2"}`, str)
 }
+
+func Test_slice_of_map(t *testing.T) {
+	should := require.New(t)
+	val := []map[string]string{{"1": "2"}}
+	str, err := MarshalToString(val)
+	should.Nil(err)
+	should.Equal(`[{"1":"2"}]`, str)
+	val = []map[string]string{}
+	should.Nil(UnmarshalFromString(str, &val))
+	should.Equal("2", val[0]["1"])
+}