Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON dump non-numeric floats with Python's ujson module

I'm trying to serialize numbers using ujson module in Python3. Some of the values are NaNs.

When using the standard json module, everything works fine.

import json
json.dumps(float('NaN'))

gives:

'NaN'

But there is a problem with ujson.

import ujson
ujson.dumps(float('NaN'))

throws an exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OverflowError: Invalid Nan value when encoding double

I'm using ujson for performance reasons. Do I have to replace all the occurrences of NaN in my structures with the string 'NaN', or is there a way to tell ujson to handle NaNs without throwing an exception?

I also have the very same issues with infinities. I was unable to find any relevant docs.

like image 480
Tregoreg Avatar asked Aug 15 '14 21:08

Tregoreg


1 Answers

According to the RFC4627 that defines the JSON format, in section 2.4 about numbers:

Numeric values that cannot be represented as sequences of digits (such as Infinity and NaN) are not permitted.

So ujson is more compliant with JSON standard then the json module in standard library. According to me this result unnecessarily pedantic, but the choice of ujson is to conform to this standard. You can read more about this here.

It seems that the only way is to convert the data before (or submit a pull request to ujson to enable it).

like image 186
enrico.bacis Avatar answered Oct 06 '22 01:10

enrico.bacis