This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Supporting customization of float encoding in JSON
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.11
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: clleew, eric.araujo, ezio.melotti, mark.dickinson, minrk, mitar, rhettinger
Priority: normal Keywords: patch

Created on 2019-05-07 20:32 by mitar, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 13233 open clleew, 2019-05-10 01:55
PR 28648 open minrk, 2021-09-30 11:08
Messages (6)
msg341817 - (view) Author: Mitar (mitar) * Date: 2019-05-07 20:32
Currently, there is only one argument which allows customization how float numbers are encoded in JSON: allow_nan. But this does not allow one to hook into the encoding of floating points really. The JSONEncoder is not called for float numbers.

The motivation here is that we would like to encode NaN and Infinity values differently, instead of non-standard approach and instead of raising an exception.

The "load" counterpart has "parse_float" which one can use to hook into parsing floats. I would suggest something similar, maybe "encode_float" which if provided would be used instead of the default.
msg341904 - (view) Author: Wei Lee (clleew) * Date: 2019-05-08 16:29
If no one is working on it, I'd like to give it a try.
msg341914 - (view) Author: Mitar (mitar) * Date: 2019-05-08 17:13
That would be awesome!

BTW, just as an additional example, JavaScrpt's JSON.stringify encodes NaN and Infinity to null. By having a custom function I could for example try to match such implementation.
msg342350 - (view) Author: Wei Lee (clleew) * Date: 2019-05-13 15:57
I've sent a PR for it.
https://github.com/python/cpython/pull/13233
msg393441 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2021-05-11 02:34
I think this should be discussed on the mailing list or Discourse, to get use cases and needs.
msg402950 - (view) Author: Min RK (minrk) * Date: 2021-09-30 11:24
We just ran into this in Jupyter where we've removed a pre-processing step for data structures passed to json.dumps, which took care of this, but was expensive https://github.com/jupyter/jupyter_client/pull/706

My expectation was that our `default` would be called for the unsupported value, but it isn't. I see the PR proposes a new option, but would it be sensible to use the already-existing `default` callback for this? It seems like what `default` is for.
History
Date User Action Args
2022-04-11 14:59:14adminsetgithub: 81022
2021-09-30 12:47:51mark.dickinsonsetnosy: + mark.dickinson
2021-09-30 11:24:43minrksetnosy: rhettinger, ezio.melotti, eric.araujo, mitar, minrk, clleew
messages: + msg402950
2021-09-30 11:08:32minrksetnosy: + minrk
pull_requests: + pull_request27016
2021-05-11 02:34:58eric.araujosetnosy: + eric.araujo

messages: + msg393441
versions: + Python 3.11, - Python 3.8
2019-05-13 15:57:46clleewsetmessages: + msg342350
2019-05-10 01:55:50clleewsetkeywords: + patch
stage: patch review
pull_requests: + pull_request13143
2019-05-08 17:13:30mitarsetmessages: + msg341914
2019-05-08 16:29:49clleewsetnosy: + clleew
messages: + msg341904
2019-05-07 20:35:14SilentGhostsetnosy: + rhettinger, ezio.melotti

type: enhancement
versions: + Python 3.8
2019-05-07 20:32:33mitarcreate