diff options
-rw-r--r-- | CHANGES.txt | 2 | ||||
-rw-r--r-- | simplejson/encoder.py | 4 | ||||
-rw-r--r-- | simplejson/tests/test_indent.py | 16 |
3 files changed, 20 insertions, 2 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index 31debb9..671a862 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,5 +1,7 @@ Version 2.1.2 released XXXX-XX-XX +* Correct output for indent=0 + http://bugs.python.org/issue10019 * Correctly raise TypeError when non-string keys are used with speedups http://code.google.com/p/simplejson/issues/detail?id=82 * Fix the endlineno, endcolno attributes of the JSONDecodeError exception. diff --git a/simplejson/encoder.py b/simplejson/encoder.py index cab8456..a7f4eea 100644 --- a/simplejson/encoder.py +++ b/simplejson/encoder.py @@ -147,7 +147,7 @@ class JSONEncoder(object): If encoding is not None, then all input strings will be transformed into unicode using that encoding prior to JSON-encoding. The default is UTF-8. - + If use_decimal is true (not the default), ``decimal.Decimal`` will be supported directly by the encoder. For the inverse, decode JSON with ``parse_float=decimal.Decimal``. @@ -268,7 +268,7 @@ class JSONEncoder(object): key_memo = {} if (_one_shot and c_make_encoder is not None - and not self.indent and not self.sort_keys): + and self.indent is None and not self.sort_keys): _iterencode = c_make_encoder( markers, self.default, _encoder, self.indent, self.key_separator, self.item_separator, self.sort_keys, diff --git a/simplejson/tests/test_indent.py b/simplejson/tests/test_indent.py index 985831b..75a2f6e 100644 --- a/simplejson/tests/test_indent.py +++ b/simplejson/tests/test_indent.py @@ -2,6 +2,7 @@ from unittest import TestCase import simplejson as json import textwrap +from StringIO import StringIO class TestIndent(TestCase): def test_indent(self): @@ -51,3 +52,18 @@ class TestIndent(TestCase): # so the following is expected to fail. Python 2.4 is not a # supported platform in simplejson 2.1.0+. self.assertEquals(d2, expect) + + def test_indent0(self): + h = {3: 1} + def check(indent, expected): + d1 = json.dumps(h, indent=indent) + self.assertEquals(d1, expected) + + sio = StringIO() + json.dump(h, sio, indent=indent) + self.assertEquals(sio.getvalue(), expected) + + # indent=0 should emit newlines + check(0, '{\n"3": 1\n}') + # indent=None is more compact + check(None, '{"3": 1}') |