
    i\                        S SK Jr  S SKrS SKrS SKrS SKJrJr  S SKJ	r	  S SK
Jr  SSKJrJr   " S S	\5      r " S
 S\5      r " S S\5      r\rg)    )annotationsN)ABCabstractmethod)Any)	BaseModel   )get_qualified_name!import_module_from_qualified_namec                  @    \ rS rSrSr\SS j5       r\SS j5       rSrg)	BaseSerializer   an  
Interface for value serialization used by the workflow context and state store.

Implementations must encode arbitrary Python values into a string and be able
to reconstruct the original values from that string.

See Also:
    - [JsonSerializer][workflows.context.serializers.JsonSerializer]
    - [PickleSerializer][workflows.context.serializers.PickleSerializer]
c                    g N selfvalues     W/home/james-whalen/.local/lib/python3.13/site-packages/workflows/context/serializers.py	serializeBaseSerializer.serialize   s    ,/    c                    g r   r   r   s     r   deserializeBaseSerializer.deserialize    s    .1r   r   Nr   r   returnstrr   r   r   r   )	__name__
__module____qualname____firstlineno____doc__r   r   r   __static_attributes__r   r   r   r   r      s%    	 / /1 1r   r   c                  @    \ rS rSrSrS	S jrS
S jrSS jrSS jrSr	g)JsonSerializer$   aF  
JSON-first serializer that understands Pydantic models and LlamaIndex components.

Behavior:
- Pydantic models are encoded as JSON with their qualified class name so they
  can be faithfully reconstructed.
- LlamaIndex components (objects exposing `class_name` and `to_dict`) are
  serialized to their dict form alongside the qualified class name.
- Dicts and lists are handled recursively.

Fallback for unsupported objects is to attempt JSON encoding directly; if it
fails, a `ValueError` is raised.

Examples:
    ```python
    s = JsonSerializer()
    payload = s.serialize({"x": 1, "y": [2, 3]})
    data = s.deserialize(payload)
    assert data == {"x": 1, "y": [2, 3]}
    ```

See Also:
    - [BaseSerializer][workflows.context.serializers.BaseSerializer]
    - [PickleSerializer][workflows.context.serializers.PickleSerializer]
c                   [        US5      (       a  SUR                  5       [        U5      S.nU$ [        U[        5      (       a  SUR                  SS9[        U5      S.$ [        U[        5      (       a4  UR                  5        VVs0 s H  u  p4X0R                  U5      _M     snn$ [        U[        5      (       a!  U Vs/ s H  oPR                  U5      PM     sn$ U$ s  snnf s  snf )a"  
Events with a wrapper type that includes type metadata, so that they can be reserialized into the original Event type.
Traverses dicts and lists recursively.

Args:
    value (Any): The value to serialize.

Returns:
    Any: The serialized value. A dict, list, string, number, or boolean.

class_nameT)__is_componentr   qualified_namejson)mode)__is_pydanticr   r+   )
hasattrto_dictr	   
isinstancer   
model_dumpdictitemsserialize_valuelist)r   r   retvalkvitems         r   r5   JsonSerializer.serialize_value?   s     5,''"&"4U";F
 MeY''!%))v)6"4U";  eT"";@;;=I=41A++A..=IIeT"";@A54((.5AA J Bs   
C#C)c                     U R                  U5      n[        R                  " U5      $ ! [         a    [	        S[        U5       SU< 35      ef = f)zSerialize an arbitrary value to a JSON string.

Args:
    value (Any): The value to encode.

Returns:
    str: JSON string.

Raises:
    ValueError: If the value cannot be encoded to JSON.
zFailed to serialize value: z: )r5   r,   dumps	Exception
ValueErrortype)r   r   serialized_values      r   r   JsonSerializer.serializeb   sW    	U#33E:::.// 	U:4;-r%STT	Us	   &) &Ac                R   [        U[        5      (       a  UR                  S5      (       a8  UR                  S5      (       a"  [        US   5      nUR	                  US   5      $ UR                  S5      (       a8  UR                  S5      (       a"  [        US   5      nUR                  US   5      $ UR                  5        VVs0 s H  u  p4X0R                  U5      _M     snn$ [        U[        5      (       a!  U Vs/ s H  oPR                  U5      PM     sn$ U$ s  snnf s  snf )zHelper to deserialize a single dict or other json value from its discriminator fields back into a python class.

Args:
    data (Any): a dict, list, string, number, or boolean

Returns:
    Any: The deserialized value.
r.   r+   r   r*   )	r1   r3   getr
   model_validate	from_dictr4   deserialize_valuer6   )r   datamodule_classr8   r9   r:   s         r   rG    JsonSerializer.deserialize_valuet   s     dD!!xx((TXX6F-G-G@FVAWX#224=AA*++9I0J0J@FVAWX#--d7m<<=AZZ\J\TQA--a00\JJd##=ABTT**40TBB KBs   D?D$c                P    [         R                  " U5      nU R                  U5      $ )zDeserialize a JSON string into Python objects.

Args:
    value (str): JSON string.

Returns:
    Any: The reconstructed value.
)r,   loadsrG   )r   r   rH   s      r   r   JsonSerializer.deserialize   s#     zz% %%d++r   r   N)r   r   r   r   r   )rH   r   r   r   r   )
r   r    r!   r"   r#   r5   r   rG   r   r$   r   r   r   r&   r&   $   s    4!FU$*
,r   r&   c                  @   ^  \ rS rSrSrSU 4S jjrSU 4S jjrSrU =r$ )PickleSerializer   a  
Hybrid serializer: JSON when possible, Pickle as a safe fallback.

This serializer attempts JSON first for readability and portability, and
transparently falls back to Pickle for objects that cannot be represented in
JSON. Deserialization prioritizes Pickle and falls back to JSON.

Warning:
    Pickle can execute arbitrary code during deserialization. Only
    deserialize trusted payloads.

Note: Used to be called `JsonPickleSerializer` but it was renamed to `PickleSerializer`.

Examples:
    ```python
    s = PickleSerializer()
    class Foo:
        def __init__(self, x):
            self.x = x
    payload = s.serialize(Foo(1))  # will likely use Pickle
    obj = s.deserialize(payload)
    assert isinstance(obj, Foo)
    ```
c                   >  [         TU ]  U5      $ ! [         a<    [        R                  " [
        R                  " U5      5      R                  S5      s $ f = f)zSerialize with JSON preference and Pickle fallback.

Args:
    value (Any): The value to encode.

Returns:
    str: Encoded string (JSON or base64-encoded Pickle bytes).
zutf-8)superr   r>   base64	b64encodepickler=   decoder   r   	__class__s     r   r   PickleSerializer.serialize   sO    	I7$U++ 	I##FLL$78??HH	Is    AAAc                   >  [         R                  " [        R                  " U5      5      $ ! [         a    [
        TU ]  U5      s $ f = f)zDeserialize with Pickle preference and JSON fallback.

Args:
    value (str): Encoded string.

Returns:
    Any: The reconstructed value.

Notes:
    Use only with trusted payloads due to Pickle security implications.
)rU   rL   rS   	b64decoder>   rR   r   rW   s     r   r   PickleSerializer.deserialize   sA    	.<< 0 0 788 	.7&u--	.s   )- A	A	r   r   r   )	r   r    r!   r"   r#   r   r   r$   __classcell__)rX   s   @r   rO   rO      s    2I. .r   rO   )
__future__r   rS   r,   rU   abcr   r   typingr   pydanticr   utilsr	   r
   r   r&   rO   JsonPickleSerializerr   r   r   <module>rd      sO    #    #   H2S 2&o,^ o,d7.~ 7.t ( r   