Skip to content

Instantly share code, notes, and snippets.

@jdub
Last active April 26, 2016 16:31
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 jdub/a84810e6ca7b433931f3a58924d917c0 to your computer and use it in GitHub Desktop.
Save jdub/a84810e6ca7b433931f3a58924d917c0 to your computer and use it in GitHub Desktop.
JavaScriptBoolean type for unmarshalling truthy JSON
type JavaScriptBoolean bool
func (bit *JavaScriptBoolean) UnmarshalJSON(data []byte) error {
s := strings.ToLower(strings.Trim(string(data), `"`))
if s == "1" || s == "true" {
*bit = true
} else if s == "0" || s == "false" {
*bit = false
} else {
return fmt.Errorf("Boolean unmarshal error: invalid input %s", s)
}
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment