Skip Menu |

This queue is for tickets about the JSON-PP CPAN distribution.

Report information
The Basics
Id: 84511
Status: open
Priority: 0/
Queue: JSON-PP

People
Owner: Nobody in particular
Requestors: lindahl [...] pbm.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: (no value)
Fixed in: (no value)



Subject: inf and nan handling
Perl's JSON deals with +-inf and nan by emitting invalid json. This is documented: "these cannot be represented in JSON, and it is an error to pass those in." This is not so user-friendly: the json standard says you should use null. There is no option to emit null. The Python JSON module defaults to throwing an exception, or optionally emits valid Javascript names for these guys. That is against the json standard, but hey, it's optional. http://docs.python.org/2/library/json.html Show quoted text
> If allow_nan is False (default: True), then it will be a ValueError to > serialize out of range float values (nan, inf, -inf) in strict > compliance of the JSON specification, instead of using the JavaScript > equivalents (NaN, Infinity, -Infinity).
See also: http://stackoverflow.com/questions/1423081/json-left-out-infinity-and-nan-json-status-in-ecmascript for a long and fruitless discussion. Suggestion: At the least, it would be nice for perl JSON to have an option where it emits null for these values -- conforming to the standard. It would also be nice to have an option to throw an exception when nan/inf are encoutered. It might also be nice to have an option to do it the python (Nan/Infinity) way, and to accept the python way. Thanks for the JSON module, we used the JSON::XS module as the basis of our serializer/deserializer, and have a few petabytes of data stored with it!
From: bitcard [...] stormcloud9.net
On Mon Apr 08 19:43:13 2013, lindahl@pbm.com wrote: Show quoted text
> Perl's JSON deals with +-inf and nan by emitting invalid json. This is > documented: "these cannot be represented in JSON, and it is an error > to pass those in." This is not so user-friendly: the json standard > says you should use null. There is no option to emit null. > > The Python JSON module defaults to throwing an exception, or > optionally emits valid Javascript names for these guys. That is > against the json standard, but hey, it's optional. > > http://docs.python.org/2/library/json.html >
> > If allow_nan is False (default: True), then it will be a ValueError > > to > > serialize out of range float values (nan, inf, -inf) in strict > > compliance of the JSON specification, instead of using the JavaScript > > equivalents (NaN, Infinity, -Infinity).
> > See also: > > http://stackoverflow.com/questions/1423081/json-left-out-infinity-and- > nan-json-status-in-ecmascript > > for a long and fruitless discussion. > > Suggestion: > > At the least, it would be nice for perl JSON to have an option where > it emits null for these values -- conforming to the standard. It would > also be nice to have an option to throw an exception when nan/inf are > encoutered. > > It might also be nice to have an option to do it the python > (Nan/Infinity) way, and to accept the python way. > > Thanks for the JSON module, we used the JSON::XS module as the basis > of our serializer/deserializer, and have a few petabytes of data > stored with it!
This gets even better. The JSON module can't parse its own data: # perl -e 'use JSON; decode_json(encode_json([NaN + 0]));' 'null' expected, at character offset 1 (before "nan]") at -e line 1.