mirror of
https://github.com/json-iterator/go.git
synced 2025-04-20 11:28:49 +02:00
Merge pull request #406 from bbrks/fix_nil_map_encoding
Fixes #405 - Encode nil map into null
This commit is contained in:
commit
695ec2b83b
@ -42,3 +42,11 @@ func Test_map_eface_of_eface(t *testing.T) {
|
|||||||
should.NoError(err)
|
should.NoError(err)
|
||||||
should.Equal(`{"1":2,"3":"4"}`, output)
|
should.Equal(`{"1":2,"3":"4"}`, output)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func Test_encode_nil_map(t *testing.T) {
|
||||||
|
should := require.New(t)
|
||||||
|
var nilMap map[string]string
|
||||||
|
output, err := jsoniter.MarshalToString(nilMap)
|
||||||
|
should.NoError(err)
|
||||||
|
should.Equal(`null`, output)
|
||||||
|
}
|
||||||
|
@ -249,6 +249,10 @@ type mapEncoder struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
|
||||||
|
if *(*unsafe.Pointer)(ptr) == nil {
|
||||||
|
stream.WriteNil()
|
||||||
|
return
|
||||||
|
}
|
||||||
stream.WriteObjectStart()
|
stream.WriteObjectStart()
|
||||||
iter := encoder.mapType.UnsafeIterate(ptr)
|
iter := encoder.mapType.UnsafeIterate(ptr)
|
||||||
for i := 0; iter.HasNext(); i++ {
|
for i := 0; iter.HasNext(); i++ {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user