@@ -4,7 +4,8 @@ discard """
4
4
5
5
import std/ jsonutils
6
6
import std/ json
7
- from std/ math import isNaN
7
+ from std/ math import isNaN, signbit
8
+ from stdtest/ testutils import whenRuntimeJs
8
9
9
10
proc testRoundtrip[T](t: T, expected: string ) =
10
11
# checks that `T => json => T2 => json2` is such that json2 = json
@@ -123,6 +124,20 @@ template fn() =
123
124
testRoundtripVal((Inf , - Inf , 0.0 , - 0.0 , 1.0 )): """ ["inf","-inf",0.0,-0.0,1.0] """
124
125
doAssert ($ NaN .toJson).parseJson.jsonTo(float ).isNaN
125
126
127
+ block : # bug #18009; unfixable unless we change parseJson (which would have overhead),
128
+ # but at least we can guarantee that the distinction between 0.0 and -0.0 is preserved.
129
+ let a = (0 , 0.0 , - 0.0 , 0.5 , 1 , 1.0 )
130
+ testRoundtripVal(a): " [0,0.0,-0.0,0.5,1,1.0]"
131
+ let a2 = $ ($ a.toJson).parseJson
132
+ whenRuntimeJs:
133
+ doAssert a2 == " [0,0,-0.0,0.5,1,1]"
134
+ do :
135
+ doAssert a2 == " [0,0.0,-0.0,0.5,1,1.0]"
136
+ let b = a2.parseJson.jsonTo(type (a))
137
+ doAssert not b[1 ].signbit
138
+ doAssert b[2 ].signbit
139
+ doAssert not b[3 ].signbit
140
+
126
141
block : # case object
127
142
type Foo = object
128
143
x0: float
0 commit comments