10000 Merge issue #14570: Document json sort_keys parameter properly. · python/cpython@42b8932 · GitHub
[go: up one dir, main page]

Skip to content

Commit 42b8932

Browse files
committed
Merge issue #14570: Document json sort_keys parameter properly.
Patch by Chris Rebert.
2 parents 61254b9 + 6d50a54 commit 42b8932

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

Doc/library/json.rst

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,10 @@ Using json.tool from the shell to validate and pretty-print::
116116
Basic Usage
117117
-----------
118118

119-
.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, **kw)
119+
.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, \
120+
check_circular=True, allow_nan=True, cls=None, \
121+
indent=None, separators=None, default=None, \
122+
sort_keys=False, **kw)
120123
121124
Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting
122125
:term:`file-like object`).
@@ -159,12 +162,18 @@ Basic Usage
159162
*default(obj)* is a function that should return a serializable version of
160163
*obj* or raise :exc:`TypeError`. The default simply raises :exc:`TypeError`.
161164

165+
If *sort_keys* is ``True`` (default: ``False``), then the output of
166+
dictionaries will be sorted by key.
167+
162168
To use a custom :class:`JSONEncoder` subclass (e.g. one that overrides the
163169
:meth:`default` method to serialize additional types), specify it with the
164170
*cls* kwarg; otherwise :class:`JSONEncoder` is used.
165171

166172

167-
.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, **kw)
173+
.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, \
174+
check_circular=True, allow_nan=True, cls=None, \
175+
indent=None, separators=None, default=None, \
176+
sort_keys=False, **kw)
168177
169178
Serialize *obj* to a JSON formatted :class:`str`. The arguments have the
170179
same meaning as in :func:`dump`.

Lib/json/__init__.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@
122122

123123
def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
124124
allow_nan=True, cls=None, indent=None, separators=None,
125-
default=None, **kw):
125+
default=None, sort_keys=False, **kw):
126126
"""Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
127127
``.write()``-supporting file-like object).
128128
@@ -155,6 +155,9 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
155155
``default(obj)`` is a function that should return a serializable version
156156
of obj or raise TypeError. The default simply raises TypeError.
157157
158+
If *sort_keys* is ``True`` (default: ``False``), then the output of
159+
dictionaries will be sorted by key.
160+
158161
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
159162
``.default()`` method to serialize additional types), specify it with
160163
the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
@@ -164,15 +167,15 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
164167
if (not skipkeys and ensure_ascii and
165168
check_circular and allow_nan and
166169
cls is None and indent is None and separators is None and
167-
default is None and not kw):
170+
default is None and not sort_keys and not kw):
168171
iterable = _default_encoder.iterencode(obj)
169172
else:
170173
if cls is None:
171174
cls = JSONEncoder
172175
iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii,
173176
check_circular=check_circular, allow_nan=allow_nan, indent=indent,
174177
separators=separators,
175-
default=default, **kw).iterencode(obj)
178+
default=default, sort_keys=sort_keys, **kw).iterencode(obj)
176179
# could accelerate with writelines in some versions of Python, at
177180
# a debuggability cost
178181
for chunk in iterable:
@@ -181,7 +184,7 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
181184

182185
def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
183186
allow_nan=True, cls=None, indent=None, separators=None,
184-
default=None, **kw):
187+
default=None, sort_keys=False, **kw):
185188
"""Serialize ``obj`` to a JSON formatted ``str``.
186189
187190
If ``skipkeys`` is false then ``dict`` keys that are not basic types
@@ -213,6 +216,9 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
213216
``default(obj)`` is a function that should return a serializable version
214217
of obj or raise TypeError. The default simply raises TypeError.
215218
219+
If *sort_keys* is ``True`` (default: ``False``), then the output of
220+
dictionaries will be sorted by key.
221+
216222
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
217223
``.default()`` method to serialize additional types), specify it with
218224
the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
@@ -222,14 +228,14 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
222228
if (not skipkeys and ensure_ascii and
223229
check_circular and allow_nan and
224230
cls is None and indent is None and separators is None and
225-
default is None and not kw):
231+
default is None and not sort_keys and not kw):
226232
return _default_encoder.encode(obj)
227233
if cls is None:
228234
cls = JSONEncoder
229235
return cls(
230236
skipkeys=skipkeys, ensure_ascii=ensure_ascii,
231237
check_circular=check_circular, allow_nan=allow_nan, indent=indent,
232-
separators=separators, default=default,
238+
separators=separators, default=default, sort_keys=sort_keys,
233239
**kw).encode(obj)
234240

235241

0 commit comments

Comments
 (0)
0