2 ms·
> JSON numbers are too often encoded as strings There's a good reason for that, since JSON comes from JavaScript, many JSON parsers treat numbers as double-pre
by craftkiller 2mo ago
> JSON numbers are too often encoded as strings
There's a good reason for that, since JSON comes from JavaScript, many JSON parsers treat numbers as double-precision floats. By encoding your number as a string, you ensure that the JSON parser has not modified your number.
https://blog.json-everything.net/posts/numbers-are-numbers-not-strings/ https://blog.json-everything.net/posts/numbers-are-numbers-n...
- imoverclocked 2mo agoIs the good reason that parsers are doing this in the first place? To me, it sounds like a practice/implementation that necessitates a second layer of parsing. If so, then maybe JSON is not the optimal medium to be using for the data being distributed.
- craftkiller 2mo agoThe parsers doing this in the first place is the problem. The JSON spec doesn't specify how numbers should be stored/interpreted, so theoretically you could have a 2,000 bit integer in JSON. The lack of clarity around that could also be considered a flaw in the JSON spec. But from a practical standpoint, the most common JSON parser is in web browsers running javascript, and they'll parse to double-precision floats. So from a practical standpoint, if you don't want your numbers distorted, you need to be defensive about it and encode your numbers as strings (or use something other than JSON).