3 ms·
The common approach I have seen/used is to encode big ints as a string instead. The lack of support for NaN and +/-Infininty means that doubles won't encode in
by benjaminjackman 12y ago
The common approach I have seen/used is to encode big ints as a string instead. The lack of support for NaN and +/-Infininty means that doubles won't encode in JSON either.
On other trick is that because comments aren't supported is to make a keys named "//SomeComment":"", this will still be syntactically correct json and you can simply ignore them inside your program.
JSON is missing a lot of little things, but once you add them in you end up with something that is a lot harder to parse, which ultimately hurts the ubiquitous appeal of json.
- schoen 12y agoThe JSON parser in the Python standard library does support big ints, NaN, and +/-Infinity. >>> json.loads("NaN") nan >>> json.loads("Infinity") inf >>> json.loads("-Infinity") -inf >>> json.loads("1234567890"*20) 123456789012345678901234567890123456789012345678901234567890 123456789012345678901234567890123456789012345678901234567890 123456789012345678901234567890123456789012345678901234567890 12345678901234567890L I haven't read the JSON spec, so I'm wondering whether this is actually standard JSON, whether it's an official extension of some kind to JSON, or whether the Python JSON parser is just being too clever.
- comex 12y agoNot standard. > Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted. - http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf http://www.ecma-international.org/publications/files/ECMA-ST...
- schoen 12y agoHuh, the Python json module documentation admits that the Python behavior isn't standards-compliant. https://docs.python.org/2/library/json.html#standard-compliance-and-interoperability https://docs.python.org/2/library/json.html#standard-complia... It does claim that the support for arbitrary precision numbers is standards-compliant. Looking at the ECMA standard briefly, that seems to be correct to me: the official JSON grammar does allow arbitrary-precision, and disclaims restrictions on how a particular programming language interprets numbers. I have to admit that I'm kind of concerned about the NaN and Infinity issue because I thought there was more uniformity among JSON parsers about what is or isn't legitimate JSON.