Skip to content

Commit

Permalink
Documentation and a test for the previous commit
Browse files Browse the repository at this point in the history
  • Loading branch information
kini committed Feb 27, 2012
1 parent 627af99 commit 7eac636
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 2 deletions.
2 changes: 1 addition & 1 deletion simplejson/encoder.py
Expand Up @@ -171,7 +171,7 @@ def __init__(self, skipkeys=False, ensure_ascii=True,
self.namedtuple_as_object = namedtuple_as_object
self.tuple_as_array = tuple_as_array
try:
indent = ' ' * indent
indent = indent * ' '

This comment has been minimized.

Copy link
@kini

kini Feb 27, 2012

Author Owner

In making the class for the test, I noticed that I needed this further change. In my original use case, the class, or perhaps some other mechanism in the environment there, seems to be smart enough even to be able to handle ' ' * indent, but my toy example (and thus perhaps other users' examples) cannot. This diff chunk is not a regression fix, though, because such objects wouldn't have worked anyway before the indent parameter to the JSONEncoder constructor was made a string instead of an int, as far as I understand. (?)

This comment has been minimized.

Copy link
@kini

kini Feb 27, 2012

Author Owner

I take that back. Such objects would have worked in the old version of simplejson, because in the old version, the indent parameter is never directly multiplied by ' '. The relevant line is

newline_indent = '\n' + (' ' * (_indent * _current_indent_level))

The fake integer turns into a real integer (by multiplication with a real integer in the "correct" order) before it is multiplied by ' '. So this is indeed part of the regression fix.

except TypeError:
pass
self.indent = indent
Expand Down
42 changes: 41 additions & 1 deletion simplejson/tests/test_dump.py
Expand Up @@ -24,4 +24,44 @@ def test_ordered_dict(self):
# http://bugs.python.org/issue6105
items = [('one', 1), ('two', 2), ('three', 3), ('four', 4), ('five', 5)]
s = json.dumps(json.OrderedDict(items))
self.assertEqual(s, '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}')
self.assertEqual(s, '{"one": 1, "two": 2, "three": 3, "four": 4, "five": 5}')

def test_indent_unknown_type_acceptance(self):
"""
A test against the regression mentioned at `github issue 29`_.
The indent parameter should accept any type which pretends to be
an instance of int or long when it comes to being multiplied by
strings, even if it is not actually an int or long, for
backwards compatibility.
.. _github issue 29:
http://github.com/simplejson/simplejson/issue/29
"""

class AwesomeInt(object):
"""An awesome reimplementation of integers"""

def __init__(self, *args, **kwargs):
if len(args) > 0:
# [construct from literals, objects, etc.]
# ...

# Finally, if args[0] is an integer, store it
if isinstance(args[0], int):
self._int = args[0]

# [various methods]

def __mul__(self, other):
# [various ways to multiply AwesomeInt objects]
# ... finally, if the right-hand operand is not awesome enough,
# try to do a normal integer multiplication
if hasattr(self, '_int'):
return self._int * other
else:
raise NotImplementedError("To do non-awesome things with"
" this object, please construct it from an integer!")

s = json.dumps(range(3), indent=AwesomeInt(3))
self.assertEqual(s, '[\n 0,\n 1,\n 2\n]')

0 comments on commit 7eac636

Please sign in to comment.