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:
Min RK 2021-09-30 13:02:56 +02:00 committed by GitHub
parent b34dd58fee
commit 528fe30c38
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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