javascript - JSON.stringify converting Infinity to null -


i have javascript object say:

var = {b: infinity, c: 10}; 

when do

var b = json.stringify(a); 

it returns following

b = "{"b":null, "c":10}";

how json.stringify converts object strings?

i tried mdn solution.

function censor(key, value) {   if (value == infinity) {     return "infinity";   }   return value; } var b = json.stringify(a, censor); 

but in case have return string "infinity" not infinity. if return infinity again converts infinity null.

how solve problem.

like other answers stated, infintity not part of values json can store value.

you can reverse censor method on parsing json:

var c = json.parse(           b,           function (key, value) {             return value === "infinity"  ? infinity : value;           }         ); 

Comments

Popular posts from this blog

.htaccess - First slash is removed after domain when entering a webpage in the browser -

Automatically create pages in phpfox -

c# - Farseer ContactListener is not working -