If I put json:",omitempty"
after a field, (for example a string) if the string is empty, the result is not contain the field.
But I have a custom type with custom MarshalJSON function. the problem is, How i can tell the json library that this value is empty?
something like this (this not work, I get the : “json: error calling MarshalJSON for type main.Custom: unexpected end of JSON input” when the Show is false)
type Custom struct {
Name string
Show bool
}
type FinalJSON struct {
T1 Custom `json:"field,omitempty"`
T2 string `json:"field2,omitempty"
}
func (c *Custom) MarshalJSON() ([]byte, error) {
if !c.Show {
return nil, nil
}
return json.Marshal(c.Name)
}