Description
There have been many reports about encoding a nil slice or map as [ ]
or { }
respectively.
See: #2278
Currently it is encoded as null
. This is a source of bugs in many non-theoretical cases (eg consumers of JSON APIs). There are many issues on Google about others falling into this pitfall so it is at least not uncommon.
It would be nice if a struct tag can be used to signify that we intend the nil slice or map to be treated as an empty slice or map respectively for the purposes of marshalling.
Current solutions include: https://github.com/helloeave/json. This is not a nice solution because it requires the assistance of a third-party package.
This is how it's implemented in third party lib: homelight/json@7b6e442
My PR can be found here: #27813 (subject to an agreed struct tag name, which when decided, can be updated in the PR)