You can write a replacement and reviver function for JSON.stringifyand JSON.parse, respectively. The substitute can use -0 === 0, 1 / 0 === Infinityand 1 / -0 === -Infinityto determine negative zeros and convert them to a special line. The examiner should simply convert the special string back to -0. Here is the jsfiddle.
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
"NEGATIVE_ZERO", , "(-0)".