dmeranda / demjson

Python module for JSON data encoding, including jsonlint. See the project Wiki here on Github. Also read the README at the bottom of this page, or the project homepage at
http://deron.meranda.us/python/demjson/
Other
302 stars 76 forks source link

New option allow_zero_leading_decimals. #7

Closed sardok closed 10 years ago

sardok commented 10 years ago

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:

 > 012389
=> 12389
 > 000012389
=> 12389

Similar approach documented at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt.

behnam commented 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.

sardok commented 10 years ago

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.

dmeranda commented 10 years ago

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/

sardok commented 10 years ago

Thank you all, for very succinct answers.

dmeranda commented 10 years ago

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.

dmeranda commented 10 years ago

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.