enable custom serialization of non-finite floats#158
Conversation
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.
|
Any update on this? |
|
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. |
|
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. |
|
If I remember correctly, all primary JSON types (string, number, array, object) are handled explicitly by 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 The use case? I was trying to support JSON-compatible serialization of non-finite floats in my library |
Call user-provided
default()to serialize non-finite floats (inf,-inf, nan) if
allow_nan=False.Breaking change(?): if
default()not provided, andallow_nan=False, fail with::instead of with
ValueError: ....Python implementation only. C speedups impl pending.
Added tests for the new use case.