admin管理员组

文章数量:1290756

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 reads and writes
  • Java Jackson reads but writes strings instead of barewords
  • Java GSON reads and writes
  • Javascript can read

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.

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 reads and writes
  • Java Jackson reads but writes strings instead of barewords
  • Java GSON reads and writes
  • Javascript can read

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.

Share Improve this question edited May 23, 2017 at 12:33 CommunityBot 11 silver badge asked Feb 20, 2014 at 2:20 elhefeelhefe 3,5044 gold badges33 silver badges47 bronze badges 6
  • 1 This is exactly a replacement parameter job, it's not so difficult. Perhaps you can specify more about your actual problem, along with an example and sample data? – Xotic750 Commented Feb 20, 2014 at 2:48
  • JSON.stringify({a:1,b:1/"gg", c:{d:5}}, function(a,b,c){if(Object.is(NaN, b)){return "NaN";} return b;}) – dandavis Commented Feb 20, 2014 at 2:50
  • @dandavis see the update above. – elhefe Commented Feb 20, 2014 at 5:05
  • If you're going to downvote the question, at least explain what's wrong with it so I can fix it. – elhefe Commented Feb 20, 2014 at 6:25
  • 1 you can use a guid instead of, ex:"NaN" if you're worried about string collisions and want the use the fast native parser. – dandavis Commented Feb 20, 2014 at 8:18
 |  Show 1 more ment

2 Answers 2

Reset to default 7

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

replacement parameter of JSON.stringify is quite right tool for the job.

JSON.stringify(data, function(key, value) {
   if (value === Infinity) {
      return "Infinity";
   } else if (value === -Infinity) {
      return "-Infinity";
   } else if (value !== value) {
      return "NaN";
   }
   return value;
});

And on the other side you can use reviver parameter of JSON.parse.

 JSON.parse(data, function(key, value) {
   if (value === "Infinity") {
      return Infinity;
   } else if (value === "-Infinity") {
      return -Infinity;
   } else if (value === "NaN") {
      return NaN;
   }
   return value;
});

本文标签: javascriptForce JSONstringify() to emit NaNInfinity or JS JSON lib that does soStack Overflow