Skip to content

enable custom serialization of non-finite floats#158

Open
randomir wants to merge 1 commit into
simplejson:mainfrom
randomir:non-finite-numbers-custom-serialization
Open

enable custom serialization of non-finite floats#158
randomir wants to merge 1 commit into
simplejson:mainfrom
randomir:non-finite-numbers-custom-serialization

Conversation

@randomir

@randomir randomir commented Mar 2, 2017

Copy link
Copy Markdown

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
Copy Markdown

Any update on this?

@randomir

Copy link
Copy Markdown
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

etrepum commented Aug 24, 2018

Copy link
Copy Markdown
Member

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
Copy Markdown
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.

3 participants