diff --git a/Lib/json/encoder.py b/Lib/json/encoder.py index 718b3254241c565..8768b63a3f80417 100644 --- a/Lib/json/encoder.py +++ b/Lib/json/encoder.py @@ -223,29 +223,6 @@ def iterencode(self, o, _one_shot=False): else: _encoder = encode_basestring - def floatstr(o, allow_nan=self.allow_nan, - _repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY): - # Check for specials. Note that this type of test is processor - # and/or platform-specific, so do tests which don't depend on the - # internals. - - if o != o: - text = 'NaN' - elif o == _inf: - text = 'Infinity' - elif o == _neginf: - text = '-Infinity' - else: - return _repr(o) - - if not allow_nan: - raise ValueError( - "Out of range float values are not JSON compliant: " + - repr(o)) - - return text - - if self.indent is None or isinstance(self.indent, str): indent = self.indent else: @@ -256,6 +233,28 @@ def floatstr(o, allow_nan=self.allow_nan, self.key_separator, self.item_separator, self.sort_keys, self.skipkeys, self.allow_nan) else: + def floatstr(o, allow_nan=self.allow_nan, + _repr=float.__repr__, _inf=INFINITY, _neginf=-INFINITY): + # Check for specials. Note that this type of test is processor + # and/or platform-specific, so do tests which don't depend on + # the internals. + + if o != o: + text = 'NaN' + elif o == _inf: + text = 'Infinity' + elif o == _neginf: + text = '-Infinity' + else: + return _repr(o) + + if not allow_nan: + raise ValueError( + "Out of range float values are not JSON compliant: " + + repr(o)) + + return text + _iterencode = _make_iterencode( markers, self.default, _encoder, indent, floatstr, self.key_separator, self.item_separator, self.sort_keys, diff --git a/Misc/NEWS.d/next/Library/2026-06-02-15-45-00.gh-issue-150820.W7tpO7.rst b/Misc/NEWS.d/next/Library/2026-06-02-15-45-00.gh-issue-150820.W7tpO7.rst new file mode 100644 index 000000000000000..ae9858f5294183a --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-06-02-15-45-00.gh-issue-150820.W7tpO7.rst @@ -0,0 +1 @@ +Speed up :func:`json.dumps` for small documents. Patch by Bernát Gábor.