I'm looking into the feasibility of adding NaN/Infinity support to a pre-existing scientific application that uses JSONRPC for client/server interactions. Many JSON libs do handle (optionally in some cases) NaNs and Infs, for example:
I'm aware that NaN and Infinity are not supported in the JSON spec, and am aware of the related questions. However, AFAICT the question of whether there's some way of coercing the native JS JSON.stringify()
method to emit NaN/Infinity or, alternately, there's a JS JSON library that does the same is unanswered. A subtle difference to the referenced questions, perhaps, but important. So far I've been unable to discover such a method or library, so here I am. Is the only option writing one's own JSON serializer?
Note that the replacement
parameter of JSON.stringify()
is not helpful, at least in my hands.
UPDATE: Emitting NaN/Infinity etc. as strings makes the semantics of those strings ambiguous. They need to be emitted as barewords as in the Python and GSON implementations.
The JSON.stringify() method converts a JavaScript value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.
There is no way to represent Infinity or NaN in JSON. There is a very strict syntax for numbers, and Infinity or NaN is not part of it.
The JSON. parse() function is used to convert a string into a JavaScript object while the JSON. stringify() function is used to convert a JavaScript object into a string.
Use the JavaScript function JSON.stringify() to convert it into a string. const myJSON = JSON.stringify(obj); The result will be a string following the JSON notation.
Here is an example
Javascript
var array1 = [-Infinity, -1, 0, 1, 2, NaN, 4, 5, Infinity],
json = JSON.stringify(array1, function (key, value) {
if (value !== value) {
return 'NaN';
}
if (value === Infinity) {
return 'Infinity';
}
if (value === -Infinity) {
return '-Infinity';
}
return value;
}),
array2 = JSON.parse(json, function (key, value) {
if (value === 'NaN') {
return NaN;
}
if (value === 'Infinity') {
return Infinity;
}
if (value === '-Infinity') {
return -Infinity;
}
return value;
});
console.log(json);
console.log(array2);
Output
["-Infinity",-1,0,1,2,"NaN",4,5,"Infinity"]
[-Infinity, -1, 0, 1, 2, NaN, 4, 5, Infinity]
References
JSON.stringify
JSON.parse
On jsFiddle
Update:
Javascript
var array1 = [-Infinity, -1, 0, 1, 2, NaN, 4, 5, Infinity],
json = JSON.stringify(array1, function (key, value) {
if (value !== value) {
return '0/0';
}
if (value === 1/0) {
return '1/0';
}
if (value === -1/0) {
return '-1/0';
}
return value;
}),
array2 = JSON.parse(json, function (key, value) {
if (value === '0/0') {
return 0/0;
}
if (value === '1/0') {
return Infinity;
}
if (value === '-1/0') {
return -1/0;
}
return value;
});
console.log(json);
console.log(array2);
Output
["-1/0",-1,0,1,2,"0/0",4,5,"1/0"]
[-Infinity, -1, 0, 1, 2, NaN, 4, 5, Infinity]
On jsFiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With