Closed sardok closed 10 years ago
@sardok, JSON has tighter rules for numbers than plain JS. As noted on RFC 4627, page 3, if a number starts with zero, then it should either end right there, or have a decimal point coming right after it.
Thanks @behnam for a quick response. I think, the reason why demjson still has usage is its support to non-strict options such as supporting octal numbers, nonstrict keys etc, and i thought this change could fit demjson as an option in that manner.
From RFC 7159 section 6 http://tools.ietf.org/html/rfc7159
"... Leading zeros are not allowed."
Plus, leading zeros have, until ECMAScript 5, always indicated Octal numbers, and it will be many years before it is ubiquitous. Not to mention all the languages other than JavaScript which interchange data using JSON.
I may consider adding a "default radix" option for non-strict parsing, but it's an unfortunate ambiguity in JavaScript and reduces data portability.
It is also interesting, though unrelated, that Python has made a similar change in the meaning of leading zeros.
Deron
On Mon, Apr 28, 2014 at 1:46 PM, Sinan Nalkaya notifications@github.comwrote:
Thanks @behnam https://github.com/behnam for a quick response. I think, the reason why demjson still has usage is its support to non-strict options such as supporting octal numbers, nonstrict keys etc, and i thought this change could fit demjson as an option in that manner.
— Reply to this email directly or view it on GitHubhttps://github.com/dmeranda/demjson/pull/7#issuecomment-41589844 .
Deron Meranda http://deron.meranda.us/
Thank you all, for very succinct answers.
The upcoming new version 2.0, which is very close to being ready, will support choosing the radix to use for numbers with leading zeros; whether as octal or decimal numbers. Only in non-strict mode, of course.
Since 2.0 will be a major almost-rewrite, the pull request is not needed. However it might be useful for somebody wanting to stick to 1.x.
After a long delay, I've finally released version 2.0. It includes radix support and much more.
Check out http://deron.meranda.us/python/demjson/ for changes and documentation.
This PR allows decimal string to be started with multiple 0s if the related option is given.
I don't know if this change comply with JSON RFC but it seems that leading zeros in decimal number seems to be valid in js:
Similar approach documented at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt.