o
    qmiG                  
   @   s   d Z ddlZddlZddlZddl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 ddlZddlmZ dd	lmZmZmZ eeeef  Zeeeef ef Zdd
edeeeeegef f  defddZdS )z
jsonable_encoder converts a Python object to a JSON-friendly dict
(e.g. datetimes to strings, Pydantic models to dicts).

Taken from FastAPI, and made a bit simpler
https://github.com/tiangolo/fastapi/blob/master/fastapi/encoders.py
    N)Enum)PurePath)GeneratorType)AnyCallableDictListOptionalSetUnion   )serialize_datetime)IS_PYDANTIC_V2encode_by_typeto_jsonable_with_fallbackobjcustom_encoderreturnc                    s   pi   r(t |  v r t |  | S   D ]\}}t| |r'||   S qt| tjrbtr8t| jdi }nt| jdi } rF|	  | j
dd}d|v rT|d }d|v r\|d }t||dS t| rrt| }t| dS t| trt| dS t| tr| jS t| trt| S t| tttt d fr| S t| tjrt| S t| tjrt| S t| t
ri }t|  }|  D ]\}}	||v rt| d}
t|	 d}|||
< q|S t| ttt t!t"frg }| D ]}|#t| d q|S dt$d	t$f fd
d}t%| |S )Njson_encodersT)by_alias__root__rootr   zutf-8or   c                    s   t | }|d ur
|S zt| }W n6 tyF } z*g }|| zt| }W n ty; } z
|| t||d }~ww W Y d }~nd }~ww t| dS )Nr   )r   dict	Exceptionappendvars
ValueErrorjsonable_encoder)r   attempt_encodedataeerrorsr    R/home/ubuntu/.local/lib/python3.10/site-packages/sarvamai/core/jsonable_encoder.pyfallback_serializerS   s&   


z-jsonable_encoder.<locals>.fallback_serializer)&typeitems
isinstancepydantic	BaseModelr   getattrmodel_config
__config__updater   r   dataclassesis_dataclassasdictbytesbase64	b64encodedecoder   valuer   strintfloatdtdatetimer   datesetkeyslist	frozensetr   tupler   r   r   )r   r   encoder_typeencoder_instanceencoderobj_dictencoded_dictallowed_keyskeyr7   encoded_keyencoded_valueencoded_listitemr&   r$   r   r%   r      sh   








r   )N)__doc__r4   r0   r<   r;   enumr   pathlibr   typesr   typingr   r   r   r   r	   r
   r   r*   datetime_utilsr   pydantic_utilitiesr   r   r   r9   r8   	SetIntStrDictIntStrAnyr   r$   r$   r$   r%   <module>   s   $2