diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index 4c70e8b75ed..fe87cff9675 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -179,6 +179,11 @@ class JSONEncoder(object): return super().default(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') @@ -239,9 +244,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