Skip to content
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

Open
zyxkad opened this issue Jul 14, 2023 · 6 comments
Open

proposal: encoding/json.Encoder: add ZeroizeNilSlice option #61368

zyxkad opened this issue Jul 14, 2023 · 6 comments
Labels
Milestone

Comments

@zyxkad
Copy link

zyxkad commented Jul 14, 2023

What does it do

Use (*json.Encoder).SetZeroizeNilSlice(true) can let the encoder encode nil slice as an empty json array [] instead of null

Why

If we have a nil slive []Type(nil), the encoder will encode it as null. 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

  1. This change should be completely backward compatible.
  2. The options name is not good, maybe have to decide a better name later.
@gopherbot gopherbot added this to the Proposal milestone Jul 14, 2023
@arp242
Copy link

arp242 commented Jul 14, 2023

Also: #27589, #37711, probably a few others

@zyxkad

This comment was marked as off-topic.

@arp242
Copy link

arp242 commented Jul 14, 2023

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

@mvdan
Copy link
Member

mvdan commented Jul 15, 2023

I'm going to close this as a duplicate, since there isn't any new information in this thread compared to the older ones.

@mvdan mvdan closed this as not planned Won't fix, can't repro, duplicate, stale Jul 15, 2023
@zyxkad
Copy link
Author

zyxkad commented Jul 15, 2023

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.
@mvdan

@mvdan
Copy link
Member

mvdan commented Jul 15, 2023

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.

@mvdan mvdan reopened this Jul 15, 2023
@ianlancetaylor ianlancetaylor changed the title proposal: encoding/json.Encoder: Add ZeroizeNilSlice option proposal: encoding/json.Encoder: add ZeroizeNilSlice option Mar 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

No branches or pull requests

4 participants