3
 [ð]÷  ã               @   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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| jƒ S t | tjƒr(t| ƒS t| dƒr>t| j	ƒ ƒS t
ƒ ‚d S )NÚ__html__)Ú
isinstanceÚdatetimeÚdateÚ	isoformatÚuuidÚUUIDÚstrÚhasattrr   r   Ú	TypeError)Úo© r   ú:/tmp/pip-build-w1as9q70/Werkzeug/werkzeug/wrappers/json.pyÚ_default   s    
z_JSONModule._defaultc             K   s4   |j ddƒ |j d| jƒ |j ddƒ tj|f|Ž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| 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   | 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   s"   | j }|dkp |jdƒo |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   s*   y| j |d�S  tk
r$   | j ƒ S X d 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 |p$| js*dS | j|d�}y| jj|ƒ}W nf tk
r¬ } zJ|rzd}|rœ| j \}}||f| _ n"| j|ƒ}|rœ| j \}}	||	f| _ W Y dd}~X 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.
        N)r4   )Ú_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)FFT)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   