122122
123123def 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
182185def 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