Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Proper or custom JSON serialization of non-finite float values #98306

Open
Labels
stdlibPython modules in the Lib dirtype-featureA feature request or enhancement
@Dzeri96

Description

@Dzeri96

Feature or enhancement

The goal of this feature is to allow the JSON serializer in stdlib to serialize non-finite values (NaN, Inf, -Inf) according to theJSON specification. Going beyond just conforming to the spec, we could allow for custom serialization behavior.

Previous discussion

This problem was previously discussed in#84813, and arelated PR was submitted in 2019, however,@mdickinson suggested I open a fresh issue where we can discuss the implementation in-depth.

Pitch

Currently, python's default JSON serializer encodes values likeNaN as-is, with the explanation being that many JS-based JSON libraries also do this, and that the corresponding parsers can handle such non-conforming input.
In reality, most major browsers do not support this type of encoding and even NodeJS(v14.16.0) acts according to the previously-linked JSON spec.
The keyword argumentallow_nan makes the serializer throw when encountering non-finite values when set to true, but I'd argue it is paramount to ensure compatibility with the spec and modern browsers. Changing the default behavior is of course not needed or possible at this point.

When implementing this feature, there are two main decisions to make.

Firstly, it has to be decided ifallow_nan should be extended to take more datatypes like strings and callables, or if we should create a separate argument for this functionality.
Re-purposingallow_nan would make the control over such behavior centralized, however the name is very limiting.
It doesn't say anything about other non-finite values, and without looking at the docs, one would think it only takes bool values.

Secondly, it has to be decided how far we want to take this feature.
Do we want to have pre-defined cases likeas_is,throw, andto_null, or do we want to allow the user to pass their own callable? The latter is implemented by the linked PR. Having both options is also a possibility.

Overall, each combination of decisions has its advantages and drawbacks. Since I wasn't a part of such discussions before, I don't have a preference.
All I want is to see this feature get implemented, and I can create a PR once consensus is reached.

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibPython modules in the Lib dirtype-featureA feature request or enhancement

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp