summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Baumgold <singingwolfboy@gmail.com>2011-11-10 22:42:46 -0500
committerDavid Baumgold <singingwolfboy@gmail.com>2011-11-10 22:42:46 -0500
commit698e6c1e3fc00a41ef8a09f2a5c8b287034e6390 (patch)
tree068f0c48b4d51ee58f4c5c1b0237740bedae6843
parent52d00503192973f36e629c8869652df1b65eb8be (diff)
downloadsimplejson-698e6c1e3fc00a41ef8a09f2a5c8b287034e6390.tar.gz
Removed isinstance() check for namedtuple_as_object
Using isinstance() is not Pythonic and should be avoided when possible. By using duck-typing instead, we can easily support other objects that do not inherit from tuple but still support the same API as namedtuple, such as objects created using the `recordtype` library: http://pypi.python.org/pypi/recordtype
-rw-r--r--simplejson/encoder.py11
1 files changed, 4 insertions, 7 deletions
diff --git a/simplejson/encoder.py b/simplejson/encoder.py
index 5ec7440..383d834 100644
--- a/simplejson/encoder.py
+++ b/simplejson/encoder.py
@@ -155,7 +155,7 @@ class JSONEncoder(object):
be supported directly by the encoder. For the inverse, decode JSON
with ``parse_float=decimal.Decimal``.
- If namedtuple_as_object is true (the default), tuple subclasses with
+ If namedtuple_as_object is true (the default), objects with
``_asdict()`` methods will be encoded as JSON objects.
If tuple_as_array is true (the default), tuple (and subclasses) will
@@ -387,8 +387,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
yield buf
if isinstance(value, list):
chunks = _iterencode_list(value, _current_indent_level)
- elif (_namedtuple_as_object and isinstance(value, tuple) and
- hasattr(value, '_asdict')):
+ elif (_namedtuple_as_object and hasattr(value, '_asdict')):
chunks = _iterencode_dict(value._asdict(),
_current_indent_level)
elif _tuple_as_array and isinstance(value, tuple):
@@ -472,8 +471,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
else:
if isinstance(value, list):
chunks = _iterencode_list(value, _current_indent_level)
- elif (_namedtuple_as_object and isinstance(value, tuple) and
- hasattr(value, '_asdict')):
+ elif (_namedtuple_as_object and hasattr(value, '_asdict')):
chunks = _iterencode_dict(value._asdict(),
_current_indent_level)
elif _tuple_as_array and isinstance(value, tuple):
@@ -507,8 +505,7 @@ def _make_iterencode(markers, _default, _encoder, _indent, _floatstr,
elif isinstance(o, list):
for chunk in _iterencode_list(o, _current_indent_level):
yield chunk
- elif (_namedtuple_as_object and isinstance(o, tuple) and
- hasattr(o, '_asdict')):
+ elif (_namedtuple_as_object and hasattr(o, '_asdict')):
for chunk in _iterencode_dict(o._asdict(), _current_indent_level):
yield chunk
elif (_tuple_as_array and isinstance(o, tuple)):