Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
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
You can’t perform that action at this time.