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: Struct omitempty #58489

Closed
PumpkinSeed opened this issue Feb 13, 2023 · 2 comments
Closed

proposal: encoding/json: Struct omitempty #58489

PumpkinSeed opened this issue Feb 13, 2023 · 2 comments

Comments

@PumpkinSeed
Copy link

PumpkinSeed commented Feb 13, 2023

Problem

The current json package can't handle non-pointer struct encoding alongside with omitempty tag. This means we need to add the structs as pointers. Many of the proposals have been submitted. This is the main ticket dealing with this problem: #11939

I tried to go to an other direction in this proposal, because the major problem with the IsZero, that it's not backward compatible.

Possible solution

I think we need to use the encOpts located in the json/encode.go. By using it we can setup a solution which can handle this problem and just like the SetIndent, it can be called over the Encoder struct.

One possible example can be the SetOmitemptyOnIsZero(b bool), which omitting the value if the IsZero function of the given struct returns true. (Note, the naming conventions isn't good, it's just something for showing the idea.)

Implementation details: https://github.com/PumpkinSeed/go-json-proposal/pull/1/files

@gopherbot gopherbot added this to the Proposal milestone Feb 13, 2023
@earthboundkid
Copy link
Contributor

This has been proposed before. See https://github.com/go-json-experiment/json for work on a next generation encoding/json package.

@seankhliao
Copy link
Member

Duplicate of #19763

@seankhliao seankhliao marked this as a duplicate of #19763 Feb 13, 2023
@seankhliao seankhliao closed this as not planned Won't fix, can't repro, duplicate, stale Feb 13, 2023
@golang golang locked and limited conversation to collaborators Feb 13, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants