Allow serializing of Infinity/NaN/etc

Bug #325998 reported by alecf
4
Affects Status Importance Assigned to Milestone
jsonlib
Won't Fix
Undecided
Unassigned

Bug Description

I have some cases where I need to serialize Infinity/<inf> from Python out to JavaScript.

Attached is a patch that does this.

The patch also includes a fallback to UTF8/etc encoding when the encoder encounters a non-ascii 8-bit string.

Revision history for this message
alecf (alecf) wrote :
Revision history for this message
John Millikin (jmillikin) wrote :

The literal values Infinity, -Infinity and NaN are features of JavaScript, but are not part of JSON[1]. If you serialize them directly, any conforming parser, including JSON.org's parser for JavaScript[2], will raise an exception.

This is similar to the problem of handling dates, which also have no explicit JSON representation. One popular solution is to encode unrepresentable values to strings, and then convert them back on the client side.

If at some future point the JSON spec is amended to include these values, jsonlib will be updated to include support for them. Until then, I have no plans to support non-standard extensions to the format.

[1] http://www.ietf.org/rfc/rfc4627.txt
[2] http://json.org/json2.js

Changed in jsonlib:
status: New → Won't Fix
Revision history for this message
alecf (alecf) wrote :

I know it's not part of the standard, but clearly you were considering it because most of the plumbing is already in the C code, I just hooked it up.

It's pretty disappointing, and probably why there are so many json parsers out there: I'm probably going to have to fork jsonlib because supporting Javascript's notion of Infinity/NaN/etc is more important to my project than adhering to the JSON standard (Which is overly restrictive IMO) - simplejson really gets this right, it's just slow!

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.