Skip to content

gh-137533: document key type coercion limitations in json.loads #137545

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
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

Rov-Waff
Copy link

@Rov-Waff Rov-Waff commented Aug 8, 2025

…bility of key type coercion limitation in json module docs
@python-cla-bot
Copy link

python-cla-bot bot commented Aug 8, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@StanFromIreland
Copy link
Member

Please do not use the Update Branch button unless necessary (e.g. fixing conflicts, jogging the CI, or very old PRs) as it uses valuable resources. For more information see the devguide.

Thanks for StanFromIreland's suggestion

Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com>
Comment on lines 372 to 374
For Example,
``json.loads('{"42":"value"}')`` returns the dictionary
``{'42': 'value'}``.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't totally see how this example is related to the note. I think it's fine to just have the two sentences without the example.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm.this note is for beginners,they may think loads JSON text like {"42":"spam"} will get {42:'spam'}

@ZeroIntensity ZeroIntensity added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Aug 9, 2025
@picnixz picnixz changed the title gh-137533:Add a note under function:: loads() to Improve visibility… gh-137533: Add a note under function:: loads() to Improve visibility… Aug 9, 2025
@picnixz picnixz changed the title gh-137533: Add a note under function:: loads() to Improve visibility… gh-137533: document key type coercion limitations in json.loads Aug 9, 2025
Rov-Waff and others added 2 commits August 9, 2025 16:29
Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@Rov-Waff
Copy link
Author

Rov-Waff commented Aug 9, 2025

Now I have added note in the module-level documentation,need we nuke the notes under function:: dumps and function:: loads

@@ -22,7 +22,6 @@ is a lightweight data interchange format inspired by
The term "object" in the context of JSON processing in Python can be
ambiguous. All values in Python are objects. In JSON, an object refers to
any data wrapped in curly braces, similar to a Python dictionary.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please undo unrelated changes.

@@ -146,6 +145,13 @@ See :ref:`json-commandline` for detailed documentation.
This module's encoders and decoders preserve input and output order by
default. Order is only lost if the underlying containers are unordered.

.. note::
According to RFC 7159, the keys of all objects in JSON are strings.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can link the RFC as it was done previously.

@Rov-Waff
Copy link
Author

Rov-Waff commented Aug 9, 2025

Now I am thinking whether putting this note above "Basic Usage" Part is suitable

@@ -146,6 +146,13 @@ See :ref:`json-commandline` for detailed documentation.
This module's encoders and decoders preserve input and output order by
default. Order is only lost if the underlying containers are unordered.

.. note::
According to :rfc:`7159`, the keys of all objects in JSON are strings.
Copy link
Member

@picnixz picnixz Aug 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is too long and we have now three places where this kind of mention occurs. Here, in json.dumps() and in json.loads(). Let's avoid three different formulations and let's do as follows:

  • Remove the new note in json.loads().
  • Remove the note in json.dumps() and move it here (you can start this note by "RFC ... requires JSON keys to be strings".
  • You can add an example for json.dumps() and json.loads() which shows what happens when we dump or load something with an numeric python key.

into JSON and then back into a dictionary, the dictionary may not equal
the original one. That is, ``loads(dumps(x)) != x`` if x has non-string
keys.
The encoder will convert all non-string keys in dictionaries into string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the note in json.dumps()

@picnixz
Copy link
Member

picnixz commented Aug 10, 2025

We're stil lacking the:

You can add an example for json.dumps() and json.loads() which shows what happens when we dump or load something with an numeric python key.

I wanted you to remove the redundant part, not the entire part, sorry if I was unclear. We may also avoid using a note and just write it directly in the text. I'm leaving this afternoon so I won't be able to shepherd this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting review docs Documentation in the Doc dir needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes skip news
Projects
Status: Todo
Development

Successfully merging this pull request may close these issues.

Improve visibility of key type coercion limitation in json module docs
4 participants