
    [e&                        d Z ddlZddlZddlZddlZddl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  ej                   d      Z ej                   d      Zej&                  ej(                  ej*                  ej,                  ej.                  ej0                  ej2                  ej4                  ej6                  ej8                  ej:                  ej<                  gZe e! e"d      e#e$fZ%	 	 	 dd
Z&ejN                  Z'ejP                  Z(e&fdZ)e&d	fdZ*d Z+ddZ,ddZ-	 ddl.Z.e.j^                  ja                  e1de2de3df        e.jh                  e1       y# e5$ r Y yw xY w)a  
JSON related utilities.

This module provides a few things:

#. A handy function for getting an object down to something that can be
   JSON serialized.  See :func:`.to_primitive`.
#. Wrappers around :func:`.loads` and :func:`.dumps`. The :func:`.dumps`
   wrapper will automatically use :func:`.to_primitive` for you if needed.
#. This sets up ``anyjson`` to use the :func:`.loads` and :func:`.dumps`
   wrappers if ``anyjson`` is available.
    N)client)encodeutils)importutils)	timeutils	ipaddressnetaddrutf-8c           	      2    |}|t         }t         t              r S t         t              r j	                  |      S t         t
        j                        r,t        j                  t         j                               dd   t         t        j                        r#|r j                  t        j                        S  S t         t        j                        rt               S t        r5t         t        j                   t        j"                  f      rt               S t$        r5t         t$        j&                  t$        j(                  f      rt               S t         t*              rt-               S t/               t0        j2                  u r |       S t5         fdt6        D              r |       S ||kD  ry	 t9        j:                  t<        ||||||      }t         t>              r0 jA                         D 	
ci c]  \  }	}
 ||	       ||
       c}
}	S tC         d      r$ |t?         jE                               |dz         S tC         d	      r$ |t?         jA                               |dz         S tC         d
      r/t         tF        jH                        stK        tM        |             S |r#tC         d      r | jN                  |dz         S |tS        d d       |       S c c}
}	w # tP        $ r  |       cY S w xY w)a8  Convert a complex object into primitives.

    Handy for JSON serialization. We can optionally handle instances,
    but since this is a recursive function, we could have cyclical
    data structures.

    To handle cyclical data structures we could track the actual objects
    visited in a set, but not all objects are hashable. Instead we just
    track the depth of the object inspections and don't go too deep.

    Therefore, ``convert_instances=True`` is lossy ... be aware.

    If the object cannot be converted to primitive, it is returned unchanged
    if fallback is not set, return fallback(value) otherwise.

    .. versionchanged:: 2.22
       Added *fallback* parameter.

    .. versionchanged:: 1.3
       Support UUID encoding.

    .. versionchanged:: 1.6
       Dictionary keys are now also encoded.
    N)encoding   c              3   .   K   | ]  } |        y w)N ).0testvalues     >/usr/lib/python3/dist-packages/oslo_serialization/jsonutils.py	<genexpr>zto_primitive.<locals>.<genexpr>   s     
544;
5s   )convert_instancesconvert_datetimelevel	max_depthr   fallback	iteritems   )r   items__iter____dict__zCannot convert z to primitive)*str
isinstance_simple_typesbytesdecode	xmlrpclibDateTimedatetimetuple	timetuplestrftimer   PERFECT_TIME_FORMATuuidUUIDr   	IPAddress	IPNetworkr   IPv4AddressIPv6Address	Exceptionreprtype	itertoolscountany_nasty_type_tests	functoolspartialto_primitivedictr   hasattrr   ioIOBaselistmapr   	TypeError
ValueError)r   r   r   r   r   r   r   orig_fallback	recursivekvs   `          r   r9   r9   :   s   6 M %'%||X|..
 %++,!!5):#;BQ#?@%**+>>)"?"?@@L%#5z:eg&7&79J9J%KL5zZ!*!6!6!*!6!6!89 5z %#E{ E{ioo%

5#4
55y%%l8I7G,109/7/<>	 eT" %/1 aL)A,. / /UK(T%//"34EAIFFUG$T%++-0	BBUJ'
5"))0LIu-..75*#= U^^519== UDEE+/   s=   AL K<L #/L /L :L >$L <L LLc                 2    t        j                  | fd|i|S )a  Serialize ``obj`` to a JSON formatted ``str``.

    :param obj: object to be serialized
    :param default: function that returns a serializable version of an object,
                    :func:`to_primitive` is used by default.
    :param kwargs: extra named parameters, please see documentation     of `json.dumps <https://docs.python.org/2/library/json.html#basic-usage>`_
    :returns: json formatted string

    Use dump_as_bytes() to ensure that the result type is ``bytes`` on Python 2
    and Python 3.
    default)jsondumps)objrG   kwargss      r   rI   rI      s     ::c575f55    c                 <    t        | fd|i|j                  |      S )a  Serialize ``obj`` to a JSON formatted ``bytes``.

    :param obj: object to be serialized
    :param default: function that returns a serializable version of an object,
                    :func:`to_primitive` is used by default.
    :param encoding: encoding used to encode the serialized JSON output
    :param kwargs: extra named parameters, please see documentation     of `json.dumps <https://docs.python.org/2/library/json.html#basic-usage>`_
    :returns: json formatted string

    .. versionadded:: 1.10
    rG   )rI   encode)rJ   rG   r   rK   s       r   dump_as_bytesrO      s$     0g0077AArL   c                 f    |j                  dt              }t        j                  | |g|d|i|S )a  Serialize ``obj`` as a JSON formatted stream to ``fp``

    :param obj: object to be serialized
    :param fp: a ``.write()``-supporting file-like object
    :param default: function that returns a serializable version of an object,
                    :func:`to_primitive` is used by default.
    :param args: extra arguments, please see documentation     of `json.dump <https://docs.python.org/2/library/json.html#basic-usage>`_
    :param kwargs: extra named parameters, please see documentation     of `json.dump <https://docs.python.org/2/library/json.html#basic-usage>`_

    .. versionchanged:: 1.3
       The *default* parameter now uses :func:`to_primitive` by default.
    rG   )getr9   rH   dump)rJ   fpargsrK   rG   s        r   rR   rR      s4     jjL1G99S"??g???rL   c                 V    t        j                  t        j                  | |      fi |S )aZ  Deserialize ``s`` (a ``str`` or ``unicode`` instance containing a JSON

    :param s: string to deserialize
    :param encoding: encoding used to interpret the string
    :param kwargs: extra named parameters, please see documentation     of `json.loads <https://docs.python.org/2/library/json.html#basic-usage>`_
    :returns: python object
    )rH   loadsr   safe_decode)sr   rK   s      r   rV   rV      s%     ::k--a:EfEErL   c                 `    t        j                   t        j                  |      |       fi |S )aP  Deserialize ``fp`` to a Python object.

    :param fp: a ``.read()`` -supporting file-like object
    :param encoding: encoding used to interpret the string
    :param kwargs: extra named parameters, please see documentation     of `json.loads <https://docs.python.org/2/library/json.html#basic-usage>`_
    :returns: python object
    )rH   loadcodecs	getreader)rS   r   rK   s      r   rZ   rZ      s+     99/V%%h/3>v>>rL   rI   rV   rZ   )FTr      r	   N)r	   )6__doc__r[   r%   r7   inspectr<   r3   rH   r*   xmlrpcr   r#   
oslo_utilsr   r   r   
try_importr   r   ismoduleisclassismethod
isfunctionisgeneratorfunctionisgeneratoristracebackisframeiscode	isbuiltin	isroutine
isabstractr6   r   intr2   boolfloatr    r9   JSONEncoderJSONDecoderrI   rO   rR   rV   rZ   anyjson_modulesappend__name__r@   rA   force_implementationImportErrorr   rL   r   <module>rz      s{  $     	    & " "  "K"";/	
 +
 
 
+%%w8H8H'')D)D(('*=*=w^^W%6%68I8I''	)  c4:tU3 CG07x v  $ 6   ,g B @&	F	?+ Xw	'.
FD E G  *  		s   (E   E('E(