
_                 @   s   d  d l  m Z d  d l Z d  d l Z d d l m Z d d l m Z d d l m	 Z	 y d  d l
 Z Wn e k
 r d  d l Z Yn XGd d   d e  Z Gd	 d
   d
 e  Z d S)    )absolute_importN   )	text_type)
BadRequest)detect_utf_encodingc               @   sF   e  Z d  Z e d d    Z e d d    Z e d d    Z d S)_JSONModulec             C   sd   t  |  t j  r |  j   St  |  t j  r8 t |   St |  d  rW t |  j	    St
    d  S)N__html__)
isinstancedatetimedate	isoformatuuidUUIDstrhasattrr   r   	TypeError)o r   :/tmp/pip-build-5gj8f0j9/Werkzeug/werkzeug/wrappers/json.py_default   s    

z_JSONModule._defaultc             K   sC   | j  d d  | j  d |  j  | j  d d  t j | |  S)N
separators,:default	sort_keysT)r   r   )
setdefaultr   _jsondumps)clsobjkwr   r   r   r      s    z_JSONModule.dumpsc             K   s:   t  |  t  r* t |   } |  j |  }  t j |  |  S)N)r	   bytesr   decoder   loads)sr    encodingr   r   r   r#   %   s    z_JSONModule.loadsN)__name__
__module____qualname__staticmethodr   classmethodr   r#   r   r   r   r   r      s   r   c               @   sy   e  Z d  Z d Z e Z e d d    Z e d d    Z d d   Z	 e
 e
 f Z d d d	 d
 d  Z d d   Z d S)	JSONMixinaD  Mixin to parse :attr:`data` as JSON. Can be mixed in for both
    :class:`~werkzeug.wrappers.Request` and
    :class:`~werkzeug.wrappers.Response` classes.

    If `simplejson`_ is installed it is preferred over Python's built-in
    :mod:`json` module.

    .. _simplejson: https://simplejson.readthedocs.io/en/latest/
    c             C   s
   |  j    S)zThe parsed JSON data if :attr:`mimetype` indicates JSON
        (:mimetype:`application/json`, see :meth:`is_json`).

        Calls :meth:`get_json` with default arguments.
        )get_json)selfr   r   r   json>   s    zJSONMixin.jsonc             C   s1   |  j  } | d k p0 | j d  o0 | j d  S)zCheck if the mimetype indicates JSON data, either
        :mimetype:`application/json` or :mimetype:`application/*+json`.
        zapplication/jsonzapplication/z+json)mimetype
startswithendswith)r-   mtr   r   r   is_jsonG   s    	zJSONMixin.is_jsonc             C   s7   y |  j  d |  SWn t k
 r2 |  j    SYn Xd  S)Ncache)get_datar   )r-   r4   r   r   r   _get_data_for_jsonS   s    zJSONMixin._get_data_for_jsonFTc       
      C   s  | r$ |  j  | t k	 r$ |  j  | S| p0 |  j s7 d S|  j d |  } y |  j j |  } Wn t k
 r } zk | r d } | r |  j  \ } } | | f |  _  n3 |  j |  } | r |  j  \ } }	 | |	 f |  _  WYd d } ~ Xn X| r| | f |  _  | S)a&  Parse :attr:`data` as JSON.

        If the mimetype does not indicate JSON
        (:mimetype:`application/json`, see :meth:`is_json`), this
        returns ``None``.

        If parsing fails, :meth:`on_json_loading_failed` is called and
        its return value is used as the return value.

        :param force: Ignore the mimetype and always try to parse JSON.
        :param silent: Silence parsing errors and return ``None``
            instead.
        :param cache: Store the parsed JSON to return for subsequent
            calls.
        Nr4   )_cached_jsonEllipsisr3   r6   json_moduler#   
ValueErroron_json_loading_failed)
r-   forceZsilentr4   datarveZ	normal_rv_Z	silent_rvr   r   r   r,   ^   s(    "zJSONMixin.get_jsonc             C   s   t  d j |    d S)zCalled if :meth:`get_json` parsing fails and isn't silenced.
        If this method returns a value, it is used as the return value
        for :meth:`get_json`. The default implementation raises
        :exc:`~werkzeug.exceptions.BadRequest`.
        z!Failed to decode JSON object: {0}N)r   format)r-   r?   r   r   r   r;      s    z JSONMixin.on_json_loading_failedN)r&   r'   r(   __doc__r   r9   propertyr.   r3   r6   r8   r7   r,   r;   r   r   r   r   r+   /   s   			-r+   )
__future__r   r
   r   _compatr   
exceptionsr   utilsr   Z
simplejsonr   ImportErrorr.   objectr   r+   r   r   r   r   <module>   s   