Issue 36841: Supporting customization of float encoding in JSON (original) (raw)

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:14 admin set github: 81022
2021-09-30 12:47:51 mark.dickinson set nosy: + mark.dickinson
2021-09-30 11:24:43 minrk set nosy:rhettinger, ezio.melotti, eric.araujo, mitar, minrk, clleewmessages: +
2021-09-30 11:08:32 minrk set nosy: + minrkpull_requests: + <pull%5Frequest27016>
2021-05-11 02:34:58 eric.araujo set nosy: + eric.araujomessages: + versions: + Python 3.11, - Python 3.8
2019-05-13 15:57:46 clleew set messages: +
2019-05-10 01:55:50 clleew set keywords: + patchstage: patch reviewpull_requests: + <pull%5Frequest13143>
2019-05-08 17:13:30 mitar set messages: +
2019-05-08 16:29:49 clleew set nosy: + clleewmessages: +
2019-05-07 20:35:14 SilentGhost set nosy: + rhettinger, ezio.melottitype: enhancementversions: + Python 3.8
2019-05-07 20:32:33 mitar create