1
0
mirror of https://github.com/json-iterator/go.git synced 2024-11-27 08:30:57 +02:00

#126 add space between map key and value when MarshalIndent

This commit is contained in:
Tao Wen 2017-07-10 22:14:11 +08:00
parent 45c22b130b
commit d37197e176
2 changed files with 14 additions and 0 deletions

View File

@ -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)
}

View File

@ -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))
}