diff options
Diffstat (limited to 'simplejson/__init__.py')
-rw-r--r-- | simplejson/__init__.py | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/simplejson/__init__.py b/simplejson/__init__.py index 3de54de..eb38b46 100644 --- a/simplejson/__init__.py +++ b/simplejson/__init__.py @@ -97,11 +97,11 @@ Using simplejson.tool from the shell to validate and pretty-print:: $ echo '{ 1.2:3.4}' | python -m simplejson.tool Expecting property name: line 1 column 2 (char 2) """ -__version__ = '2.4.0' +__version__ = '2.5.0' __all__ = [ 'dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONDecodeError', 'JSONEncoder', - 'OrderedDict', 'simple_first' + 'OrderedDict', 'simple_first', ] __author__ = 'Bob Ippolito <bob@redivi.com>' @@ -146,7 +146,8 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None, use_decimal=True, namedtuple_as_object=True, tuple_as_array=True, - bigint_as_string=False, item_sort_key=None, **kw): + bigint_as_string=False, sort_keys=False, item_sort_key=None, + **kw): """Serialize ``obj`` as a JSON formatted stream to ``fp`` (a ``.write()``-supporting file-like object). @@ -203,7 +204,11 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, If specified, *item_sort_key* is a callable used to sort the items in each dictionary. This is useful if you want to sort items other than - in alphabetical order by key. + in alphabetical order by key. This option takes precedence over + *sort_keys*. + + If *sort_keys* is true (default: ``False``), the output of dictionaries + will be sorted by item. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with @@ -228,6 +233,7 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, namedtuple_as_object=namedtuple_as_object, tuple_as_array=tuple_as_array, bigint_as_string=bigint_as_string, + sort_keys=sort_keys, item_sort_key=item_sort_key, **kw).iterencode(obj) # could accelerate with writelines in some versions of Python, at @@ -240,7 +246,7 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, cls=None, indent=None, separators=None, encoding='utf-8', default=None, use_decimal=True, namedtuple_as_object=True, tuple_as_array=True, - bigint_as_string=False, item_sort_key=None, + bigint_as_string=False, sort_keys=False, item_sort_key=None, **kw): """Serialize ``obj`` to a JSON formatted ``str``. @@ -287,13 +293,17 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, If *tuple_as_array* is true (default: ``True``), :class:`tuple` (and subclasses) will be encoded as JSON arrays. - If bigint_as_string is true (not the default), ints 2**53 and higher + If *bigint_as_string* is true (not the default), ints 2**53 and higher or lower than -2**53 will be encoded as strings. This is to avoid the rounding that happens in Javascript otherwise. If specified, *item_sort_key* is a callable used to sort the items in each dictionary. This is useful if you want to sort items other than - in alphabetical order by key. + in alphabetical order by key. This option takes precendence over + *sort_keys*. + + If *sort_keys* is true (default: ``False``), the output of dictionaries + will be sorted by item. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with @@ -306,7 +316,8 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, cls is None and indent is None and separators is None and encoding == 'utf-8' and default is None and use_decimal and namedtuple_as_object and tuple_as_array - and not bigint_as_string and not item_sort_key and not kw): + and not bigint_as_string and not sort_keys + and not item_sort_key and not kw): return _default_encoder.encode(obj) if cls is None: cls = JSONEncoder @@ -318,6 +329,7 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, namedtuple_as_object=namedtuple_as_object, tuple_as_array=tuple_as_array, bigint_as_string=bigint_as_string, + sort_keys=sort_keys, item_sort_key=item_sort_key, **kw).encode(obj) @@ -495,4 +507,4 @@ def simple_first(kv): """Helper function to pass to item_sort_key to sort simple elements to the top, then container elements. """ - return isinstance(kv[1],(list,dict,tuple)), kv[0] + return (isinstance(kv[1], (list, dict, tuple)), kv[0]) |