diff -r 2c27093fd11f Doc/library/json.rst --- a/Doc/library/json.rst Mon Apr 30 06:20:37 2012 +0200 +++ b/Doc/library/json.rst Mon Apr 30 08:34:13 2012 +0200 @@ -112,7 +112,7 @@ Using json.tool from the shell to valida Basic Usage ----------- -.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, **kw) +.. function:: dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw) Serialize *obj* as a JSON formatted stream to *fp* (a ``.write()``-supporting file-like object). @@ -156,8 +156,11 @@ Basic Usage :meth:`default` method to serialize additional types), specify it with the *cls* kwarg; otherwise :class:`JSONEncoder` is used. + If *sort_keys* is true, then the output of dictionaries will be sorted by key; + this is useful for regression tests to ensure that JSON serializations can be + compared on a day-to-day basis. -.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, **kw) +.. function:: dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, default=None, sort_keys=False, **kw) Serialize *obj* to a JSON formatted :class:`str`. The arguments have the same meaning as in :func:`dump`. diff -r 2c27093fd11f Lib/json/__init__.py --- a/Lib/json/__init__.py Mon Apr 30 06:20:37 2012 +0200 +++ b/Lib/json/__init__.py Mon Apr 30 08:34:13 2012 +0200 @@ -122,7 +122,7 @@ from .encoder import JSONEncoder def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, - default=None, **kw): + default=None, sort_keys=False, **kw): """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a ``.write()``-supporting file-like object). @@ -159,7 +159,11 @@ def dump(obj, fp, skipkeys=False, ensure ``.default()`` method to serialize additional types), specify it with the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. + If sort_keys is true, then the output of dictionaries will be + sorted by key; this is useful for regression tests to ensure + that JSON serializations can be compared on a day-to-day basis. """ + kw['sort_keys'] = sort_keys # cached encoder if (not skipkeys and ensure_ascii and check_circular and allow_nan and @@ -181,7 +185,7 @@ def dump(obj, fp, skipkeys=False, ensure def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, - default=None, **kw): + default=None, sort_keys=False, **kw): """Serialize ``obj`` to a JSON formatted ``str``. If ``skipkeys`` is false then ``dict`` keys that are not basic types @@ -217,7 +221,11 @@ def dumps(obj, skipkeys=False, ensure_as ``.default()`` method to serialize additional types), specify it with the ``cls`` kwarg; otherwise ``JSONEncoder`` is used. + If sort_keys is true, then the output of dictionaries will be + sorted by key; this is useful for regression tests to ensure + that JSON serializations can be compared on a day-to-day basis. """ + kw['sort_keys'] = sort_keys # cached encoder if (not skipkeys and ensure_ascii and check_circular and allow_nan and