Your data will work in all existing browsers - it just loses precision. Your example of python output is exactly the thing that would cause a problem. > (This will parse in today's browsers, too, but you'll get an imprecise result, as JavaScript's Number cannot represent that value exactly. What should JSON.parse() do? There's no reason for it to parse any of the values before the final extra precision value as a regular number, so if it parses the large one as a bigint, it would break. Because you can't mix and match numeric types, JSON.parse() cannot distinguish between a number, and a bigint. In JS your number would drop precision, because it doesn't have the 'n' suffix. (This will parse in today's browsers, too, but you'll get an imprecise result, as JavaScript's Number cannot represent that value exactly. Python, for example, will happily serialize such integers: There are serializers out there that support arbitrary precision integers. > “I want to transmit non-Json strings but claim they’re json “. The subsequent paragraphs warn, of course, that not all implementations support sending or receiving numbers outside of certain ranges, and explicitly calls out the contiguous integer range of an IEEE double as a good range for good interoperability. > This specification allows implementations to set limits on the range and precision of numbers accepted. > It’s all interoperable because there is just that one syntax.Īnd that syntax and specification leaves open the possibility of arbitrary precision integers. If any of the values get interpreted as a BigInt this will throw. So should this parse as Number, BigInt, Number, BigInt? Or BigInt, BigInt, BigInt, BigInt? What value should be the trigger for treating an integer as a BigInt vs. The first and third are exactly representable as a double, the second and forth are not. Should this parse to a BigInt or a Number? In JavaScript today it will parse as a Number, without losing precision, but it is beyond the range of exactly representable integers in JS. Ok, let's try to do something about the endless downvotes: * all shipped products that read json, because as with the browsers they could not parse any “json” that contained invalid data. * old browsers - not supporting bigints would mean they couldn’t parse any of your “json” data, even if you weren’t using the bigints. So if you say “add JSON.parse() API”, you’re saying “I want to transmit non-Json strings but claim they’re json “. It’s all interoperable because there is just that one syntax. JSON is a stable interchange format, and for that reason we’ve got a huge base of tooling that uses and emits it. This is not related to parsing, it’s related to the JSON format.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |