Reputation: 381
How can I use JSON.stringify to convert a negative zero into a string (-0)? It appears that JSON.stringify converts a negative zero into a string representing a positive one. Any idea for a nice workaround?
var jsn = {
negative: -0
};
isNegative(jsn.negative) ? document.write("negative") : document.write("positive");
var jsonString = JSON.stringify(jsn),
anotherJSON = JSON.parse(jsonString);
isNegative(anotherJSON.negative) ? document.write("negative") : document.write("positive");
function isNegative(a)
{
if (0 !== a)
{
return !1;
}
var b = Object.freeze(
{
z: -0
});
try
{
Object.defineProperty(b, "z",
{
value: a
});
}
catch (c)
{
return !1;
}
return !0;
}
Upvotes: 8
Views: 4221
Reputation: 109
you can use JSON.stringify with a replacer function to change out negative zeros to special strings (as stated in the prior answer), and then use a global string replace to change those special strings back to negative zeros in the resulting json string. Ex:
function json(o){
return JSON.stringify(o,(k,v)=>
(v==0&&1/v==-Infinity)?"-0.0":v).replace(/"-0.0"/g,'-0')
}
console.log(json({'hello':0,'world':-0}))
Upvotes: 0
Reputation: 28688
You can write a replacer and a reviver function, for JSON.stringify
and JSON.parse
, respectively. The replacer can utilize that -0 === 0
, 1 / 0 === Infinity
and 1 / -0 === -Infinity
to identify negative zeros and convert them to a special string. The reviver should simply convert the special string back to -0
. Here is the jsfiddle.
The code:
function negZeroReplacer(key, value) {
if (value === 0 && 1 / value < 0)
return "NEGATIVE_ZERO";
return value;
}
function negZeroReviver(key, value) {
if (value === "NEGATIVE_ZERO")
return -0;
return value;
}
var a = {
plusZero: 0,
minusZero: -0
},
s = JSON.stringify(a, negZeroReplacer),
b = JSON.parse(s, negZeroReviver);
console.clear();
console.log(a, 1 / a.plusZero, 1 / a.minusZero)
console.log(s);
console.log(b, 1 / b.plusZero, 1 / b.minusZero);
Output:
Object {plusZero: 0, minusZero: 0} Infinity -Infinity
{"plusZero":0,"minusZero":"NEGATIVE_ZERO"}
Object {plusZero: 0, minusZero: 0} Infinity -Infinity
I converted negative zeros to "NEGATIVE_ZERO"
, but you can use any other string, like "(-0)"
.
Upvotes: 6