diff --git a/simplejson/__init__.py b/simplejson/__init__.py index 7847c64f22eced4627e25cfbf9e08aeb8c2c5d1f_c2ltcGxlanNvbi9fX2luaXRfXy5weQ==..7186e3f6265392fe5101ab4e2e7a57e4cca0be5e_c2ltcGxlanNvbi9fX2luaXRfXy5weQ== 100644 --- a/simplejson/__init__.py +++ b/simplejson/__init__.py @@ -27,6 +27,21 @@ >>> io.getvalue() '["streaming API"]' +Compact encoding:: + + >>> import simplejson + >>> simplejson.dumps([1,2,3,{'4': 5, '6': 7}], separators=(',',':')) + '[1,2,3,{"4":5,"6":7}]' + +Pretty printing:: + + >>> import simplejson + >>> print simplejson.dumps({'4': 5, '6': 7}, sort_keys=True, indent=4) + { + "4": 5, + "6": 7 + } + Decoding JSON:: >>> import simplejson @@ -68,8 +83,8 @@ ['[', '2.0', ', ', '1.0', ']'] -Note that the JSON produced by this module is a subset of YAML, -so it may be used as a serializer for that as well. +Note that the JSON produced by this module's default settings +is a subset of YAML, so it may be used as a serializer for that as well. """ __version__ = '1.5' __all__ = [ @@ -124,7 +139,7 @@ fp.write(chunk) def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, - allow_nan=True, cls=None, indent=None, **kw): + allow_nan=True, cls=None, indent=None, separators=None, **kw): """ Serialize ``obj`` to a JSON formatted ``str``. @@ -145,9 +160,14 @@ strict compliance of the JSON specification, instead of using the JavaScript equivalents (``NaN``, ``Infinity``, ``-Infinity``). - If ``indent`` is a non-negative integer, then JSON array elements and object - members will be pretty-printed with that indent level. An indent level - of 0 will only insert newlines. ``None`` is the most compact representation. + If ``indent`` is a non-negative integer, then JSON array elements and + object members will be pretty-printed with that indent level. An indent + level of 0 will only insert newlines. ``None`` is the most compact + representation. + + If ``separators`` is an ``(item_separator, dict_separator)`` tuple + then it will be used instead of the default ``(', ', ': ')`` separators. + ``(',', ':')`` is the most compact JSON representation. To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with @@ -155,8 +175,11 @@ """ if cls is None: cls = JSONEncoder - return cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, - check_circular=check_circular, allow_nan=allow_nan, indent=indent, **kw).encode(obj) + return cls( + skipkeys=skipkeys, ensure_ascii=ensure_ascii, + check_circular=check_circular, allow_nan=allow_nan, indent=indent, + separators=separators, + **kw).encode(obj) def load(fp, encoding=None, cls=None, object_hook=None, **kw): """