
Á«‘_÷  ã               @   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)zµThe 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)z‚Check 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)züCalled 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   