diff --git a/setup.cfg b/setup.cfg index 4f58544c918ec3385f3f234341d8162c41ec12f3_c2V0dXAuY2Zn..ba010af8a92eb563d53e19a7b7d2d9a3a91dffc3_c2V0dXAuY2Zn 100644 --- a/setup.cfg +++ b/setup.cfg @@ -1,3 +1,3 @@ [egg_info] -#tag_build = dev -#tag_svn_revision = true +tag_build = dev +tag_svn_revision = true diff --git a/setup.py b/setup.py index 4f58544c918ec3385f3f234341d8162c41ec12f3_c2V0dXAucHk=..ba010af8a92eb563d53e19a7b7d2d9a3a91dffc3_c2V0dXAucHk= 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ from setuptools import setup, find_packages -VERSION = '1.3' +VERSION = '1.4' DESCRIPTION = "Simple, fast, extensible JSON encoder/decoder for Python" LONG_DESCRIPTION = """ simplejson is a simple, fast, complete, correct and extensible diff --git a/simplejson/__init__.py b/simplejson/__init__.py index 4f58544c918ec3385f3f234341d8162c41ec12f3_c2ltcGxlanNvbi9fX2luaXRfXy5weQ==..ba010af8a92eb563d53e19a7b7d2d9a3a91dffc3_c2ltcGxlanNvbi9fX2luaXRfXy5weQ== 100644 --- a/simplejson/__init__.py +++ b/simplejson/__init__.py @@ -71,7 +71,7 @@ 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. """ -__version__ = '1.3' +__version__ = '1.4' __all__ = [ 'dump', 'dumps', 'load', 'loads', 'JSONDecoder', 'JSONEncoder', @@ -81,7 +81,7 @@ from encoder import JSONEncoder def dump(obj, fp, skipkeys=False, ensure_ascii=True, check_circular=True, - allow_nan=True, cls=None, **kw): + allow_nan=True, cls=None, indent=None, **kw): """ Serialize ``obj`` as a JSON formatted stream to ``fp`` (a ``.write()``-supporting file-like object). @@ -105,6 +105,10 @@ in 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. + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with the ``cls`` kwarg. @@ -112,7 +116,7 @@ if cls is None: cls = JSONEncoder iterable = cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, - check_circular=check_circular, allow_nan=allow_nan, + check_circular=check_circular, allow_nan=allow_nan, indent=indent, **kw).iterencode(obj) # could accelerate with writelines in some versions of Python, at # a debuggability cost @@ -120,7 +124,7 @@ fp.write(chunk) def dumps(obj, skipkeys=False, ensure_ascii=True, check_circular=True, - allow_nan=True, cls=None, **kw): + allow_nan=True, cls=None, indent=None, **kw): """ Serialize ``obj`` to a JSON formatted ``str``. @@ -141,6 +145,10 @@ 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. + To use a custom ``JSONEncoder`` subclass (e.g. one that overrides the ``.default()`` method to serialize additional types), specify it with the ``cls`` kwarg. @@ -148,7 +156,7 @@ if cls is None: cls = JSONEncoder return cls(skipkeys=skipkeys, ensure_ascii=ensure_ascii, - check_circular=check_circular, allow_nan=allow_nan, **kw).encode(obj) + check_circular=check_circular, allow_nan=allow_nan, indent=indent, **kw).encode(obj) def load(fp, encoding=None, cls=None, object_hook=None, **kw): """ diff --git a/simplejson/encoder.py b/simplejson/encoder.py index 4f58544c918ec3385f3f234341d8162c41ec12f3_c2ltcGxlanNvbi9lbmNvZGVyLnB5..ba010af8a92eb563d53e19a7b7d2d9a3a91dffc3_c2ltcGxlanNvbi9lbmNvZGVyLnB5 100644 --- a/simplejson/encoder.py +++ b/simplejson/encoder.py @@ -91,7 +91,7 @@ """ __all__ = ['__init__', 'default', 'encode', 'iterencode'] def __init__(self, skipkeys=False, ensure_ascii=True, - check_circular=True, allow_nan=True, sort_keys=False): + check_circular=True, allow_nan=True, sort_keys=False, indent=None): """ Constructor for JSONEncoder, with sensible defaults. @@ -116,6 +116,11 @@ 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. + + 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. """ self.skipkeys = skipkeys @@ -123,6 +128,13 @@ self.check_circular = check_circular self.allow_nan = allow_nan self.sort_keys = sort_keys + self.indent = indent + self.current_indent_level = 0 + + def _newline_indent(self): + if self.indent is None: + return '' + return '\n' + (' ' * (self.indent * self.current_indent_level)) def _iterencode_list(self, lst, markers=None): if not lst: @@ -133,9 +145,11 @@ if markerid in markers: raise ValueError("Circular reference detected") markers[markerid] = lst - yield '[' + self.current_indent_level += 1 + newline_indent = self._newline_indent() + yield '[' + newline_indent first = True for value in lst: if first: first = False else: @@ -137,8 +151,8 @@ first = True for value in lst: if first: first = False else: - yield ', ' + yield ', ' + newline_indent for chunk in self._iterencode(value, markers): yield chunk @@ -143,6 +157,7 @@ for chunk in self._iterencode(value, markers): yield chunk - yield ']' + self.current_indent_level -= 1 + yield self._newline_indent() + ']' if markers is not None: del markers[markerid] @@ -155,7 +170,9 @@ if markerid in markers: raise ValueError("Circular reference detected") markers[markerid] = dct - yield '{' + self.current_indent_level += 1 + newline_indent = self._newline_indent() + yield '{' + newline_indent first = True if self.ensure_ascii: encoder = encode_basestring_ascii @@ -190,8 +207,8 @@ if first: first = False else: - yield ', ' + yield ', ' + newline_indent yield encoder(key) yield ': ' for chunk in self._iterencode(value, markers): yield chunk @@ -194,8 +211,9 @@ yield encoder(key) yield ': ' for chunk in self._iterencode(value, markers): yield chunk - yield '}' + self.current_indent_level -= 1 + yield self._newline_indent() + '}' if markers is not None: del markers[markerid] diff --git a/simplejson/tests/test_indent.py b/simplejson/tests/test_indent.py new file mode 100644 index 0000000000000000000000000000000000000000..ba010af8a92eb563d53e19a7b7d2d9a3a91dffc3_c2ltcGxlanNvbi90ZXN0cy90ZXN0X2luZGVudC5weQ== --- /dev/null +++ b/simplejson/tests/test_indent.py @@ -0,0 +1,18 @@ + + + +def test_indent(): + import simplejson + + h = [['blorpie'], ['whoops'], [], 'd-shtaeou', 'd-nthiouh', 'i-vhbjkhnth', + {'nifty': 87}, {'field': 'yes', 'morefield': False} ] + + + d1 = simplejson.dumps(h) + d2 = simplejson.dumps(h, indent=2) + + h1 = simplejson.loads(d1) + h2 = simplejson.loads(d2) + + assert h1 == h + assert h2 == h