Skip to content

Instantly share code, notes, and snippets.

@kendellfab
Created August 16, 2013 22:35
Show Gist options
  • Save kendellfab/6254073 to your computer and use it in GitHub Desktop.
Save kendellfab/6254073 to your computer and use it in GitHub Desktop.
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