-
-
Notifications
You must be signed in to change notification settings - Fork 33.2k
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
base: main
Are you sure you want to change the base?
Changes from all commits
98ba5d5
dcfd3f3
1161ad0
56dfcfd
40d0cd1
9b6732b
8c9d81b
40c5678
541878a
e0dc7eb
1814bfa
f17de69
a4b8240
1a71527
da6e655
3d0b304
d61e84b
96f54a7
9bbdb5a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -146,7 +146,6 @@ 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. | ||
|
||
|
||
Basic Usage | ||
----------- | ||
|
||
|
@@ -256,12 +255,8 @@ Basic Usage | |
|
||
.. note:: | ||
|
||
Keys in key/value pairs of JSON are always of the type :class:`str`. When | ||
a dictionary is converted into JSON, all the keys of the dictionary are | ||
coerced to strings. As a result of this, if a dictionary is converted | ||
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 dose not preserve the types of dictionary keys in Python. | ||
Read more at :ref:`json-key-convertion` | ||
|
||
.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, \ | ||
parse_int=None, parse_constant=None, \ | ||
|
@@ -364,6 +359,11 @@ Basic Usage | |
.. versionchanged:: 3.9 | ||
The keyword argument *encoding* has been removed. | ||
|
||
.. note:: | ||
StanFromIreland marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The decoder preserves the keys in JSON text as :class:`str`. | ||
Read more at :ref:`json-key-convertion` | ||
|
||
|
||
Encoders and Decoders | ||
--------------------- | ||
|
@@ -577,6 +577,28 @@ Encoders and Decoders | |
for chunk in json.JSONEncoder().iterencode(bigobject): | ||
mysocket.write(chunk) | ||
|
||
.. _json-key-convertion: | ||
|
||
JSON Key Convertion | ||
^^^^^^^^^^^^^^^^^^^ | ||
|
||
:rfc:`7159` requires that keys in key/value pairs of JSON are always of the | ||
type :class:`str`. When a dictionary is converted into JSON, all the keys | ||
of the dictionary arecoerced to strings.When a JSON object is converted into | ||
dictionaries,all the keys of the dictionary are strings. | ||
For example: | ||
|
||
>>> import json | ||
>>> foo={1:"spam"} | ||
>>> result=json.dumps(foo) | ||
>>> print(result) | ||
{"1": "spam"} | ||
>>> print(foo) | ||
{1: 'spam'} | ||
>>> print(foo==result) | ||
False | ||
|
||
It can be seen that non-string keys are converted into strings after being encoded as JSON | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This example needs to be improved. To begin with, it should comply PEP 8. But the main issue is that you're comparing a string with a dictionary, of course they will be different. You need to also Probably the example will be more clear if you use more explicit names. In short, it would be better to have:
|
||
|
||
Exceptions | ||
---------- | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Revert unrelated changes.