I'm trying to serialize numbers using ujson
module in Python3. Some of the values are NaN
s.
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 NaN
s without throwing an exception?
I also have the very same issues with infinities. I was unable to find any relevant docs.
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With