Skip to content

proposal: add omitnull option #33400

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

Closed
Qhesz opened this issue Aug 1, 2019 · 1 comment
Closed

proposal: add omitnull option #33400

Qhesz opened this issue Aug 1, 2019 · 1 comment

Comments

@Qhesz
Copy link

Qhesz commented Aug 1, 2019

Current behaviour

Given the following types:

type container struct {
    A optInt `json:"a"`
}

type optInt struct {
    Present bool
    Value int
}

func (o *optInt) UnmarshalJSON(b []byte) error { ... }

func (o optInt) MarshalJSON() ([]byte, error) {
    // Returns "null" if not present
    ...
}

The input json {} would be marshalled back out as {"a":null}

Proposed optional behaviour:

With an extra option omitnull:

type container struct {
    A optInt `json:"a,omitnull"`
}

The input json {} would be marshalled back out as {}

In general, omitnull would mean that if the tagged field's value marshals to the json null value, then omit the field entirely from the output.

Reason I don't want to use *int

I want to maintain == on structs, and adding a pointer inside my container struct means I can no longer use ==.

Generalising

This would provide a mechanism for omitting any nested struct, which is the only (?) type not covered by the existing omitempty option.

@gopherbot gopherbot added this to the Proposal milestone Aug 1, 2019
@agnivade
Copy link
Contributor

agnivade commented Aug 1, 2019

Hello @Qhesz, thanks for creating this proposal. This is a duplicate of #11939. I will close this in favor of that one. Please feel free to subscribe to that one. Thank you.

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

3 participants