瀏覽代碼

#126 add space between map key and value when MarshalIndent

Tao Wen 8 年之前
父節點
當前提交
d37197e176
共有 2 個文件被更改,包括 14 次插入0 次删除
  1. 3 0
      feature_reflect_map.go
  2. 11 0
      jsoniter_adapter_test.go

+ 3 - 0
feature_reflect_map.go

@@ -102,6 +102,9 @@ func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
 		}
 		encodeMapKey(key, stream)
 		stream.writeByte(':')
+		if stream.indention > 0 {
+			stream.writeByte(' ')
+		}
 		val := realVal.MapIndex(key).Interface()
 		encoder.elemEncoder.EncodeInterface(val, stream)
 	}

+ 11 - 0
jsoniter_adapter_test.go

@@ -68,3 +68,14 @@ func Test_marshal_indent(t *testing.T) {
 	should.Nil(err)
 	should.Equal("{\n  \"F1\": 1,\n  \"F2\": [\n    2,\n    3,\n    4\n  ]\n}", string(output))
 }
+
+func Test_marshal_indent_map(t *testing.T) {
+	should := require.New(t)
+	obj := map[int]int{1: 2}
+	output, err := json.MarshalIndent(obj, "", "  ")
+	should.Nil(err)
+	should.Equal("{\n  \"1\": 2\n}", string(output))
+	output, err = MarshalIndent(obj, "", "  ")
+	should.Nil(err)
+	should.Equal("{\n  \"1\": 2\n}", string(output))
+}