Last active
April 26, 2016 16:31
-
-
Save jdub/a84810e6ca7b433931f3a58924d917c0 to your computer and use it in GitHub Desktop.
JavaScriptBoolean type for unmarshalling truthy JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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