diff --git a/simplejson/__init__.py b/simplejson/__init__.py index 59699461a5bb55b34449d0b7c725153d400ef55c_c2ltcGxlanNvbi9fX2luaXRfXy5weQ==..3af09031db962576b79749600338184f1ec6f150_c2ltcGxlanNvbi9fX2luaXRfXy5weQ== 100644 --- a/simplejson/__init__.py +++ b/simplejson/__init__.py @@ -12,10 +12,10 @@ >>> 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('\\') "\\" @@ -16,9 +16,11 @@ >>> 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 index 59699461a5bb55b34449d0b7c725153d400ef55c_c2ltcGxlanNvbi9lbmNvZGVyLnB5..3af09031db962576b79749600338184f1ec6f150_c2ltcGxlanNvbi9lbmNvZGVyLnB5 100644 --- a/simplejson/encoder.py +++ b/simplejson/encoder.py @@ -90,7 +90,7 @@ 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. @@ -95,8 +95,8 @@ """ 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 @@ -98,10 +98,10 @@ 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,4 +112,8 @@ 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. """ @@ -115,6 +119,6 @@ """ - + self.skipkeys = skipkeys self.ensure_ascii = ensure_ascii self.check_circular = check_circular self.allow_nan = allow_nan @@ -117,7 +121,8 @@ 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