10000 gh-98306: Support JSON encoding of NaNs and infinities as null by mdickinson · Pull Request #115246 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-98306: Support JSON encoding of NaNs and infinities as null #115246

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

Closed
wants to merge 12 commits into from
Closed
Prev Previous commit
Next Next commit
Documentation wording tweaks
  • Loading branch information
mdickinson committed May 12, 2024
commit ebcd36d347ec1dc47439b44516447e435b02a5ed
28 changes: 14 additions & 14 deletions Doc/library/json.rst
Original file line number Diff line number Diff line change
Expand Up @@ -178,8 +178,8 @@ Basic Usage
``JSON.stringify``. If *allow_nan* is true but not equal to ``'as_null'``
then NaNs and infinities are converted to non-quote-delimited strings
``NaN``, ``Infinity`` and ``-Infinity`` in the JSON output. Note that this
represents an extension of the JSON specification, and is not compliant with
standard JSON.
represents an extension of the JSON specification, and that the generated
output may not be accepted as valid JSON by third party JSON parsers.

If *indent* is a non-negative integer or string, then JSON array elements and
object members will be pretty-printed with that indent level. An indent level
Expand Down Expand Up @@ -216,7 +216,7 @@ Basic Usage

.. versionchanged:: 3.14
Added support for ``allow_nan='as_null'``. Passing any string value
other than ``'as_null'`` for ``allow_nan`` now raises a
other than ``'as_null'`` for ``allow_nan`` now triggers a
:warning:`DeprecationWarning`.

.. note::
Expand All @@ -225,7 +225,6 @@ Basic Usage
so trying to serialize multiple objects with repeated calls to
:func:`dump` using the same *fp* will result in an invalid JSON file.


.. function:: dumps(obj, *, skipkeys=False, ensure_ascii=True, \
check_circular=True, allow_nan=True, cls=None, \
indent=None, separators=None, default=None, \
Expand Down Expand Up @@ -461,15 +460,16 @@ Encoders and Decoders
prevent an infinite recursion (which would cause a :exc:`RecursionError`).
Otherwise, no such check takes place.

If *allow_nan* is the string ``'as_null'``, then NaNs and infinities are
encoded as JSON ``null`` values. This matches the behavior of JavaScript's
``JSON.stringify``. If *allow_nan* is true but not equal to ``'as_null'``,
then ``NaN``, ``Infinity``, and ``-Infinity`` will be encoded as
corresponding non-quote-delimited strings in the JSON output. This is the
default behavior. This behavior represents an extension of the JSON
specification, but is consistent with some JavaScript based encoders and
decoders (as well as Python's own decoder). If *allow_nan* is false, it
will be a :exc:`ValueError` to encode such floats.
If *allow_nan* is false (default: ``True``), then it will be a
:exc:`ValueError` to serialize out of range :class:`float` values (``nan``,
``inf``, ``-inf``) in strict compliance with the JSON specification. If
*allow_nan* is the string ``'as_null'``, NaNs and infinities will be
converted to a JSON ``null``, matching the behavior of JavaScript's
``JSON.stringify``. If *allow_nan* is true but not equal to ``'as_null'``
then NaNs and infinities are converted to non-quote-delimited strings
``NaN``, ``Infinity`` and ``-Infinity`` in the JSON output. Note that this
represents an extension of the JSON specification, and that the generated
output may not be accepted as valid JSON by third party JSON parsers.

If *sort_keys* is true (default: ``False``), then the output of dictionaries
will be sorted by key; this is useful for regression tests to ensure that
Expand Down Expand Up @@ -503,7 +503,7 @@ Encoders and Decoders

.. versionchanged:: 3.14
Added support for ``allow_nan='as_null'``. Passing any string value
other than ``'as_null'`` for *allow_nan* now raises a
other than ``'as_null'`` for *allow_nan* now triggers a
:warning:`DeprecationWarning`.

.. method:: default(o)
Expand Down
2 changes: 1 addition & 1 deletion Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ json
----

* Add support for ``allow_nan='as_null'`` when encoding to JSON. This converts
floating-point infinities and NaNs to a JSON ``null``, for compatibility
floating-point infinities and NaNs to a JSON ``null``, for alignment
with ECMAScript's ``JSON.stringify``.
(Contributed by Mark Dickinson in :gh:`115246`.)

Expand Down
Loading
0