You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
The text was updated successfully, but these errors were encountered:
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.
Current behaviour
Given the following types:
The input json
{}
would be marshalled back out as{"a":null}
Proposed optional behaviour:
With an extra option
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 jsonnull
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.The text was updated successfully, but these errors were encountered: