mirror of
https://github.com/python/cpython.git
synced 2025-11-01 10:45:30 +00:00
bpo-24641: Improved error message for JSON unserializible keys. (#4364)
Also updated an example for default() in the module docstring. Removed quotes around type name in other error messages.
This commit is contained in:
parent
5b48dc638b
commit
cfa797c068
4 changed files with 19 additions and 13 deletions
|
|
@ -76,7 +76,8 @@ Specializing JSON object encoding::
|
|||
>>> def encode_complex(obj):
|
||||
... if isinstance(obj, complex):
|
||||
... return [obj.real, obj.imag]
|
||||
... raise TypeError(repr(obj) + " is not JSON serializable")
|
||||
... raise TypeError(f'Object of type {obj.__class__.__name__} '
|
||||
... f'is not JSON serializable')
|
||||
...
|
||||
>>> json.dumps(2 + 1j, default=encode_complex)
|
||||
'[2.0, 1.0]'
|
||||
|
|
@ -344,8 +345,8 @@ def loads(s, *, encoding=None, cls=None, object_hook=None, parse_float=None,
|
|||
s, 0)
|
||||
else:
|
||||
if not isinstance(s, (bytes, bytearray)):
|
||||
raise TypeError('the JSON object must be str, bytes or bytearray, '
|
||||
'not {!r}'.format(s.__class__.__name__))
|
||||
raise TypeError(f'the JSON object must be str, bytes or bytearray, '
|
||||
f'not {s.__class__.__name__}')
|
||||
s = s.decode(detect_encoding(s), 'surrogatepass')
|
||||
|
||||
if (cls is None and object_hook is None and
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue