1
0
mirror of https://github.com/json-iterator/go.git synced 2025-07-03 23:30:41 +02:00

Fix encoding 0-length arrays

The array encoder assumed that arrays had at least one value, so it
would serialize them with a zero-value for the array, such as `[0]`.

This adds a test to reproduce the issue, and updates the encoder to
write an empty array if the length is 0.
This commit is contained in:
Matt Good
2018-01-16 11:02:03 -08:00
parent c3ed5e85e0
commit ba3857729b
2 changed files with 13 additions and 0 deletions

View File

@ -27,6 +27,10 @@ type arrayEncoder struct {
}
func (encoder *arrayEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
if encoder.arrayType.Len() == 0 {
stream.WriteEmptyArray()
return
}
stream.WriteArrayStart()
elemPtr := unsafe.Pointer(ptr)
encoder.elemEncoder.Encode(elemPtr, stream)