Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion sio/jsonio/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"encoding/json"
"fmt"
"io"
"math"
"strconv"
"time"

"github.com/superdb/super"
Expand Down Expand Up @@ -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:
Expand Down
16 changes: 16 additions & 0 deletions sio/jsonio/ztests/nonfinite-floats.yaml
Original file line number Diff line number Diff line change
@@ -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]"
Loading