U
    e+                  4   @   s  U d dl Z d dlZd dlmZmZ d dlmZ d dlmZ d dl	m
Z
mZmZmZmZmZ d dlmZmZ d dlmZ d dlmZ d d	lmZmZmZmZmZmZmZmZ d d
l m!Z! d dl"m#Z# d dl$m%Z% d dl&m'Z' d dl(m)Z)m*Z* d dl+m,Z,m-Z- d dl.m/Z/m0Z0 ddl1m2Z2m3Z3m4Z4 eej5ej6f e7dddZ8eee9e:f dddZ;e<dd e'e7ej5e8eje8ej6e8ej=dd ee;edd e>e?ee?ee?e
e7ee7ee7ee7ee7ee7e*e7ee7edd e,e7e-e7e@e?e!e7e3e7e)e7iZAeee eegef f eBd< eeeegef f eeegef eedf f d d!d"ZCeCeAZDd1e/ee0d%f e/ee# e0d&f e/ee# e0d'f e/eEe0d(f e/eEe0d)f e/eEe0d*f e/eEe0d+f e/eeeeegef f  e0d,f e/eEe0d-f ed.
d/d0ZFdS )2    N)defaultdictdeque)Decimal)Enum)IPv4AddressIPv4InterfaceIPv4NetworkIPv6AddressIPv6InterfaceIPv6Network)PathPurePath)Pattern)GeneratorType)AnyCallableDictListOptionalTupleTypeUnion)UUID)IncEx)	BaseModel)Color)AnyUrl	NameEmail)SecretBytes	SecretStr)	AnnotatedDoc   )PYDANTIC_V2Url_model_dump)oreturnc                 C   s   |   S N)	isoformatr&    r+   4/tmp/pip-unpacked-wheel-8a3sx6sf/fastapi/encoders.pyr)      s    r)   )	dec_valuer'   c                 C   s"   |   jdkrt| S t| S dS )a  
    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
    r   N)as_tupleexponentintfloat)r-   r+   r+   r,   decimal_encoder%   s    r2   c                 C   s   |   S r(   )decoder*   r+   r+   r,   <lambda>;       r4   c                 C   s   |   S r(   )total_seconds)tdr+   r+   r,   r4   @   r5   c                 C   s   | j S r(   )valuer*   r+   r+   r,   r4   B   r5   c                 C   s   | j S r(   )patternr*   r+   r+   r,   r4   N   r5   ENCODERS_BY_TYPE.)type_encoder_mapr'   c                 C   s0   t t}|  D ]\}}||  |f7  < q|S r(   )r   tupleitems)r;   encoders_by_class_tuplestype_encoderr+   r+   r,   !generate_encoders_by_class_tuplesX   s    rA   TFz>
            The input object to convert to JSON.
            z}
            Pydantic's `include` parameter, passed to Pydantic models to set the
            fields to include.
            z}
            Pydantic's `exclude` parameter, passed to Pydantic models to set the
            fields to exclude.
            ap  
            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`.
            z
            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).
            z
            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.
            z
            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.
            z
            Pydantic's `custom_encoder` parameter, passed to Pydantic models to define
            a custom encoder.
            aO  
            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.
            )
objincludeexcludeby_aliasexclude_unsetexclude_defaultsexclude_nonecustom_encodersqlalchemy_safer'   c	                 C   sd  |pi }|rPt | |kr(|t |  | S | D ]\}	}
t| |	r0|
|   S q0|dk	rnt|ttfsnt|}|dk	rt|ttfst|}t| tri }tst| jdi }|r|	| t
| d||||||d}d|kr|d }t|||||dS t| r$t| }t|||||||||d	S t| tr6| jS t| trJt| S t| tttt dfrf| S t| tr.i }t|  }|dk	r|t|M }|dk	r|t|8 }|  D ]r\}}|rt|tr|ds|dk	s|s||krt||||||d	}t||||||d	}|||< q|S t| ttttttfrzg }| D ]&}|t|||||||||d	 qN|S t | tkrtt |  | S t D ]"\}}t| |r||   S qzt| }W nx tk
rH } zXg }|| zt | }W n8 tk
r6 } z|| t!||W 5 d}~X Y nX W 5 d}~X Y nX t|||||||||d	S )
a  
    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/).
    NZjson_encodersjson)moderC   rD   rE   rF   rH   rG   Z__root__)rH   rG   rI   rJ   )rC   rD   rE   rF   rG   rH   rI   rJ   Z_sa)rE   rF   rH   rI   rJ   )"typer=   
isinstancesetdictr   r#   getattrZ
__config__updater%   jsonable_encoderdataclassesZis_dataclassZasdictr   r8   r   strr0   r1   keys
startswithlist	frozensetr   r<   r   appendr:   r>   	Exceptionvars
ValueError)rB   rC   rD   rE   rF   rG   rH   rI   rJ   Zencoder_typeZencoder_instanceencodersZobj_dictZencoded_dictZallowed_keyskeyr8   Zencoded_keyZencoded_valueZencoded_listitemr@   Zclasses_tupledataeerrorsr+   r+   r,   rS   f   s   f






	

.rS   )NNTFFFNT)GrT   datetimecollectionsr   r   decimalr   enumr   	ipaddressr   r   r   r	   r
   r   pathlibr   r   rer   typesr   typingr   r   r   r   r   r   r   r   uuidr   Zfastapi.typesr   Zpydanticr   Zpydantic.colorr   Zpydantic.networksr   r   Zpydantic.typesr   r   Ztyping_extensionsr    r!   _compatr#   r$   r%   datetimerU   r)   r0   r1   r2   bytes	timedeltarY   rX   rO   r:   __annotations__rA   r>   boolrS   r+   r+   r+   r,   <module>   s.    (                          " 	  
 
 	 	  		

		