Closed
Description
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.
Metadata
Metadata
Assignees
Type
Projects
Milestone
Relationships
Development
No branches or pull requests
Activity
agnivade commentedon 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.