Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Handling custom Json marshalling in golang.
type Whatever struct {
someField int
}
func (w Whatever) MarshalJSON() ([]byte, error) {
return json.Marshal(struct{
SomeField int `json:"some_field"`
}{
SomeField: w.someField,
})
}
func (w Whatever) MarshalJSON() ([]byte, error) {
return json.Marshal(map[string]interface{}{
"some_field": w.SomeField,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment