diff --git a/CHANGES.rst b/CHANGES.rst index 05609229..1af03f26 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -12,6 +12,9 @@ Unreleased - Fix type annotation for ``errorhandler`` decorator. :issue:`4295` - Revert a change to the CLI that caused it to hide ``ImportError`` tracebacks when importing the application. :issue:`4307` +- ``app.json_encoder`` and ``json_decoder`` are only passed to + ``dumps`` and ``loads`` if they have custom behavior. This improves + performance, mainly on PyPy. :issue:`4349` Version 2.0.2 diff --git a/src/flask/json/__init__.py b/src/flask/json/__init__.py index 10d5123a..ccb9efb1 100644 --- a/src/flask/json/__init__.py +++ b/src/flask/json/__init__.py @@ -81,6 +81,11 @@ def _dump_arg_defaults( if bp is not None and bp.json_encoder is not None: cls = bp.json_encoder + # Only set a custom encoder if it has custom behavior. This is + # faster on PyPy. + if cls is not _json.JSONEncoder: + kwargs.setdefault("cls", cls) + kwargs.setdefault("cls", cls) kwargs.setdefault("ensure_ascii", app.config["JSON_AS_ASCII"]) kwargs.setdefault("sort_keys", app.config["JSON_SORT_KEYS"]) @@ -102,9 +107,10 @@ def _load_arg_defaults( if bp is not None and bp.json_decoder is not None: cls = bp.json_decoder - kwargs.setdefault("cls", cls) - else: - kwargs.setdefault("cls", JSONDecoder) + # Only set a custom decoder if it has custom behavior. This is + # faster on PyPy. + if cls not in {JSONDecoder, _json.JSONDecoder}: + kwargs.setdefault("cls", cls) def dumps(obj: t.Any, app: t.Optional["Flask"] = None, **kwargs: t.Any) -> str: