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

python - No exponential form of the z-axis in matplotlib-3D-plots -

php - Best Light server (Linux + Web server + Database) for Raspberry Pi -

c# - "Newtonsoft.Json.JsonSerializationException unable to find constructor to use for types" error when deserializing class -