Browse Source

Optimize jsonable_encoder to skip building key set when no include/exclude

pull/15624/head
AshNicolus 2 months ago
parent
commit
ef9c11010d
  1. 4
      fastapi/encoders.py

4
fastapi/encoders.py

@ -280,6 +280,8 @@ def jsonable_encoder(
return None
if isinstance(obj, dict):
encoded_dict = {}
allowed_keys: set[Any] | None = None
if include is not None or exclude is not None:
allowed_keys = set(obj.keys())
if include is not None:
allowed_keys &= set(include)
@ -293,7 +295,7 @@ def jsonable_encoder(
or (not key.startswith("_sa"))
)
and (value is not None or not exclude_none)
and key in allowed_keys
and (allowed_keys is None or key in allowed_keys)
):
encoded_key = jsonable_encoder(
key,

Loading…
Cancel
Save