Skip to content

Instantly share code, notes, and snippets.

@andyxning
Created December 15, 2015 08:55
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 andyxning/fc468eccbbc0a1b9af90 to your computer and use it in GitHub Desktop.
Save andyxning/fc468eccbbc0a1b9af90 to your computer and use it in GitHub Desktop.
NaN, Inf and Near zero value MarshalJSON
type JsonSpecial float64
func (value JsonSpecial) MarshalJSON() ([]byte, error) {
// Handle NaN, Inf and near-zero values marshalling
if math.IsNaN(float64(value)) || math.IsInf(fload64(value), 0) {
return json.Marshal(nil)
} else if math.Exp(float64(value)) == 1 {
return json.Marshal(0)
}
return json.Marshal(float64(value))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment