Browse Source

Merge 0b576b0960 into 76b324d95b

pull/5718/merge
Guillaume Gelin 22 hours ago
committed by GitHub
parent
commit
821c85f8ff
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 26
      fastapi/encoders.py

26
fastapi/encoders.py

@ -1,6 +1,6 @@
import dataclasses
import datetime
from collections import defaultdict, deque
from collections import deque
from decimal import Decimal
from enum import Enum
from ipaddress import (
@ -14,7 +14,7 @@ from ipaddress import (
from pathlib import Path, PurePath
from re import Pattern
from types import GeneratorType
from typing import Any, Callable, Dict, List, Optional, Tuple, Type, Union
from typing import Any, Callable, Dict, List, Optional, Type, Union
from uuid import UUID
from fastapi.types import IncEx
@ -85,20 +85,6 @@ ENCODERS_BY_TYPE: Dict[Type[Any], Callable[[Any], Any]] = {
}
def generate_encoders_by_class_tuples(
type_encoder_map: Dict[Any, Callable[[Any], Any]],
) -> Dict[Callable[[Any], Any], Tuple[Any, ...]]:
encoders_by_class_tuples: Dict[Callable[[Any], Any], Tuple[Any, ...]] = defaultdict(
tuple
)
for type_, encoder in type_encoder_map.items():
encoders_by_class_tuples[encoder] += (type_,)
return encoders_by_class_tuples
encoders_by_class_tuples = generate_encoders_by_class_tuples(ENCODERS_BY_TYPE)
def jsonable_encoder(
obj: Annotated[
Any,
@ -314,11 +300,9 @@ def jsonable_encoder(
)
return encoded_list
if type(obj) in ENCODERS_BY_TYPE:
return ENCODERS_BY_TYPE[type(obj)](obj)
for encoder, classes_tuple in encoders_by_class_tuples.items():
if isinstance(obj, classes_tuple):
return encoder(obj)
for base in obj.__class__.__mro__[:-1]:
if base in ENCODERS_BY_TYPE:
return ENCODERS_BY_TYPE[base](obj)
try:
data = dict(obj)

Loading…
Cancel
Save