type Foo struct {
ID string `json:"id"
Condition bool `json:"condition,omitempty"`
}
I would like to json.Marshal the Foo struct
above but the Condition field should have three states. For example if I do the JSON marshaling:
1)
foo := Foo { ID: "one", Condition: true, }
should turn into json:
{id:"one","condition":true}
2)
foo := Foo { ID: "two", Condition: false, }
should turn into json:
{id:"two","condition":false}
3)
foo := Foo { ID: "three", }
should turn into json:
{id:"three"}
Currently, if I do 2)
, the marshaled json will be {"id":"two"}
instead of {"id":"two,"condition":false}
because the zero value of bool
is false
.
One solution is to construct my struct to use the type of *bool
instead, like this:
type Foo struct {
ID string `json:"id"
Condition *bool `json:"condition,omitempty"`
}
Is that the idiomatic way of differentiating between non-existent bool and false bool in Go?