1
0
mirror of https://github.com/json-iterator/go.git synced 2025-01-05 12:50:34 +02:00

#126 fix space in case map key is sorted

This commit is contained in:
Tao Wen 2017-07-11 01:07:18 +08:00
parent d37197e176
commit 845d8438db
2 changed files with 11 additions and 3 deletions

View File

@ -101,9 +101,10 @@ func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteMore()
}
encodeMapKey(key, stream)
stream.writeByte(':')
if stream.indention > 0 {
stream.writeByte(' ')
stream.writeTwoBytes(byte(':'), byte(' '))
} else {
stream.writeByte(':')
}
val := realVal.MapIndex(key).Interface()
encoder.elemEncoder.EncodeInterface(val, stream)
@ -185,7 +186,11 @@ func (encoder *sortKeysMapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
stream.WriteMore()
}
stream.WriteVal(key.s) // might need html escape, so can not WriteString directly
stream.writeByte(':')
if stream.indention > 0 {
stream.writeTwoBytes(byte(':'), byte(' '))
} else {
stream.writeByte(':')
}
val := realVal.MapIndex(key.v).Interface()
encoder.elemEncoder.EncodeInterface(val, stream)
}

View File

@ -78,4 +78,7 @@ func Test_marshal_indent_map(t *testing.T) {
output, err = MarshalIndent(obj, "", " ")
should.Nil(err)
should.Equal("{\n \"1\": 2\n}", string(output))
output, err = ConfigCompatibleWithStandardLibrary.MarshalIndent(obj, "", " ")
should.Nil(err)
should.Equal("{\n \"1\": 2\n}", string(output))
}