# HG changeset patch # User Bob Ippolito <bob@redivi.com> # Date 1142644908 0 # Sat Mar 18 01:21:48 2006 +0000 # Node ID 3af09031db962576b79749600338184f1ec6f150 # Parent 59699461a5bb55b34449d0b7c725153d400ef55c sort_keys from cce git-svn-id: http://simplejson.googlecode.com/svn/trunk@18 a4795897-2c25-0410-b006-0d3caba88fa1 diff --git a/simplejson/__init__.py b/simplejson/__init__.py --- a/simplejson/__init__.py +++ b/simplejson/__init__.py @@ -12,13 +12,15 @@ >>> import simplejson >>> simplejson.dumps(['foo', {'bar': ('baz', None, 1.0, 2)}]) - '["foo", {"bar":["baz", null, 1.0, 2]}]' + '["foo", {"bar": ["baz", null, 1.0, 2]}]' >>> print simplejson.dumps("\"foo\bar") "\"foo\bar" >>> print simplejson.dumps(u'\u1234') "\u1234" >>> print simplejson.dumps('\\') "\\" + >>> print simplejson.dumps({"c": 0, "b": 0, "a": 0}, sort_keys=True) + {"a": 0, "b": 0, "c": 0} >>> from StringIO import StringIO >>> io = StringIO() >>> simplejson.dump(['streaming API'], io) diff --git a/simplejson/encoder.py b/simplejson/encoder.py --- a/simplejson/encoder.py +++ b/simplejson/encoder.py @@ -90,18 +90,18 @@ implementation (to raise ``TypeError``). """ __all__ = ['__init__', 'default', 'encode', 'iterencode'] - def __init__(self, skipkeys=False, ensure_ascii=True, check_circular=True, - allow_nan=True): + def __init__(self, skipkeys=False, ensure_ascii=True, + check_circular=True, allow_nan=True, sort_keys=False): """ Constructor for JSONEncoder, with sensible defaults. - + If skipkeys is False, then it is a TypeError to attempt encoding of keys that are not str, int, long, float or None. If skipkeys is True, such items are simply skipped. If ensure_ascii is True, the output is guaranteed to be str - objects with all incoming unicode characters escaped. If ensure_ascii - is false, the output will be unicode object. + objects with all incoming unicode characters escaped. If + ensure_ascii is false, the output will be unicode object. If check_circular is True, then lists, dicts, and custom encoded objects will be checked for circular references during encoding to @@ -112,12 +112,17 @@ encoded as such. This behavior is not JSON specification compliant, but is consistent with most JavaScript based encoders and decoders. Otherwise, it will be a ValueError to encode such floats. + + 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. """ - + self.skipkeys = skipkeys self.ensure_ascii = ensure_ascii self.check_circular = check_circular self.allow_nan = allow_nan + self.sort_keys = sort_keys def _iterencode_list(self, lst, markers=None): if not lst: @@ -157,7 +162,13 @@ else: encoder = encode_basestring allow_nan = self.allow_nan - for key, value in dct.iteritems(): + if self.sort_keys: + keys = dct.keys() + keys.sort() + items = [(k,dct[k]) for k in keys] + else: + items = dct.iteritems() + for key, value in items: if isinstance(key, basestring): pass # JavaScript is weakly typed for these, so it makes sense to