New submission from Justin Lebar:
The JSONEncoder documentation says we can implement our own encoder as:
>>> class ComplexEncoder(json.JSONEncoder):
... def default(self, obj):
... if isinstance(obj, complex):
... return [obj.real, obj.imag]
... return json.JSONEncoder.default(self, obj)
Later on, we give the following example of how to implement the default method
in a subclass of json.JSONEncoder:
def default(self, o):
try:
iterable = iter(o)
except TypeError:
pass
else:
return list(iterable)
return JSONEncoder.default(self, o)
These are both incorrect, as a quick reading of the source will reveal.
JSONEncoder.default() throws for all input values. We should
s/JSONEncoder.default/JSONEncoder.encode/ here, I think.
----------
assignee: docs@python
components: Documentation
messages: 171363
nosy: Justin.Lebar, docs@python
priority: normal
severity: normal
status: open
title: Subclasses of JSONEncoder should not be insturcted to call
JSONEncoder.decode
versions: Python 2.7
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue16057>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com