o
    tBhH(                     @   sF   d dl mZmZmZ ddlmZ ddlmZ G dd deZe Z	dS )    )absolute_importdivisionunicode_literals   )string_types)PY3_ORDERED_DICTc                   @   s   e Zd ZdZd ddZdd Zdd Zd	d
efddZdd Z	d!ddZ
e
Zd!ddZdd ZeZdd Zdd Zdd Zdd Zdd ZdS )"JSONBackenda8  Manages encoding and decoding using various backends.

    It tries these modules in this order:
        simplejson, json, demjson

    simplejson is a fast and popular backend and is tried first.
    json comes with Python and is tried second.
    demjson is the most permissive backend and is tried last.

    Tc                 C   s   || _ g | _i | _i | _i | _i | _i | _d| _| d | d | dddd | dd	d
d | d | d t	 }dd|if}d|ddf|||d| _d S )NF
simplejsonjsondemjsonencodedecodeJSONDecodeErrorjsonlibwriteread	ReadErroryajlujson 	sort_keys)r   escape_forward_slashes)r   r
   r	   zdjango.util.simplejson)
_fallthrough_backend_names	_encoders	_decoders_encoder_options_decoder_options_decoder_exceptions	_verifiedload_backendr   )selffallthroughsort	json_optsr   r   i/var/www/html/riverr-enterprise-integrations-main/venv/lib/python3.10/site-packages/jsonpickle/backend.py__init__   s*   



zJSONBackend.__init__c                 C   s   | j rdS td)z4Ensures that we've loaded at least one JSON backend.NzXjsonpickle requires at least one of the following:
    python2.6, simplejson, or demjson)r   AssertionError)r!   r   r   r%   _verify:   s
   zJSONBackend._verifyc                 C   s
   || _ dS )a  
        Disable jsonpickle's fallthrough-on-error behavior

        By default, jsonpickle tries the next backend when decoding or
        encoding using a backend fails.

        This can make it difficult to force jsonpickle to use a specific
        backend, and catch errors, because the error will be suppressed and
        may not be raised by the subsequent backend.

        Calling `enable_backend(False)` will make jsonpickle immediately
        re-raise any exceptions raised by the backends.

        N)r   )r!   enabler   r   r%   enable_fallthroughD   s   
zJSONBackend.enable_fallthroughdumpsloadsc                 C   s   zt |}W n
 ty   Y dS w z|ddd D ]}t||}qW n
 ty.   Y dS w | | j|||rA| | j|||sCdS t|t	rT| | j
|||sSdS n|| j
|< | j|g i f | j|g i f | j| d| _dS )a  Load a JSON backend by name.

        This method loads a backend and sets up references to that
        backend's loads/dumps functions and exception classes.

        :param dumps: is the name of the backend's encode method.
          The method should take an object and return a string.
          Defaults to 'dumps'.
        :param loads: names the backend's method for the reverse
          operation -- returning a Python object from a string.
        :param loads_exc: can be either the name of the exception class
          used to denote decoding errors, or it can be a direct reference
          to the appropriate exception class itself.  If it is a name,
          then the assumption is that an exception class of that name
          can be found in the backend module's namespace.
        :param load: names the backend's 'load' method.
        :param dump: names the backend's 'dump' method.
        :rtype bool: True on success, False if the backend could not be loaded.

        F.r   NT)
__import__ImportErrorsplitgetattrAttributeError_storer   r   
isinstancer   r   r   
setdefaultr   r   appendr   )r!   namer+   r,   	loads_excmodattrr   r   r%   r    U   s4   


zJSONBackend.load_backendc                 C   sl   | j |d | j|d | j|d | j|d | j|d || jv r.| j| t| j| _	dS )z,Remove all entries for a particular backend.N)
r   popr   r   r   r   r   removeboolr   )r!   r7   r   r   r%   remove_backend   s   
zJSONBackend.remove_backendNc                 C   s   |    | js| jd }| j||||dS t| jD ]-\}}z| j||||dW   S  tyG } z|t| jd kr=|W Y d}~qd}~ww dS )z
        Attempt to encode an object into JSON.

        This tries the loaded backends in order and passes along the last
        exception if no backend is able to encode the object.

        r   )indent
separatorsr   N)r(   r   r   backend_encode	enumerate	Exceptionlen)r!   objr?   r@   r7   idxer   r   r%   r      s    
zJSONBackend.encodec           	      C   s`   | j |g i f\}}| }|d ur||d< |d ur||d< |ft| }| j| |i |S )Nr?   r@   )r   getcopytupler   )	r!   r7   rE   r?   r@   optargs	optkwargsencoder_kwargsencoder_argsr   r   r%   rA      s   zJSONBackend.backend_encodec                 C   s   |    | js| jd }| ||S t| jD ].\}}z	| ||W   S  | j| yE } z|t| jd kr:|	 W Y d}~qd}~ww dS )z
        Attempt to decode an object from a JSON string.

        This tries the loaded backends in order and passes along the last
        exception if no backends are able to decode the string.

        r   r   N)r(   r   r   backend_decoderB   r   rD   )r!   stringr7   rF   rG   r   r   r%   r      s   
zJSONBackend.decodec                 C   s:   | j |di f\}}| }| j| |g|R i |S )Nr   )r   rH   rI   r   )r!   r7   rP   rK   rL   decoder_kwargsr   r   r%   rO      s   zJSONBackend.backend_decodec                 C   s8   || j v r| j | | j d| dS d| }t|)a  
        Set the preferred json backend.

        If a preferred backend is set then jsonpickle tries to use it
        before any other backend.

        For example::

            set_preferred_backend('simplejson')

        If the backend is not one of the built-in jsonpickle backends
        (json/simplejson, or demjson) then you must load the backend
        prior to calling set_preferred_backend.

        AssertionError is raised if the backend has not been loaded.

        r   z%The "%s" backend has not been loaded.N)r   r<   insertr'   )r!   r7   errmsgr   r   r%   set_preferred_backend   s
   
z!JSONBackend.set_preferred_backendc                 O      ||f| j |< dS )a  
        Associate encoder-specific options with an encoder.

        After calling set_encoder_options, any calls to jsonpickle's
        encode method will pass the supplied args and kwargs along to
        the appropriate backend's encode method.

        For example::

            set_encoder_options('simplejson', sort_keys=True, indent=4)
            set_encoder_options('demjson', compactly=False)

        See the appropriate encoder's documentation for details about
        the supported arguments and keyword arguments.

        N)r   r!   r7   argskwargsr   r   r%   set_encoder_options      zJSONBackend.set_encoder_optionsc                 O   rU   )a   
        Associate decoder-specific options with a decoder.

        After calling set_decoder_options, any calls to jsonpickle's
        decode method will pass the supplied args and kwargs along to
        the appropriate backend's decode method.

        For example::

            set_decoder_options('simplejson', encoding='utf8', cls=JSONDecoder)
            set_decoder_options('demjson', strict=True)

        See the appropriate decoder's documentation for details about
        the supported arguments and keyword arguments.

        N)r   rV   r   r   r%   set_decoder_options
  rZ   zJSONBackend.set_decoder_optionsc                 C   s4   z
t ||||< W dS  ty   | | Y dS w )NFT)r1   r2   r>   )r!   dctbackendrE   r7   r   r   r%   r3     s   
zJSONBackend._store)T)NN)__name__
__module____qualname____doc__r&   r(   r*   
ValueErrorr    r>   r   r+   rA   r   r,   rO   rT   rY   r[   r3   r   r   r   r%   r      s"    
'
;


r   N)

__future__r   r   r   compatr   r   objectr   r
   r   r   r   r%   <module>   s      
!