Reputation: 1312
In Golang, I have a struct whose member is a custom int type with constant values. Basically, the custom type is a logical enum.
type Flavor int
const (
Vanilla Flavor = iota
Chocolate
Strawberry
)
func (f *Flavor) MarshalJSON() ([]byte, error) {
return []byte(strconv.Quote(f.String())), nil
}
The custom type has defined MarshalJSON and UnmarshalJSON functions so when I serialize the custom type to JSON, I expect to get the string of the value in the serialized output, not the int value.
My issue is that if I have a pointer to a containing type, then the containing type marshals using the custom function but if try to marshal with just a struct value, the custom MarshalJSON is not invoked by the JSON package
type Dessert struct {
Flavor Flavor `json:"flavor"`
Count int
}
....
d := Dessert{Strawberry, 13}
b, err = json.Marshal(d) // !! does not invoke members Marshal !!
b, err = json.Marshal(&d) // works as expected
....
produces
{"flavor":2,"Count":13}
{"flavor":"Strawberry","Count":13}
I expected the second output in both case.
Why does passing a struct value not invoke MarshalJSON on the member but it does encode otherwise correct JSON?
see https://play.golang.org/p/mOl1GHhgynf for full working code
Upvotes: 1
Views: 1226
Reputation: 1312
oh huh. I think you had it Volker and Leon. I had assumed that I needed a pointer receiver for MarshalJSON since UnmarshalJSON definitely needs a pointer receiver. But
func (f Flavor) MarshalJSON() ([]byte, error) {
...
func (f *Flavor) UnmarshalJSON(b []byte) error {
...
and mixing the receivers causes the expected output for both json.Marshal(d) and json.Marshal(&d)
Upvotes: -1
Reputation: 42431
In your code Flavor
does not have a method MarshalJSON as you defined the method for *Flavor
only.
If you want type Flavor
to have the MarshalJSON method you must define it on Flavor
not *Flavor
.
Upvotes: 8