Skip to content
Myr

Only use a custom JSONDecoder if needed

Before adoption: this commit came in with the history the project adopted, so no landing records it.

The default JSONDecoder adds no extra functionality to `json.loads`, but using a custom class in the function call you off of an optimized fast path in pypy (it adds some overhead in cpython, but I think it's minimal). This change sticks with the default loading behaviour unless the user has specifically opted in to customising it.

authored by olliemath · committed by David Lord · parent c5ca175 · Browse files · 2 files+12 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • Co-authored-by: David Lord <davidism@gmail.com>

Diff stats

2 files+12 added−3 removed

  • other:+12 added−3 removed

2 files+12 added−3 removed

modified:src/flask/json/__init__.py+9 added-3 removed
79 unmodified lines
80 bp = app.blueprints.get(request.blueprint) if request else None # type: ignore81 if bp is not None and bp.json_encoder is not None:82 cls = bp.json_encoder8384 # Only set a custom encoder if it has custom behavior. This is85 # faster on PyPy.86 if cls is not _json.JSONEncoder:87 kwargs.setdefault("cls", cls)8889 kwargs.setdefault("cls", cls)90 kwargs.setdefault("ensure_ascii", app.config["JSON_AS_ASCII"])91 kwargs.setdefault("sort_keys", app.config["JSON_SORT_KEYS"])92 else:
13 unmodified lines
106 bp = app.blueprints.get(request.blueprint) if request else None # type: ignore107 if bp is not None and bp.json_decoder is not None:108 cls = bp.json_decoder109105 kwargs.setdefault("cls", cls)106 else:107 kwargs.setdefault("cls", JSONDecoder)110 # Only set a custom decoder if it has custom behavior. This is111 # faster on PyPy.112 if cls not in {JSONDecoder, _json.JSONDecoder}:113 kwargs.setdefault("cls", cls)114115116def dumps(obj: t.Any, app: t.Optional["Flask"] = None, **kwargs: t.Any) -> str:117 """Serialize an object to a string of JSON.
246 unmodified lines
modified:CHANGES.rst+3 added
10 unmodified lines
11 2.0.x. Use ``response.request.environ`` instead. :pr:`4341`12- Fix type annotation for ``errorhandler`` decorator. :issue:`4295`13- Revert a change to the CLI that caused it to hide ``ImportError``14 tracebacks when importing the application. :issue:`4307`15- ``app.json_encoder`` and ``json_decoder`` are only passed to16 ``dumps`` and ``loads`` if they have custom behavior. This improves17 performance, mainly on PyPy. :issue:`4349`181920Version 2.0.221-------------
1180 unmodified lines