8000 Supporting customization of float encoding in JSON · Issue #81022 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

Supporting customization of float encoding in JSON #81022

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

Open
mitar mannequin opened this issue May 7, 2019 · 11 comments
Open

Supporting customization of float encoding in JSON #81022

mitar mannequin opened this issue May 7, 2019 · 11 comments
Labels
3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@mitar
Copy link
Mannequin
mitar mannequin commented May 7, 2019
BPO 36841
Nosy @rhettinger, @mdickinson, @ezio-melotti, @merwok, @mitar, @minrk, @Lee-W
PRs
  • gh-81022: Supporting customization of float encoding in JSON #13233
  • bpo-36841: JSONEncoder call self.default for unsupported floats #28648
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2019-05-07.20:32:33.357>
    labels = ['type-feature', 'library', '3.11']
    title = 'Supporting customization of float encoding in JSON'
    updated_at = <Date 2021-09-30.12:47:51.360>
    user = 'https://github.com/mitar'

    bugs.python.org fields:

    activity = <Date 2021-09-30.12:47:51.360>
    actor = 'mark.dickinson'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2019-05-07.20:32:33.357>
    creator = 'mitar'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 36841
    keywords = ['patch']
    message_count = 6.0
    messages = ['341817', '341904', '341914', '342350', '393441', '402950']
    nosy_count = 7.0
    nosy_names = ['rhettinger', 'mark.dickinson', 'ezio.melotti', 'eric.araujo', 'mitar', 'minrk', 'clleew']
    pr_nums = ['13233', '28648']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = 'enhancement'
    url = 'https://bugs.python.org/issue36841'
    versions = ['Python 3.11']

    @mitar
    Copy link
    Mannequin Author
    mitar mannequin commented May 7, 2019

    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.

    @mitar mitar mannequin added the stdlib Python modules in the Lib dir label May 7, 2019
    @SilentGhost SilentGhost mannequin added 3.8 (EOL) end of life type-feature A feature request or enhancement labels May 7, 2019
    @Lee-W
    Copy link
    Mannequin
    Lee-W mannequin commented May 8, 2019

    If no one is working on it, I'd like to give it a try.

    @mitar
    Copy link
    Mannequin Author
    mitar mannequin commented May 8, 2019

    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.

    @Lee-W
    Copy link
    Mannequin
    Lee-W mannequin commented May 13, 2019

    I've sent a PR for it.
    #13233

    @merwok
    Copy link
    Member
    merwok commented May 11, 2021

    I think this should be discussed on the mailing list or Discourse, to get use cases and needs.

    @merwok merwok added 3.11 only security fixes and removed 3.8 (EOL) end of life labels May 11, 2021
    @minrk
    Copy link
    Mannequin
    minrk mannequin commented Sep 30, 2021

    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 jupyter/jupyter_client#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.

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    @arhadthedev arhadthedev added 3.13 bugs and security fixes and removed 3.11 only security fixes labels Jun 4, 2023
    @arhadthedev
    Copy link
    Member

    @Lee-W (as an author of a child PR) The Discourse is https://discuss.python.org/.

    @mdickinson
    Copy link
    Member

    @oren-hecht
    Copy link
    oren-hecht commented Feb 4, 2024

    Is there any updates on this issue?

    the fact that the standard json library is incompatible with pretty much all default Javascript parsers seems like a pretty big deal - even if it is only with non-finite float values..

    I think the solution proposed in the aforementioned thread can definitely be a decent solution!

    @mdickinson
    Copy link
    Member

    Yes, @encukou's second option in this message still seems like a good resolution to me. That is:

    • allow the existing allow_nan parameter to take the value "null" (and in that case, convert both infinities and NaNs to JSON null)
    • also deprecate non-boolean, non-"null" values for allow_nan

    I'm not likely to have cycles to work on this any time soon, but would be willing to review a PR.

    @mdickinson
    Copy link
    Member

    See #115246.

    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.13 bugs and security fixes stdlib Python modules in the Lib dir type-feature A feature request or enhancement
    Projects
    Status: No status
    Development

    No branches or pull requests

    4 participants
    0