Browse Source

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

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

4
fastapi/encoders.py

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

Loading…
Cancel
Save