122
122
123
123
def dump (obj , fp , skipkeys = False , ensure_ascii = True , check_circular = True ,
124
124
allow_nan = True , cls = None , indent = None , separators = None ,
125
- default = None , ** kw ):
125
+ default = None , sort_keys = False , ** kw ):
126
126
"""Serialize ``obj`` as a JSON formatted stream to ``fp`` (a
127
127
``.write()``-supporting file-like object).
128
128
@@ -155,6 +155,9 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
155
155
``default(obj)`` is a function that should return a serializable version
156
156
of obj or raise TypeError. The default simply raises TypeError.
157
157
158
+ If *sort_keys* is ``True`` (default: ``False``), then the output of
159
+ dictionaries will be sorted by key.
160
+
158
161
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
159
162
``.default()`` method to serialize additional types), specify it with
160
163
the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
@@ -164,15 +167,15 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
164
167
if (not skipkeys and ensure_ascii and
165
168
check_circular and allow_nan and
166
169
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 ):
168
171
iterable = _default_encoder .iterencode (obj )
169
172
else :
170
173
if cls is None :
171
174
cls = JSONEncoder
172
175
iterable = cls (skipkeys = skipkeys , ensure_ascii = ensure_ascii ,
173
176
check_circular = check_circular , allow_nan = allow_nan , indent = indent ,
174
177
separators = separators ,
175
- default = default , ** kw ).iterencode (obj )
178
+ default = default , sort_keys = sort_keys , ** kw ).iterencode (obj )
176
179
# could accelerate with writelines in some versions of Python, at
177
180
# a debuggability cost
178
181
for chunk in iterable :
@@ -181,7 +184,7 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True,
181
184
182
185
def dumps (obj , skipkeys = False , ensure_ascii = True , check_circular = True ,
183
186
allow_nan = True , cls = None , indent = None , separators = None ,
184
- default = None , ** kw ):
187
+ default = None , sort_keys = False , ** kw ):
185
188
"""Serialize ``obj`` to a JSON formatted ``str``.
186
189
187
190
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,
213
216
``default(obj)`` is a function that should return a serializable version
214
217
of obj or raise TypeError. The default simply raises TypeError.
215
218
219
+ If *sort_keys* is ``True`` (default: ``False``), then the output of
220
+ dictionaries will be sorted by key.
221
+
216
222
To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the
217
223
``.default()`` method to serialize additional types), specify it with
218
224
the ``cls`` kwarg; otherwise ``JSONEncoder`` is used.
@@ -222,14 +228,14 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True,
222
228
if (not skipkeys and ensure_ascii and
223
229
check_circular and allow_nan and
224
230
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 ):
226
232
return _default_encoder .encode (obj )
227
233
if cls is None :
228
234
cls = JSONEncoder
229
235
return cls (
230
236
skipkeys = skipkeys , ensure_ascii = ensure_ascii ,
231
237
check_circular = check_circular , allow_nan = allow_nan , indent = indent ,
232
- separators = separators , default = default ,
238
+ separators = separators , default = default , sort_keys = sort_keys ,
233
239
** kw ).encode (obj )
234
240
235
241
0 commit comments