mirror of
https://github.com/python/cpython.git
synced 2025-12-23 09:19:18 +00:00
json: call self.default for unsupported floats
bpo-36841: allows `default` option to override unsupported float behavior, instead of unconditional ValueError.
This commit is contained in:
parent
b34dd58fee
commit
528fe30c38
1 changed files with 6 additions and 3 deletions
|
|
@ -176,6 +176,11 @@ class JSONEncoder(object):
|
|||
return JSONEncoder.default(self, o)
|
||||
|
||||
"""
|
||||
if isinstance(o, float):
|
||||
raise ValueError(
|
||||
"Out of range float values are not JSON compliant: " +
|
||||
repr(o)
|
||||
)
|
||||
raise TypeError(f'Object of type {o.__class__.__name__} '
|
||||
f'is not JSON serializable')
|
||||
|
||||
|
|
@ -236,9 +241,7 @@ class JSONEncoder(object):
|
|||
return _repr(o)
|
||||
|
||||
if not allow_nan:
|
||||
raise ValueError(
|
||||
"Out of range float values are not JSON compliant: " +
|
||||
repr(o))
|
||||
return self.default(o)
|
||||
|
||||
return text
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue