Message171363
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. |
|
Date |
User |
Action |
Args |
2012-09-26 21:40:43 | Justin.Lebar | set | recipients:
+ Justin.Lebar, docs@python |
2012-09-26 21:40:43 | Justin.Lebar | set | messageid: <1348695643.05.0.172807817425.issue16057@psf.upfronthosting.co.za> |
2012-09-26 21:40:42 | Justin.Lebar | link | issue16057 messages |
2012-09-26 21:40:41 | Justin.Lebar | create | |
|