Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON tricks cannot dump nan and infinity #63

Closed
liuzhe-lz opened this issue Nov 6, 2019 · 2 comments
Closed

JSON tricks cannot dump nan and infinity #63

liuzhe-lz opened this issue Nov 6, 2019 · 2 comments

Comments

@liuzhe-lz
Copy link

liuzhe-lz commented Nov 6, 2019

Although they are not included in JSON standard, it's sometimes useful to dump these values.
And they are supported in official Python library.

>>> import math
>>> import json
>>> import json_tricks
>>> test_obj = {'nan': math.nan, 'inf': math.inf, 'neg_inf': -math.inf}
>>> json.dumps(test_obj)
'{"nan": NaN, "inf": Infinity, "neg_inf": -Infinity}'
>>> json_tricks.dumps(test_obj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib/python3.8/site-packages/json_tricks/nonp.py", line 98, in dumps
    txt = cls(sort_keys=sort_keys, obj_encoders=encoders, allow_nan=allow_nan,
  File "/usr/lib/python3.8/json/encoder.py", line 199, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/lib/python3.8/json/encoder.py", line 257, in iterencode
    return _iterencode(o, 0)
ValueError: Out of range float values are not JSON compliant
>>> json.loads('{"nan": NaN, "inf": Infinity, "neg_inf": -Infinity}')
{'nan': nan, 'inf': inf, 'neg_inf': -inf}
>>> json_tricks.loads('{"nan": NaN, "inf": Infinity, "neg_inf": -Infinity}')
OrderedDict([('nan', nan), ('inf', inf), ('neg_inf', -inf)])
@liuzhe-lz
Copy link
Author

liuzhe-lz commented Nov 6, 2019

Sorry, I didn't notice there is an allow_nan parameter.
Maybe you can mention it in README? I think I'm not the only one who ignore readthedoc documentation of a library with really simple APIs like this...

@mverleg
Copy link
Owner

mverleg commented Nov 6, 2019

Glad you found it! I'm never really sure which parts of the API I should and shouldn't add to the README, I think everything is too much...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants