
    c                     $    d Z ddZd Zd Zd ZdS )a  
This module serves to convert a data structure composed of Python primitives
(dict, list, tuple, int, str, None) to JSON-serializable primitives (object,
array, number, str, null).

A core feature of this serializer is that the produced will round-trip to
identical objects when deserialized by the standard library json module.
In other words, this property always holds:

    >>> original_data = ... any JSON ...
    >>> encoded = python_to_json(original_data)
    >>> serialized = json.dumps(encoded)
    >>> decoded = json.loads(serialized)
    >>> rehydrated_data = json_to_python(decoded)

    >>> assert rehydrated_data == original_data
    >>> assert encoded == decoded

Couple challenges in straight serialization that this module helps resolve:

- json.dumps() maps both Python list and tuple to JSON array. This module
  provides two variants:

  - In version=1, this module converts Python list `[1, 2, 3]` as-is and
    converts Python tuple `(1, 2, 3)` to special object construct
    `{"$": "t", "items": [1, 2, 3]}`

  - In version=2, it is the other way around, this module converts Python tuple
    `(1, 2, 3)` as-is and converts Python list `[1, 2, 3]` to special object
    construct `{"$": "l", "items": [1, 2, 3]}`

- Python dict keys can be a tuple/dict, but JSON Object keys must be strings
  This module replaces all `dict` keys with `refid` which can be resolved using
  the `encoded["references"][refid]` lookup table. Except there's a small
  optimisation, if the dict key is a string that isn't only numeric, which is
  encoded directly into the object.

- Python dict keys cannot be another dict because it is unhashable, therefore
  there's no encoding for having objects as keys either.

- There is currently no support for floating point numbers.

Note that `json_to_python` only accepts Python objects that can be the output
of `python_to_json`, there is NO guarantee for going the other way around. This
may or may not work:

    >>> python_to_json(json_to_python(original_data)) == original_data

   c                 x    |dvrt          d|           g }|t          | ||          |d}|d         s|d= |S )Nr      Unexpected version )version)vdata
referencesr
   )
ValueError_py2js)or   r
   results       4lib/python3.11/site-packages/rope/base/serializer.pypython_to_jsonr   4   sk    f :8w88999Jq*g666  F
 , !< M    c                     | d         }|dvrt          d|           |                     di           }t          | d         ||          }|S )Nr   r   r   r
   r	   )r   get_js2py)r   r   r
   r	   s       r   json_to_pythonr   B   s\    fGf :8w88999|R((J!F)Z11DKr   c                    t          | t          t          f          s| | S t          | t                    r'dk    rdfd| D             dS fd| D             S t          | t                    r'dk    rdfd| D             dS fd	| D             S t          | t
                    ri }|                                 D ]\  }}|d
k    rt          d          t          |t                    r)|                                st          |          ||<   Xt          |t          t          t          f          s|J t          |t                    rJ t                    }                    t          |                     t          |          |t          |          <   |S t          dt          |            d|            )Nr   tc                 2    g | ]}t          |          S  r   .0itemr
   r   s     r   
<listcomp>z_py2js.<locals>.<listcomp>R   %    JJJ&z7;;JJJr   )$itemsc                 2    g | ]}t          |          S r   r   r   s     r   r   z_py2js.<locals>.<listcomp>U   %    DDD$F4W55DDDr   r   lc                 2    g | ]}t          |          S r   r   r   s     r   r   z_py2js.<locals>.<listcomp>Z   r   r   c                 2    g | ]}t          |          S r   r   r   s     r   r   z_py2js.<locals>.<listcomp>]   r#   r   r    z$dict cannot contain reserved key "$"zObject of type z is not allowed )
isinstancestrinttuplelistdictr!   r   isdigitr   lenappend	TypeErrortype)r   r
   r   r   pykeypyvaluerefids    ``    r   r   r   K   s2   !c3Z   A 	Au		 a< 	EJJJJJJJJ  
 EDDDD!DDDD	At		 a< 	EJJJJJJJJ  
 EDDDD!DDDD	At		 ggii 
	J 
	JNE7| I !GHHH%%% Jemmoo J &w
G D Du!%#sE):;;LuLLL%eT22222J!!&
G"D"DEEE%+GZ%I%Is5zz""
Bd1ggBBqBB
C
CCr   c                 (   t          | t          t          f          s| | S t          | t                    rVdk    rt          fd| D                       S dk    rt	          fd| D                       S t          d           t          | t                    rIi }d| v r| d         dk    r,dk    sJ | d         }t	          fd	|D                       S | d         d
k    r,dk    sJ | d         }t          fd|D                       S t          d| d          d|            |                                 D ]\  }}t          |t                    sJ |	                                r]t          |          }d|cxk    rt                    k     sn J |         }t          |          }t          |          }	|||	<   t          |          ||<   |S t          dt          |           j         d|            )Nr   c              3   :   K   | ]}t          |          V  d S Nr   r   s     r   	<genexpr>z_js2py.<locals>.<genexpr>t   s/      HHdtZ99HHHHHHr   r   c              3   :   K   | ]}t          |          V  d S r7   r8   r   s     r   r9   z_js2py.<locals>.<genexpr>v   s/      IItj'::IIIIIIr   r   r    r   r!   c              3   :   K   | ]}t          |          V  d S r7   r8   r   s     r   r9   z_js2py.<locals>.<genexpr>~   s/      PP4VD*g>>PPPPPPr   r$   c              3   :   K   | ]}t          |          V  d S r7   r8   r   s     r   r9   z_js2py.<locals>.<genexpr>   s/      OO$F4W==OOOOOOr   zUnrecognized object of type:      zObject of type "z" is not allowed )r'   r(   r)   r+   r*   r   r,   r0   r!   r-   r.   r   r1   __name__)
r   r
   r   r   r	   r4   jsvaluejskeyr3   r2   s
    ``       r   r   r   o   s   !c3Z    A  	At		 a< 	JHHHHHaHHHHHH\ 	JIIIIIqIIIIII8w88999	At		 !8 	Iv} P!|###zPPPPP4PPPPPP33 P!|###zOOOOO$OOOOOOHAcFHHQHHIII"#'')) 
I 
Iw!%-----==?? IJJE7777J777777&u-E$Wj'BBG"5*g>>E$+F5MM$*7J$H$HF5MM
MtAww'7MM!MM
N
NNr   N)r   )__doc__r   r   r   r   r   r   r   <module>rC      sd   0 0f     !D !D !DH"O "O "O "O "Or   