Hacker News new | past | comments | ask | show | jobs | submit login

Can you provide some more context?

    > JSON.stringify(1)
    "1"
Stored without a decimal for free, seems like it'd be up to the receiver to interpret it correctly.

What if you receive it in JS?

    JSON.parse(JSON.stringify(1))
    > 1
Ok, so it parses an int for free too... thus my confusion.

Regardless, you can still override anything it does considering the second argument to both `JSON.parse()` and `JSON.stringify()` allows you to provide your own function for handling the logic as you see fit.




JSON.stringify(9223372036854775807) "9223372036854776000" JSON.stringify(9007199254740993) "9007199254740992"

JSON doesn't support integers for example on my 64bit system it can't support INT_MAX as a value and anything greater then 9007199254740993 might just come out as wrong.


You haven't demonstrated your premise. JSON is a format for storing data, completely separate from the JavaScript language. Try validating this in a JSON validator: { "number" : 9007199254740993 }

It's absolutely valid JSON. What you're seeing happen is that the JavaScript language parses the Numeric Literal 9007199254740993 to a value of the Number type, and this value does not accurately represent the intended number. This is a feature of JavaScript, not JSON.

You can see the JSON spec here: http://json.org It does not talk about number size or type - simply that a number is a series of digits(and other symbols).

The JSON library for JavaScript has an inherent limitation when parsing JSON formatted numbers, because it tries to represent them as the Number type, and the Number type cannot represent large numbers. It is absolutely possible to write a JSON parsing library that parses JSON numbers into strings(example - http://php.net/manual/en/function.json-decode.php with the JSON_BIGINT_AS_STRING option). This does not change what kind of numbers JSON supports(any decimal).




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: