Created
December 15, 2015 08:55
-
-
Save andyxning/fc468eccbbc0a1b9af90 to your computer and use it in GitHub Desktop.
NaN, Inf and Near zero value MarshalJSON
This file contains hidden or 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 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