Commit 69be071
committed
Normative: Make JSON.stringify(-0) preserve the sign
Although JSON supports -0 per ECMA-404 and JSON.parse('-0') returns -0, JSON.stringify(-0) currently loses the sign, outputting just '0'.
This patch makes JSON.stringify(-0) return '-0' instead, so that the following holds:
Object.is(JSON.parse('-0'), -0);
// → true (this is already the case, even without this patch)
JSON.stringify(-0);
// → '-0'
Object.is(JSON.parse(JSON.stringify(-0)), -0);
// → true
Results in current JavaScript engines:
$ eshost -se 'Object.is(JSON.parse("-0"), -0)'
#### Chakra, JavaScriptCore, SpiderMonkey, V8
true
#### XS
false
$ eshost -se 'JSON.stringify(-0)'
#### Chakra, JavaScriptCore, SpiderMonkey, V8, XS
0
$ eshost -se 'Object.is(JSON.parse(JSON.stringify(-0)), -0)'
#### Chakra
true
#### JavaScriptCore, SpiderMonkey, V8, XS
false1 parent a09fc23 commit 69be071
1 file changed
+1
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
36323 | 36323 | | |
36324 | 36324 | | |
36325 | 36325 | | |
| 36326 | + | |
36326 | 36327 | | |
36327 | 36328 | | |
36328 | 36329 | | |
| |||
0 commit comments