U
    Aޫ^                     @   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	 zd dl
ZW n ek
rh   d dlZY nX G dd deZG d	d
 d
eZdS )    )absolute_importN   )	text_type)
BadRequest)detect_utf_encodingc                   @   s0   e Zd Zedd Zedd Zedd ZdS )_JSONModulec                 C   sH   t | tjr|  S t | tjr(t| S t| dr>t| 	 S t
 d S )N__html__)
isinstancedatetimedate	isoformatuuidUUIDstrhasattrr   r   	TypeError)o r   :/tmp/pip-unpacked-wheel-74fb410_/werkzeug/wrappers/json.py_default   s    
z_JSONModule._defaultc                 K   s4   | dd | d| j | dd tj|f|S )N
separators),:default	sort_keysT)
setdefaultr   _jsondumps)clsobjkwr   r   r   r      s    z_JSONModule.dumpsc                 K   s*   t | trt| }| |} tj| f|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                   @   sN   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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   |   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   s"   | j }|dkp |do |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   s0   z| j |dW S  tk
r*   |    Y S X d S )Ncache)get_datar   )r-   r5   r   r   r   _get_data_for_jsonS   s    zJSONMixin._get_data_for_jsonFTc           
   
   C   s   |r| j | tk	r| j | S |s*| js*dS | j|d}z| j|}W nf tk
r } zH|rzd}|r| j \}}||f| _ n"| |}|r| j \}}	||	f| _ W 5 d}~X Y n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   r7   json_moduler#   
ValueErroron_json_loading_failed)
r-   forceZsilentr5   datarveZ	normal_rv_Z	silent_rvr   r   r   r,   ^   s(    





zJSONMixin.get_jsonc                 C   s   t d|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)FFT)r&   r'   r(   __doc__r   r:   propertyr.   r3   r7   r9   r8   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   