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.