Merge #16057: Clarify why the base method default is called in custom encoders.
Original patch by Kushal Das.
This commit is contained in:
commit
1f2a49cfc6
|
@ -83,6 +83,7 @@ Extending :class:`JSONEncoder`::
|
||||||
... def default(self, obj):
|
... def default(self, obj):
|
||||||
... if isinstance(obj, complex):
|
... if isinstance(obj, complex):
|
||||||
... return [obj.real, obj.imag]
|
... return [obj.real, obj.imag]
|
||||||
|
... # Let the base class default method raise the TypeError
|
||||||
... return json.JSONEncoder.default(self, obj)
|
... return json.JSONEncoder.default(self, obj)
|
||||||
...
|
...
|
||||||
>>> json.dumps(2 + 1j, cls=ComplexEncoder)
|
>>> json.dumps(2 + 1j, cls=ComplexEncoder)
|
||||||
|
@ -431,6 +432,7 @@ Encoders and Decoders
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
return list(iterable)
|
return list(iterable)
|
||||||
|
# Let the base class default method raise the TypeError
|
||||||
return json.JSONEncoder.default(self, o)
|
return json.JSONEncoder.default(self, o)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -166,6 +166,7 @@ class JSONEncoder(object):
|
||||||
pass
|
pass
|
||||||
else:
|
else:
|
||||||
return list(iterable)
|
return list(iterable)
|
||||||
|
# Let the base class default method raise the TypeError
|
||||||
return JSONEncoder.default(self, o)
|
return JSONEncoder.default(self, o)
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
Loading…
Reference in New Issue