diff options
author | Bob Ippolito <bob@redivi.com> | 2007-01-18 07:37:06 +0000 |
---|---|---|
committer | Bob Ippolito <bob@redivi.com> | 2007-01-18 07:37:06 +0000 |
commit | c4d8230e19cabd7e582f8d3189208dfade7ea0c1 (patch) | |
tree | edb47a2b5bb52add231d346db3c6bb1a2c73ffe5 | |
parent | 08cbfbd5bab16f4802a64b23d08f036a51319bdc (diff) | |
download | simplejson-c4d8230e19cabd7e582f8d3189208dfade7ea0c1.tar.gz |
expose separators
git-svn-id: http://simplejson.googlecode.com/svn/trunk@36 a4795897-2c25-0410-b006-0d3caba88fa1
-rw-r--r-- | simplejson/__init__.py | 39 |
1 files changed, 31 insertions, 8 deletions
diff --git a/simplejson/__init__.py b/simplejson/__init__.py index 6167779..5c79ccf 100644 --- a/simplejson/__init__.py +++ b/simplejson/__init__.py @@ -27,6 +27,21 @@ Encoding basic Python object hierarchies:: >>> 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 @@ Extending JSONEncoder:: ['[', '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 @@ def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, 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 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, 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 @@ def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, """ 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): """ |