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

Current JSON outputs for Infinity, -Infinity, and NaN are not valid JSON. New option to allow output them as String. #164

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

robermorales
Copy link

@robermorales robermorales commented May 3, 2017

I added a big_floats_as_string boolean option to allow producing valid JSON String values in case of Infinity, -Infinity or NaN.

It allows to parse them with a + also in JavaScript.

Tests included.

…SON String values in case of Infinity, -Infinity or NaN
@robermorales
Copy link
Author

Can we have some discussion on this topic? Has this pull request some options of being merged?

@etrepum
Copy link
Member

etrepum commented May 5, 2017

It's unlikely to get merged, just because simplejson already has too many options, and this has never been requested before. I do otherwise think it is a nice approach to the problem.

@robermorales
Copy link
Author

Thanks for your explanation.

Two of the five open pull requests are about serializing valid standard JSON. I think that following the standard definition was a requirement, anyway.

Thanks you a lot again for your great work on the library. Perhaps we will fork it to add this option, but the library is pretty good.

Regards,

@robermorales
Copy link
Author

Just for the record, I published the fork as validjson in pypi, in case someone is interested.

https://pypi.python.org/pypi/validjson

@etrepum
Copy link
Member

etrepum commented May 10, 2017

If you're going to do that, I would probably recommend that the default be to throw an exception (allow_nan=False) or encode those values with null like JavaScript does.

> JSON.stringify([+'Infinity', +'NaN'])
"[null,null]"

@robermorales
Copy link
Author

Yes, outputting null does the job of generating valid JSON, but it lacks the required goal of being able to recover the values back.

@etrepum
Copy link
Member

etrepum commented May 10, 2017

Sure, I'm just recommending a default that rejects them or does what JavaScript does.

@borzunov
Copy link

Hi @robermorales,

Thanks for creating this issue and the fork!

I had the same problem - I'm serializing a complex nested data structure with possible nan/infs, and it's difficult to replace all nan/infs manually before serializing. I was able to get away with simplejson's ignore_nan=True (so nan/infs get serialized as null), but I'm keeping an eye on your fork as well in case if consumers will have to distinguish between -infs/infs/nan.

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

Successfully merging this pull request may close these issues.

None yet

3 participants