Skip to content

Instantly share code, notes, and snippets.

@kevholditch
Created July 31, 2018 19:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevholditch/7912c58233c1b0cfb8bf175fa2aef16a to your computer and use it in GitHub Desktop.
Save kevholditch/7912c58233c1b0cfb8bf175fa2aef16a to your computer and use it in GitHub Desktop.
Custom go JSON deserialisation
func (a *Api) UnmarshalJSON(data []byte) error {
fixedJson := strings.Replace(string(data), `"hosts":{}`, `"hosts":[]`, -1)
fixedJson = strings.Replace(fixedJson, `"uris":{}`, `"uris":[]`, -1)
fixedJson = strings.Replace(fixedJson, `"methods":{}`, `"methods":[]`, -1)
type Alias Api
aux := &struct {
*Alias
}{
Alias: (*Alias)(a),
}
return json.Unmarshal([]byte(fixedJson), &aux)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment