Skip to content

Incorrect marshalling of nil map #405

@bbrks

Description

@bbrks

Related to #56 - but it seems like the fix has had a regression since then? I can't find the unit test that was included as part of the fix either.

jsoniter marshals a nil map as {} instead of null. Empty maps are correctly marshalled as {}. This also affects nil maps inside structs.

var nilMap map[string]string
data, _ := jsoniter.Marshal(nilMap)
fmt.Printf("%s\n", string(data))
// {}

Interestingly, if you enable SortMapKeys, the problem goes away, but I don't want the performance cost of sorting the map keys just to fix this issue.

I have an example below showing the difference between encoding/json, the default jsoniter config, and a SortMapKeys:true config:

https://play.golang.org/p/bMKiBk1wptz

Which produces this result:

encoding/json:        null
jsoniter default:     {}
jsoniter sortMapKeys: null

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions