-
Notifications
You must be signed in to change notification settings - Fork 17.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
proposal: encoding/json.Encoder: add ZeroizeNilSlice
option
#61368
Comments
This comment was marked as off-topic.
This comment was marked as off-topic.
Searching for "encoding/json" in the title with the "proposal" tag narrows it down a good bit: https://github.com/golang/go/issues?q=is%3Aissue+%22encoding%2Fjson%22+in%3Atitle+label%3Aproposal+sort%3Areactions-%2B1-desc |
I'm going to close this as a duplicate, since there isn't any new information in this thread compared to the older ones. |
Hm, well, I don't think so. The two issues arp242 mentioned is about struct tags, but mines is about Encoder options. |
Sure, I'll reopen. Just note that the proposals seem identical other than the method for opting into the behavior. That's probably one of the most important design characteristics, but ideally we wouldn't have multiple threads due to it. |
ZeroizeNilSlice
optionZeroizeNilSlice
option
What does it do
Use
(*json.Encoder).SetZeroizeNilSlice(true)
can let the encoder encode nil slice as an empty json array[]
instead ofnull
Why
If we have a nil slive
[]Type(nil)
, the encoder will encode it asnull
. Then the consumers will having trouble with it, they must to check if the field is null first.A way to solve this problem is always alloc a zero length slice before encoding. However, I want my code be simplify but not fill with
make([]Type, 0)
every where.Additional Information
The text was updated successfully, but these errors were encountered: