
    Æe                     "    d Z ddZd Zd Zd Zy)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                 b    |dvrt        d|       g }|t        | ||      |d}|d   s|d= |S )N      Unexpected version )version)vdata
referencesr
   )
ValueError_py2js)or   r
   results       6/usr/lib/python3/dist-packages/rope/base/serializer.pypython_to_jsonr   4   sR    f.wi899Jq*g6 F
 ,< M    c                 x    | d   }|dvrt        d|       | j                  di       }t        | d   ||      }|S )Nr   r   r   r
   r	   )r   get_js2py)r   r   r
   r	   s       r   json_to_pythonr   B   sM    fGf.wi899|R(J!F)Z1DKr   c           	         t        | t        t        f      s| | S t        | t              r>|dk(  rd| D cg c]  }t	        |||       c}dS | D cg c]  }t	        |||       c}S t        | t
              r>|dk(  rd| D cg c]  }t	        |||       c}dS | D cg c]  }t	        |||       c}S t        | t              ri }| j                         D ]  \  }}|dk(  rt        d      t        |t              r!|j                         st	        |||      ||<   Gt        |t        t        t        f      s|J t        |t
              rJ t        |      }|j                  t	        |||             t	        |||      |t        |      <    |S t        dt        |        d	|        c c}w c c}w c c}w c c}w )
Nr   t)$itemsr   lr   z$dict cannot contain reserved key "$"zObject of type z is not allowed )
isinstancestrinttupler   listdictr   r   isdigitlenappend	TypeErrortype)r   r
   r   itemr   pykeypyvaluerefids           r   r   r   K   s   !c3Z AI	Au	a<HIJ&z7;J 
 CDD$F4W5DD	At	a<HIJ&z7;J 
 CDD$F4W5DD	At	ggi 
	JNE7| !GHH%%emmo &w
G Du!%#sE):;u}LL%eT222J!!&
G"DE%+GZ%Is5z"
	J 
od1gY.>qcB
CC5 K E
 K Es   F/F4F9 F>c                 J   t        | t        t        f      s| | S t        | t              rBdk(  rt        fd| D              S dk(  rt	        fd| D              S t        d       t        | t              ri }d| v rf| 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|        | j                         D ]  \  }}t        |t              sJ |j                         rIt        |      }d|cxk  rt              k  sJ  J |   }t        |      }t        |      }	|||	<   qt        |      ||<    |S t        dt        |       j                   d|        )Nr   c              3   8   K   | ]  }t        |        y wNr   .0r&   r
   r   s     r   	<genexpr>z_js2py.<locals>.<genexpr>t   s     HdtZ9H   r   c              3   8   K   | ]  }t        |        y wr,   r-   r.   s     r   r0   z_js2py.<locals>.<genexpr>v   s     Itj':Ir1   r   r   r   r   c              3   8   K   | ]  }t        |        y wr,   r-   r.   s     r   r0   z_js2py.<locals>.<genexpr>~   s     P4VD*g>Pr1   r   c              3   8   K   | ]  }t        |        y wr,   r-   r.   s     r   r0   z_js2py.<locals>.<genexpr>   s     O$F4W=Or1   zUnrecognized object of type:      zObject of type "z" is not allowed )r   r   r   r   r   r   r    r$   r   r!   r"   r   r%   __name__)
r   r
   r   r   r	   r)   jsvaluejskeyr(   r'   s
    ``       r   r   r   o   s   !c3Z AI	At	a<HaHHH\IqIII.wi899	At	!8v}!|#|zP4PPP33!|#|zO$OOO;AcF81QCHII"#'') 
Iw!%---==?JE7J77777&u-E$Wj'BG"5*g>E$+F5M$*7J$HF5M
I 
&tAw'7'7&88I!M
NNr   N)r   )__doc__r   r   r   r    r   r   <module>r<      s"   0f!DH"Or   