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

> JSON is just this beautiful thing that lets you store data however you want.

What if I want to store integers?

Not floats.

Integers.




Store it as a string and write your own math routines.

I don't know why you guys are having trouble with this. \s


Or dates? No one would ever need to store dates or times, right?


Write the dates/times as string in ISO 8601 format.


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).




Consider applying for YC's W25 batch! Applications are open till Nov 12.

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

Search: