Skip to content

Instantly share code, notes, and snippets.

@JonathanPorta
Forked from kendellfab/golang-json-marshall
Created October 5, 2015 17:25
Show Gist options
  • Save JonathanPorta/932f22dd2e68336d1c55 to your computer and use it in GitHub Desktop.
Save JonathanPorta/932f22dd2e68336d1c55 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