diff --git a/sio/jsonio/writer.go b/sio/jsonio/writer.go index cd4cf6e3c..fc1e67095 100644 --- a/sio/jsonio/writer.go +++ b/sio/jsonio/writer.go @@ -7,6 +7,8 @@ import ( "encoding/json" "fmt" "io" + "math" + "strconv" "time" "github.com/superdb/super" @@ -234,7 +236,11 @@ func (w *Writer) writePrimitive(val super.Value) { case super.IsUnsigned(id): v, c = val.Uint(), numberColor case super.IsFloat(id): - v, c = val.Float(), numberColor + if f := val.Float(); math.IsInf(f, 0) || math.IsNaN(f) { + v = strconv.FormatFloat(f, 'g', -1, 64) + } else { + v, c = f, numberColor + } case id == super.IDBool: v, c = val.AsBool(), boolColor case id == super.IDBytes: diff --git a/sio/jsonio/ztests/nonfinite-floats.yaml b/sio/jsonio/ztests/nonfinite-floats.yaml new file mode 100644 index 000000000..07de628d2 --- /dev/null +++ b/sio/jsonio/ztests/nonfinite-floats.yaml @@ -0,0 +1,16 @@ +# Infinities and NaN, which JSON numbers can't represent, are written as +# strings in their SUP form, including by json_stringify(). + +spq: | + values 1.0/0, -1.0/0, 0.0/0, + {a:+Inf::float32,b:[-Inf::float16,NaN],c:1.5}, + json_stringify([+Inf,1.5]) + +flags: -f json + +output: | + "+Inf" + "-Inf" + "NaN" + {"a":"+Inf","b":["-Inf","NaN"],"c":1.5} + "[\"+Inf\",1.5]"