Closed noamt closed 4 years ago
Consider checking if the value is NaN within the marshaller
e.g
package main
import (
"encoding/json"
"fmt"
"math"
"github.com/francoispqt/gojay"
)
type A struct {
B float64
}
func (a *A) MarshalJSONObject(enc *gojay.Encoder) {
if !math.IsNaN(a.B) {
enc.Float64Key("b", a.B)
}
}
func (a *A) IsNil() bool {
return a == nil
}
func main() {
a := &A{B: math.NaN()}
b, e := json.Marshal(a)
fmt.Printf("body %s Error: %v\n", b, e)
b, e = gojay.Marshal(a)
fmt.Printf("body %s Error: %v\n", b, e)
}
https://goplay.space/#2CR0VbuT-M0
It won't error out still, but the JSON will be a valid empty JSON
If I have a struct with a floating point number field like:
And I assign it with
NaN
like:Running this through the standard library's Marshal method will fail with
Error: json: unsupported value: NaN
:But using GoJay's Marshal method produces an invalid JSON
{"b":NaN}
:How would you recommend handling this situation?