Skip to content

Commit d61e84b

Browse files
committed
gh-137533:make a section about json key convertion,then make some ref
1 parent 3d0b304 commit d61e84b

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

Doc/library/json.rst

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,10 @@ Basic Usage
259259
table <py-to-json-table>`. The arguments have the same meaning as in
260260
:func:`dump`.
261261

262+
.. note::
263+
264+
The encoder dose not preserve the types of dictionary keys in Python.Read more at :ref:`json-key-convertion`
265+
262266
.. function:: load(fp, *, cls=None, object_hook=None, parse_float=None, \
263267
parse_int=None, parse_constant=None, \
264268
object_pairs_hook=None, **kw)
@@ -360,6 +364,10 @@ Basic Usage
360364
.. versionchanged:: 3.9
361365
The keyword argument *encoding* has been removed.
362366

367+
.. note::
368+
369+
The decoder preserves the keys in JSON text as :class:`str`.Read more at :ref:`json-key-convertion`
370+
363371

364372
Encoders and Decoders
365373
---------------------
@@ -573,6 +581,28 @@ Encoders and Decoders
573581
for chunk in json.JSONEncoder().iterencode(bigobject):
574582
mysocket.write(chunk)
575583

584+
.. _json-key-convertion:
585+
586+
JSON Key Convertion
587+
^^^^^^^^^^^^^^^^^^^
588+
589+
:rfc:`7159` requires that keys in key/value pairs of JSON are always of the
590+
type :class:`str`. When a dictionary is converted into JSON, all the keys
591+
of the dictionary arecoerced to strings.When a JSON object is converted into
592+
dictionaries,all the keys of the dictionary are strings.
593+
For example:
594+
595+
>>> import json
596+
>>> foo={1:"spam"}
597+
>>> result=json.dumps(foo)
598+
>>> print(result)
599+
{"1": "spam"}
600+
>>> print(foo)
601+
{1: 'spam'}
602+
>>> print(foo==result)
603+
False
604+
605+
It can be seen that non-string keys are converted into strings after being encoded as JSON
576606

577607
Exceptions
578608
----------

0 commit comments

Comments
 (0)