强制JSON.stringify()发出NaN / Infinity或JS JSON lib [英] Force JSON.stringify() to emit NaN / Infinity or JS JSON lib that does so
问题描述
我正在研究将NaN / Infinity支持添加到使用JSONRPC进行客户端/服务器交互的预先存在的科学应用程序的可行性。许多JSON库确实处理(可选在某些情况下)NaN和Infs,例如:
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:
- Python json 读取和写入
- Java Jackson 读取但是写字符串而不是裸字
- Java < a href =http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/com/google/gson/GsonBuilder.html#serializeSpecialFloatingPointValues%28%29 =nofollow noreferrer> GSON 读取和写入
- Javascript可以阅读
- Python json reads and writes
- Java Jackson reads but writes strings instead of barewords
- Java GSON reads and writes
- Javascript can read
我知道 JSON规范,我知道相关 问题 。但是,AFAICT是否有某种方法可以强制使用本机JS JSON.stringify()
方法来发出NaN / Infinity,或者有一个JS JSON库可以执行同样没有答案。或许,引用问题的细微差别,但很重要。到目前为止,我一直无法发现这样的方法或库,所以我在这里。是编写自己的JSON序列化程序的唯一选择吗?
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?
请注意的
没有帮助,至少在我手中。替换
参数JSON.stringify()
Note that the replacement
parameter of JSON.stringify()
is not helpful, at least in my hands.
更新:发出NaN / Infinity等字符串使得这些字符串的语义模糊不清。它们需要像Python和GSON实现一样以单词形式发出。
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.
推荐答案
这是一个例子
Javascript
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);
输出
["-Infinity",-1,0,1,2,"NaN",4,5,"Infinity"]
[-Infinity, -1, 0, 1, 2, NaN, 4, 5, Infinity]
参考文献
在 jsFiddle
更新:
Javascript
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);
输出
["-1/0",-1,0,1,2,"0/0",4,5,"1/0"]
[-Infinity, -1, 0, 1, 2, NaN, 4, 5, Infinity]
On jsFiddle
这篇关于强制JSON.stringify()发出NaN / Infinity或JS JSON lib的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!