summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBob Ippolito <bob@redivi.com>2015-10-27 08:31:48 -0700
committerBob Ippolito <bob@redivi.com>2015-10-27 08:31:48 -0700
commitb979a80722bfbcecc6cde366449e1e0b605c51b6 (patch)
tree1d977c0ea878cc2ad27aeb14e51cdf0ec5dc9857
parent82fdecb6a892579bde16f43b4b983b9dce6c3643 (diff)
downloadsimplejson-3.8.1.tar.gz
Fix issue with iterable_as_array and indent option, closes #128v3.8.1
-rw-r--r--CHANGES.txt4
-rw-r--r--simplejson/encoder.py12
-rw-r--r--simplejson/tests/__init__.py1
-rw-r--r--simplejson/tests/test_iterable.py34
4 files changed, 29 insertions, 22 deletions
diff --git a/CHANGES.txt b/CHANGES.txt
index ccce4d5..af8e566 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,5 +1,7 @@
-Version 3.8.1 released 2015-XX-XX
+Version 3.8.1 released 2015-10-27
+* Fix issue with iterable_as_array and indent option
+ https://github.com/simplejson/simplejson/issues/128
* Fix typo in keyword argument name introduced in 3.8.0
https://github.com/simplejson/simplejson/pull/123
diff --git a/simplejson/encoder.py b/simplejson/encoder.py
index d771bb4..5b9bda7 100644
--- a/simplejson/encoder.py
+++ b/simplejson/encoder.py
@@ -496,10 +496,14 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
chunks = _iterencode(value, _current_indent_level)
for chunk in chunks:
yield chunk
- if newline_indent is not None:
- _current_indent_level -= 1
- yield '\n' + (_indent * _current_indent_level)
- yield ']'
+ if first:
+ # iterable_as_array misses the fast path at the top
+ yield '[]'
+ else:
+ if newline_indent is not None:
+ _current_indent_level -= 1
+ yield '\n' + (_indent * _current_indent_level)
+ yield ']'
if markers is not None:
del markers[markerid]
diff --git a/simplejson/tests/__init__.py b/simplejson/tests/__init__.py
index 8c1a4f1..88249d1 100644
--- a/simplejson/tests/__init__.py
+++ b/simplejson/tests/__init__.py
@@ -49,6 +49,7 @@ def all_tests_suite():
'simplejson.tests.test_fail',
'simplejson.tests.test_float',
'simplejson.tests.test_indent',
+ 'simplejson.tests.test_iterable',
'simplejson.tests.test_pass1',
'simplejson.tests.test_pass2',
'simplejson.tests.test_pass3',
diff --git a/simplejson/tests/test_iterable.py b/simplejson/tests/test_iterable.py
index 4b37d00..35d3e75 100644
--- a/simplejson/tests/test_iterable.py
+++ b/simplejson/tests/test_iterable.py
@@ -1,5 +1,5 @@
import unittest
-from StringIO import StringIO
+from simplejson.compat import StringIO
import simplejson as json
@@ -13,19 +13,19 @@ def sio_dump(obj, **kw):
class TestIterable(unittest.TestCase):
def test_iterable(self):
- l = [1, 2, 3]
- for dumps in (json.dumps, iter_dumps, sio_dump):
- expect = dumps(l)
- default_expect = dumps(sum(l))
- # Default is False
- self.assertRaises(TypeError, dumps, iter(l))
- self.assertRaises(TypeError, dumps, iter(l), iterable_as_array=False)
- self.assertEqual(expect, dumps(iter(l), iterable_as_array=True))
- # Ensure that the "default" gets called
- self.assertEqual(default_expect, dumps(iter(l), default=sum))
- self.assertEqual(default_expect, dumps(iter(l), iterable_as_array=False, default=sum))
- # Ensure that the "default" does not get called
- self.assertEqual(
- default_expect,
- dumps(iter(l), iterable_as_array=True, default=sum))
- \ No newline at end of file
+ for l in ([], [1], [1, 2], [1, 2, 3]):
+ for opts in [{}, {'indent': 2}]:
+ for dumps in (json.dumps, iter_dumps, sio_dump):
+ expect = dumps(l, **opts)
+ default_expect = dumps(sum(l), **opts)
+ # Default is False
+ self.assertRaises(TypeError, dumps, iter(l), **opts)
+ self.assertRaises(TypeError, dumps, iter(l), iterable_as_array=False, **opts)
+ self.assertEqual(expect, dumps(iter(l), iterable_as_array=True, **opts))
+ # Ensure that the "default" gets called
+ self.assertEqual(default_expect, dumps(iter(l), default=sum, **opts))
+ self.assertEqual(default_expect, dumps(iter(l), iterable_as_array=False, default=sum, **opts))
+ # Ensure that the "default" does not get called
+ self.assertEqual(
+ expect,
+ dumps(iter(l), iterable_as_array=True, default=sum, **opts))