Skip to content

Commit 028e2ef

Browse files
committed
Fixes #405 - Encode nil map into null
1 parent 27518f6 commit 028e2ef

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

misc_tests/jsoniter_map_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,11 @@ func Test_map_eface_of_eface(t *testing.T) {
4242
should.NoError(err)
4343
should.Equal(`{"1":2,"3":"4"}`, output)
4444
}
45+
46+
func Test_encode_nil_map(t *testing.T) {
47+
should := require.New(t)
48+
var nilMap map[string]string
49+
output, err := jsoniter.MarshalToString(nilMap)
50+
should.NoError(err)
51+
should.Equal(`null`, output)
52+
}

reflect_map.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,10 @@ type mapEncoder struct {
249249
}
250250

251251
func (encoder *mapEncoder) Encode(ptr unsafe.Pointer, stream *Stream) {
252+
if *(*unsafe.Pointer)(ptr) == nil {
253+
stream.WriteNil()
254+
return
255+
}
252256
stream.WriteObjectStart()
253257
iter := encoder.mapType.UnsafeIterate(ptr)
254258
for i := 0; iter.HasNext(); i++ {

0 commit comments

Comments
 (0)