taken from pydantic v1 as is taken from pydantic v1 as is todo pv2 should this return strings instead encodes a decimal as int of there s no exponent otherwise float this is useful when we use constraineddecimal to represent numericx 0 where a integer but not int typed is used encoding this as a float results in failed roundtripping between encode and parse our id type is a prime example of this decimalencoderdecimal1 0 1 0 decimalencoderdecimal1 1 the input object to convert to json pydantic s include parameter passed to pydantic models to set the fields to include pydantic s exclude parameter passed to pydantic models to set the fields to exclude pydantic s byalias parameter passed to pydantic models to define if the output should use the alias names when provided or the python attribute names in an api if you set an alias it s probably because you want to use it in the result so you probably want to leave this set to true pydantic s excludeunset parameter passed to pydantic models to define if it should exclude from the output the fields that were not explicitly set and that only had their default values pydantic s excludedefaults parameter passed to pydantic models to define if it should exclude from the output the fields that had the same default value even when they were explicitly set pydantic s excludenone parameter passed to pydantic models to define if it should exclude from the output any fields that have a none value pydantic s customencoder parameter passed to pydantic models to define a custom encoder exclude from the output any fields that start with the name sa this is mainly a hack for compatibility with sqlalchemy objects they store internal sqlalchemyspecific state in attributes named with sa and those objects can t and shouldn t be serialized to json convert any object to something that can be encoded in json this is used internally by fastapi to make sure anything you return can be encoded as json before it is sent to the client you can also use it yourself for example to convert objects before saving them in a database that supports only json read more about it in the fastapi docs for json compatible encoderhttps fastapi tiangolo comtutorialencoder todo remove when deprecating pydantic v1 todo remove when deprecating pydantic v1 type ignore attr defined taken from pydantic v1 as is taken from pydantic v1 as is todo pv2 should this return strings instead encodes a decimal as int of there s no exponent otherwise float this is useful when we use constraineddecimal to represent numeric x 0 where a integer but not int typed is used encoding this as a float results in failed round tripping between encode and parse our id type is a prime example of this decimal_encoder decimal 1 0 1 0 decimal_encoder decimal 1 1 type ignore operator the input object to convert to json pydantic s include parameter passed to pydantic models to set the fields to include pydantic s exclude parameter passed to pydantic models to set the fields to exclude pydantic s by_alias parameter passed to pydantic models to define if the output should use the alias names when provided or the python attribute names in an api if you set an alias it s probably because you want to use it in the result so you probably want to leave this set to true pydantic s exclude_unset parameter passed to pydantic models to define if it should exclude from the output the fields that were not explicitly set and that only had their default values pydantic s exclude_defaults parameter passed to pydantic models to define if it should exclude from the output the fields that had the same default value even when they were explicitly set pydantic s exclude_none parameter passed to pydantic models to define if it should exclude from the output any fields that have a none value pydantic s custom_encoder parameter passed to pydantic models to define a custom encoder exclude from the output any fields that start with the name _sa this is mainly a hack for compatibility with sqlalchemy objects they store internal sqlalchemy specific state in attributes named with _sa and those objects can t and shouldn t be serialized to json convert any object to something that can be encoded in json this is used internally by fastapi to make sure anything you return can be encoded as json before it is sent to the client you can also use it yourself for example to convert objects before saving them in a database that supports only json read more about it in the fastapi docs for json compatible encoder https fastapi tiangolo com tutorial encoder todo remove when deprecating pydantic v1 type ignore attr defined todo remove when deprecating pydantic v1
import dataclasses
import datetime
from collections import defaultdict, deque
from decimal import Decimal
from enum import Enum
from ipaddress import (
    IPv4Address,
    IPv4Interface,
    IPv4Network,
    IPv6Address,
    IPv6Interface,
    IPv6Network,
)
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 uuid import UUID

from fastapi.types import IncEx
from pydantic import BaseModel
from pydantic.color import Color
from pydantic.networks import AnyUrl, NameEmail
from pydantic.types import SecretBytes, SecretStr
from typing_extensions import Annotated, Doc  

from ._compat import PYDANTIC_V2, Url, _model_dump



def isoformat(o: Union[datetime.date, datetime.time]) -> str:
    return o.isoformat()




def decimal_encoder(dec_value: Decimal) -> Union[int, float]:
    
    if dec_value.as_tuple().exponent >= 0:  
        return int(dec_value)
    else:
        return float(dec_value)


ENCODERS_BY_TYPE: Dict[Type[Any], Callable[[Any], Any]] = {
    bytes: lambda o: o.decode(),
    Color: str,
    datetime.date: isoformat,
    datetime.datetime: isoformat,
    datetime.time: isoformat,
    datetime.timedelta: lambda td: td.total_seconds(),
    Decimal: decimal_encoder,
    Enum: lambda o: o.value,
    frozenset: list,
    deque: list,
    GeneratorType: list,
    IPv4Address: str,
    IPv4Interface: str,
    IPv4Network: str,
    IPv6Address: str,
    IPv6Interface: str,
    IPv6Network: str,
    NameEmail: str,
    Path: str,
    Pattern: lambda o: o.pattern,
    SecretBytes: str,
    SecretStr: str,
    set: list,
    UUID: str,
    Url: str,
    AnyUrl: str,
}


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,
        Doc(
            
        ),
    ],
    include: Annotated[
        Optional[IncEx],
        Doc(
            
        ),
    ] = None,
    exclude: Annotated[
        Optional[IncEx],
        Doc(
            
        ),
    ] = None,
    by_alias: Annotated[
        bool,
        Doc(
            
        ),
    ] = True,
    exclude_unset: Annotated[
        bool,
        Doc(
            
        ),
    ] = False,
    exclude_defaults: Annotated[
        bool,
        Doc(
            
        ),
    ] = False,
    exclude_none: Annotated[
        bool,
        Doc(
            
        ),
    ] = False,
    custom_encoder: Annotated[
        Optional[Dict[Any, Callable[[Any], Any]]],
        Doc(
            
        ),
    ] = None,
    sqlalchemy_safe: Annotated[
        bool,
        Doc(
            
        ),
    ] = True,
) -> Any:
    
    custom_encoder = custom_encoder or {}
    if custom_encoder:
        if type(obj) in custom_encoder:
            return custom_encoder[type(obj)](obj)
        else:
            for encoder_type, encoder_instance in custom_encoder.items():
                if isinstance(obj, encoder_type):
                    return encoder_instance(obj)
    if include is not None and not isinstance(include, (set, dict)):
        include = set(include)
    if exclude is not None and not isinstance(exclude, (set, dict)):
        exclude = set(exclude)
    if isinstance(obj, BaseModel):
        
        encoders: Dict[Any, Any] = {}
        if not PYDANTIC_V2:
            encoders = getattr(obj.__config__, "json_encoders", {})  
            if custom_encoder:
                encoders.update(custom_encoder)
        obj_dict = _model_dump(
            obj,
            mode="json",
            include=include,
            exclude=exclude,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
            exclude_none=exclude_none,
            exclude_defaults=exclude_defaults,
        )
        if "__root__" in obj_dict:
            obj_dict = obj_dict["__root__"]
        return jsonable_encoder(
            obj_dict,
            exclude_none=exclude_none,
            exclude_defaults=exclude_defaults,
            
            custom_encoder=encoders,
            sqlalchemy_safe=sqlalchemy_safe,
        )
    if dataclasses.is_dataclass(obj):
        obj_dict = dataclasses.asdict(obj)
        return jsonable_encoder(
            obj_dict,
            include=include,
            exclude=exclude,
            by_alias=by_alias,
            exclude_unset=exclude_unset,
            exclude_defaults=exclude_defaults,
            exclude_none=exclude_none,
            custom_encoder=custom_encoder,
            sqlalchemy_safe=sqlalchemy_safe,
        )
    if isinstance(obj, Enum):
        return obj.value
    if isinstance(obj, PurePath):
        return str(obj)
    if isinstance(obj, (str, int, float, type(None))):
        return obj
    if isinstance(obj, dict):
        encoded_dict = {}
        allowed_keys = set(obj.keys())
        if include is not None:
            allowed_keys &= set(include)
        if exclude is not None:
            allowed_keys -= set(exclude)
        for key, value in obj.items():
            if (
                (
                    not sqlalchemy_safe
                    or (not isinstance(key, str))
                    or (not key.startswith("_sa"))
                )
                and (value is not None or not exclude_none)
                and key in allowed_keys
            ):
                encoded_key = jsonable_encoder(
                    key,
                    by_alias=by_alias,
                    exclude_unset=exclude_unset,
                    exclude_none=exclude_none,
                    custom_encoder=custom_encoder,
                    sqlalchemy_safe=sqlalchemy_safe,
                )
                encoded_value = jsonable_encoder(
                    value,
                    by_alias=by_alias,
                    exclude_unset=exclude_unset,
                    exclude_none=exclude_none,
                    custom_encoder=custom_encoder,
                    sqlalchemy_safe=sqlalchemy_safe,
                )
                encoded_dict[encoded_key] = encoded_value
        return encoded_dict
    if isinstance(obj, (list, set, frozenset, GeneratorType, tuple, deque)):
        encoded_list = []
        for item in obj:
            encoded_list.append(
                jsonable_encoder(
                    item,
                    include=include,
                    exclude=exclude,
                    by_alias=by_alias,
                    exclude_unset=exclude_unset,
                    exclude_defaults=exclude_defaults,
                    exclude_none=exclude_none,
                    custom_encoder=custom_encoder,
                    sqlalchemy_safe=sqlalchemy_safe,
                )
            )
        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)

    try:
        data = dict(obj)
    except Exception as e:
        errors: List[Exception] = []
        errors.append(e)
        try:
            data = vars(obj)
        except Exception as e:
            errors.append(e)
            raise ValueError(errors) from e
    return jsonable_encoder(
        data,
        include=include,
        exclude=exclude,
        by_alias=by_alias,
        exclude_unset=exclude_unset,
        exclude_defaults=exclude_defaults,
        exclude_none=exclude_none,
        custom_encoder=custom_encoder,
        sqlalchemy_safe=sqlalchemy_safe,
    )