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

enable custom serialization of non-finite floats #158

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

Conversation

randomir
Copy link

@randomir randomir commented Mar 2, 2017

Call user-provided default() to serialize non-finite floats (inf,
-inf, nan) if allow_nan=False.

Breaking change(?): if default() not provided, and
allow_nan=False, fail with::

TypeError: inf is not JSON serializable

instead of with ValueError: ....

Python implementation only. C speedups impl pending.
Added tests for the new use case.

Call user-provided ``default()`` to serialize non-finite floats (inf,
-inf, nan) if ``allow_nan=False``.

Breaking change(?): if ``default()`` not provided, and
``allow_nan=False``, fail with::

    TypeError: inf is not JSON serializable

instead of with ``ValueError: ...``.

Python implementation only. C speedups impl pending.
Added tests for the new use case.
@akshaybabloo
Copy link

Any update on this?

@randomir
Copy link
Author

I would love to hear what @etrepum thinks about this. The issue describing the feature is #159, and this is the initial, Python only, implementation. I didn't want to go into the guts of C code before I got some feedback on the idea. The tests were (at the time of PR submit) failing for cases when C extensions was used, if I remember correctly.

@etrepum
Copy link
Member

etrepum commented Aug 24, 2018

This would be a bit inconsistent with how default works elsewhere, in that here we have a few particular values of a type that are considered by the default function, rather than all values for that type. I'd like to hear about some use cases for this feature to see if it would justify the additional complexity it would add.

@randomir
Copy link
Author

If I remember correctly, all primary JSON types (string, number, array, object) are handled explicitly by simplejson, and default user function is never called. Default function is called only on unserializable types. float is usually serializable, and when it is, it should be serialized by simplejson (like other primary types are). Sometimes it's not (particularly when allow_nan=False, and strict adherence to JSON is requested). I'm proposing that in those cases - when float is not serializable - user supplied default function is called.

In that sense, this seems to be "a missing piece" in order to have consistent serialization of non-standard JSON types. Integer numbers, booleans, null, strings, lists and dicts (at least on container level) are always serializable. Floats are the only exception. (Inf/NaN in Decimal are indirectly handled by the same float processor.)

The use case? I was trying to support JSON-compatible serialization of non-finite floats in my library jsonplus.

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