-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
encoding/json: Add "nonil" struct tag for json encoding of nil slices/maps #27813
base: master
Are you sure you want to change the base?
Conversation
Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). 📝 Please visit https://cla.developers.google.com/ to sign. Once you've signed (or fixed any issues), please reply here (e.g. What to do if you already signed the CLAIndividual signers
Corporate signers
|
I signed it! |
CLAs look good, thanks! |
This PR (HEAD: 40e395c) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/go/+/136761 to see it. Tip: You can toggle comments from me using the |
Message from Gerrit User 5976: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 12446: Uploaded patch set 2: Commit message was updated. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 12446: Uploaded patch set 3: Commit message was updated. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 9735: Patch Set 3: Code-Review-2 Hi, thank you for your submission. However, the proposal in #2278 would need to be accepted first because we start talking about concrete code changes. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 29344: Patch Set 3: It seems to me like this can still get merged whilst thinking about the prior proposal. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 9735: Patch Set 3:
This CL adds new API that the prior proposal is trying to decide whether to add or not. I'm not sure I follow the rationale in accepting the outcome before the decision. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 29344: Patch Set 3: Code-Review+1
#2278 seems to be about what the default behaviour should be. The issue is 7 years old so no-one will change the default behaviour anymore - not even for Go 2 - especially in light of Rob's comment. My pull-request is not about default behaviour. It's about giving another option that does not conflict with current behaviour.
Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 9735: Patch Set 3:
Specifying +1 on your own CL is unnecessary. The purpose of code review is for people other than yourself review the code and give their endorsement. The acceptance criterion for adding new API is not simply whether it is backwards compatible. Every feature added should compose well with the set of features that came before it, otherwise we run into increasing number of edge cases where the behavior is obscure, surprising, and worse, leads to brittle and buggy code. This implies that each additional feature carries additional compound cost. At the present there a number of encoding/json proposals and there needs to be a comprehensive review of all of them before adding new API ad-hoc. It may be that this feature actually does interact well with other proposed features, but that has yet to be determined. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
53bd915
to
6139019
Compare
4a7ed1f
to
0f992b9
Compare
This PR (HEAD: ab41add) has been imported to Gerrit for code review. Please visit https://go-review.googlesource.com/c/go/+/136761 to see it. Tip: You can toggle comments from me using the |
Message from Gerrit User 9735: Patch Set 3:
This CL adds new API that the prior proposal is trying to decide whether to add or not. I'm not sure I follow the rationale in accepting the outcome before the decision. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 29344: Patch Set 3: Code-Review+1
#2278 seems to be about what the default behaviour should be. The issue is 7 years old so no-one will change the default behaviour anymore - not even for Go 2 - especially in light of Rob's comment. My pull-request is not about default behaviour. It's about giving another option that does not conflict with current behaviour.
Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 9735: Patch Set 3:
Specifying +1 on your own CL is unnecessary. The purpose of code review is for people other than yourself review the code and give their endorsement. The acceptance criterion for adding new API is not simply whether it is backwards compatible. Every feature added should compose well with the set of features that came before it, otherwise we run into increasing number of edge cases where the behavior is obscure, surprising, and worse, leads to brittle and buggy code. This implies that each additional feature carries additional compound cost. At the present there a number of encoding/json proposals and there needs to be a comprehensive review of all of them before adding new API ad-hoc. It may be that this feature actually does interact well with other proposed features, but that has yet to be determined. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Why was this closed? |
I closed PRs older than a year to reduce load on the Gerrit importer. If you plan to continue work on it I'm happy to reopen, though I now realize that will create a new CL, sorry. |
Is there anything we can do to get this PR moving forward? Having to write custom MashalJSON methods on every single type that contains a slice introduces more code and API bloat than this tiny PR. This PR is honestly very clean, concise, alters no default behavior and would allow many many projects that interface with external frameworks to rip out a bunch of nasty custom encoders. |
Javascript really does not like to get a null when it expects an empty array, but that's what Go will send if your struct contains e.g. a []string and you don't explicitly initialize the slice. These custom marshallers make sure we send an empty array, making the frontend's life easier. Go desperately needs some way to tell the JSON encoder that we want this behavior, see: golang/go#37711 golang/go#27589 golang/go#27813
Message from Gerrit User 5976: Patch Set 1: Congratulations on opening your first change. Thank you for your contribution! Next steps: Most changes in the Go project go through a few rounds of revision. This can be During May-July and Nov-Jan the Go project is in a code freeze, during which Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 9735: Patch Set 3: Code-Review-2 Hi, thank you for your submission. However, the proposal in #2278 would need to be accepted first because we start talking about concrete code changes. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 29344: Patch Set 3: It seems to me like this can still get merged whilst thinking about the prior proposal. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 9735: Patch Set 3:
This CL adds new API that the prior proposal is trying to decide whether to add or not. I'm not sure I follow the rationale in accepting the outcome before the decision. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 29344: Patch Set 3: Code-Review+1
#2278 seems to be about what the default behaviour should be. The issue is 7 years old so no-one will change the default behaviour anymore - not even for Go 2 - especially in light of Rob's comment. My pull-request is not about default behaviour. It's about giving another option that does not conflict with current behaviour.
Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
Message from Gerrit User 9735: Patch Set 3:
Specifying +1 on your own CL is unnecessary. The purpose of code review is for people other than yourself review the code and give their endorsement. The acceptance criterion for adding new API is not simply whether it is backwards compatible. Every feature added should compose well with the set of features that came before it, otherwise we run into increasing number of edge cases where the behavior is obscure, surprising, and worse, leads to brittle and buggy code. This implies that each additional feature carries additional compound cost. At the present there a number of encoding/json proposals and there needs to be a comprehensive review of all of them before adding new API ad-hoc. It may be that this feature actually does interact well with other proposed features, but that has yet to be determined. Please don’t reply on this GitHub thread. Visit golang.org/cl/136761. |
add "nonil" struct tag for json marshaling slices/maps so that nil slices/maps get encoded as []/{} respectively.
Fixes #27589