fix #18007: std/json now serializes nan,inf,-inf as strings instead of invalid json (#18026)

* fix #18007: std/json now serializes nan,inf,-inf as raw strings instead of invalid json

* fix roundtrip

* fix tests

* fix changelog

* simplify

* add runnableExamples

* fix typo [skip ci]
This commit is contained in:
Timothee Cour 2021-05-16 14:54:10 -07:00 • committed by GitHub
commit d83b25db1e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 119 additions and 51 deletions

View file

@ -4,15 +4,28 @@ discard """
import std/jsonutils
import std/json
from std/math import isNaN
proc testRoundtrip[T](t: T, expected: string) =
# checks that `T => json => T2 => json2` is such that json2 = json
let j = t.toJson
doAssert $j == expected, $j
doAssert $j == expected, "\n" & $j & "\n" & expected
doAssert j.jsonTo(T).toJson == j
var t2: T
t2.fromJson(j)
doAssert t2.toJson == j
proc testRoundtripVal[T](t: T, expected: string) =
# similar to testRoundtrip, but also checks that the `T => json => T2` is such that `T2 == T`
# note that this isn't always possible, e.g. for pointer-like types.
let j = t.toJson
let j2 = $j
doAssert j2 == expected, j2
let j3 = j2.parseJson
let t2 = j3.jsonTo(T)
doAssert t2 == t
doAssert $t2.toJson == j2 # still needed, because -0.0 = 0.0 but their json representation differs
import tables, sets, algorithm, sequtils, options, strtabs
from strutils import contains
@ -91,6 +104,12 @@ template fn() =
else:
testRoundtrip(a): "[9223372036854775807,18446744073709551615]"
block: # bug #18007
testRoundtrip((NaN, Inf, -Inf, 0.0, -0.0, 1.0)): """["nan","inf","-inf",0.0,-0.0,1.0]"""
testRoundtrip((float32(NaN), Inf, -Inf, 0.0, -0.0, 1.0)): """["nan","inf","-inf",0.0,-0.0,1.0]"""
testRoundtripVal((Inf, -Inf, 0.0, -0.0, 1.0)): """["inf","-inf",0.0,-0.0,1.0]"""
doAssert ($NaN.toJson).parseJson.jsonTo(float).isNaN
block: # case object
type Foo = object
x0: float