ó
6êWc        	   @   s  d  Z  d d l Z d d l Z d d l m Z d d l m Z m Z d d l m	 Z	 m
 Z
 d d l m Z d d l m Z y d d	 l m Z Wn! e k
 r± d d
 l m Z n Xd e j d  k Z d d d d d d d d d g	 Z d   Z d   Z d e j f d     YZ d e j f d     YZ d   Z d   Z d   Z d   Z d   Z d   Z d    Z  d!   Z! d"   Z" d#   Z# d S($   sÀ   
    flask.jsonimpl
    ~~~~~~~~~~~~~~

    Implementation helpers for the JSON support in Flask.

    :copyright: (c) 2015 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿN(   t   datei   (   t   current_appt   request(   t	   text_typet   PY2(   t	   http_date(   t   Markup(   t
   simplejson(   t   jsons   \/t   /t   dumpt   dumpst   loadt   loadst   htmlsafe_dumpt   htmlsafe_dumpst   JSONDecodert   JSONEncodert   jsonifyc         C   s:   t  |  j d  t  r6 t j t j |   |  }  n  |  S(   Ni    (   t
   isinstancet   readt   bytest   iot   TextIOWrappert   BufferedReader(   t   fpt   encoding(    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyt   _wrap_reader_for_text&   s    c         C   s;   y |  j  d  Wn# t k
 r6 t j |  |  }  n X|  S(   Nt    (   t   writet	   TypeErrorR   R   (   R   R   (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyt   _wrap_writer_for_text,   s
    c           B   s   e  Z d  Z d   Z RS(   sP  The default Flask JSON encoder.  This one extends the default simplejson
    encoder by also supporting ``datetime`` objects, ``UUID`` as well as
    ``Markup`` objects which are serialized as RFC 822 datetime strings (same
    as the HTTP date format).  In order to support more data types override the
    :meth:`default` method.
    c         C   sm   t  | t  r t | j    St  | t j  r; t |  St | d  rZ t | j	    St
 j j |  |  S(   s&  Implement this method in a subclass such that it returns a
        serializable object for ``o``, or calls the base implementation (to
        raise a :exc:`TypeError`).

        For example, to support arbitrary iterators, you could implement
        default like this::

            def default(self, o):
                try:
                    iterable = iter(o)
                except TypeError:
                    pass
                else:
                    return list(iterable)
                return JSONEncoder.default(self, o)
        t   __html__(   R   R    R   t	   timetuplet   uuidt   UUIDt   strt   hasattrR   R    t   _jsonR   t   default(   t   selft   o(    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR'   <   s    
(   t   __name__t
   __module__t   __doc__R'   (    (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR   4   s   c           B   s   e  Z d  Z RS(   s  The default JSON decoder.  This one does not change the behavior from
    the default simplejson decoder.  Consult the :mod:`json` documentation
    for more information.  This decoder is not only used for the load
    functions of this module but also :attr:`~flask.Request`.
    (   R*   R+   R,   (    (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR   V   s   c         C   sw   t  rS |  j d t  j  t  j d s9 |  j d t  n  |  j d t  j d  n  |  j d t  |  j d t  d S(   s,   Inject default arguments for dump functions.t   clst   JSON_AS_ASCIIt   ensure_asciit	   sort_keyst   JSON_SORT_KEYSN(   R   t
   setdefaultt   json_encodert   configt   Falset   TrueR   (   t   kwargs(    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyt   _dump_arg_defaults^   s    c         C   s0   t  r |  j d t  j  n |  j d t  d S(   s,   Inject default arguments for load functions.R-   N(   R   R2   t   json_decoderR   (   R7   (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyt   _load_arg_defaultsj   s    c         K   s_   t  |  | j d d  } t j |  |  } | d k	 r[ t | t  r[ | j |  } n  | S(   så  Serialize ``obj`` to a JSON formatted ``str`` by using the application's
    configured encoder (:attr:`~flask.Flask.json_encoder`) if there is an
    application on the stack.

    This function can return ``unicode`` strings or ascii-only bytestrings by
    default which coerce into unicode strings automatically.  That behavior by
    default is controlled by the ``JSON_AS_ASCII`` configuration variable
    and can be overridden by the simplejson ``ensure_ascii`` parameter.
    R   N(   R8   t   popt   NoneR&   R   R   R   t   encode(   t   objR7   R   t   rv(    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR   r   s    

c         K   sQ   t  |  | j d d  } | d k	 r: t | |  } n  t j |  | |  d S(   s1   Like :func:`dumps` but writes into a file object.R   N(   R8   R;   R<   R   R&   R
   (   R>   R   R7   R   (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR
      s
    
c         K   sM   t  |  t |  t  r= |  j | j d d  p4 d  }  n  t j |  |  S(   sµ   Unserialize a JSON object from a string ``s`` by using the application's
    configured decoder (:attr:`~flask.Flask.json_decoder`) if there is an
    application on the stack.
    R   s   utf-8N(   R:   R   R   t   decodeR;   R<   R&   R   (   t   sR7   (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR      s    
$c         K   sD   t  |  t s4 t |  | j d d  p+ d  }  n  t j |  |  S(   s5   Like :func:`loads` but reads from a file object.
    R   s   utf-8N(   R:   R   R   R;   R<   R&   R   (   R   R7   (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR      s    
$c         K   s^   t  |  |  j d d  j d d  j d d  j d d  } t sZ | j d	 d
  } n  | S(   s:  Works exactly like :func:`dumps` but is safe for use in ``<script>``
    tags.  It accepts the same arguments and returns a JSON string.  Note that
    this is available in templates through the ``|tojson`` filter which will
    also mark the result as safe.  Due to how this function escapes certain
    characters this is safe even if used outside of ``<script>`` tags.

    The following characters are escaped in strings:

    -   ``<``
    -   ``>``
    -   ``&``
    -   ``'``

    This makes it safe to embed such strings in any place in HTML with the
    notable exception of double quoted attributes.  In that case single
    quote your attributes or HTML escape it in addition.

    .. versionchanged:: 0.10
       This function's return value is now always safe for HTML usage, even
       if outside of script tags or if used in XHTML.  This rule does not
       hold true when using this function in HTML attributes that are double
       quoted.  Always single quote attributes if you use the ``|tojson``
       filter.  Alternatively use ``|tojson|forceescape``.
    u   <u   \u003cu   >u   \u003eu   &u   \u0026u   'u   \u0027s   \/R	   (   R   t   replacet   _slash_escape(   R>   R7   R?   (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR   ¡   s    c         K   s    | j  t t |  |    d S(   s:   Like :func:`htmlsafe_dumps` but writes into a file object.N(   R   R   R   (   R>   R   R7   (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR   Ä   s    c          O   sª   d } d } t j d r2 t j r2 d } d } n  |  rM | rM t d   n+ t |   d k rl |  d	 } n |  pu | } t j t | d
 | d | d f d t j d S(   s=  This function wraps :func:`dumps` to add a few enhancements that make
    life easier.  It turns the JSON output into a :class:`~flask.Response`
    object with the :mimetype:`application/json` mimetype.  For convenience, it
    also converts multiple arguments into an array or multiple keyword arguments
    into a dict.  This means that both ``jsonify(1,2,3)`` and
    ``jsonify([1,2,3])`` serialize to ``[1,2,3]``.

    For clarity, the JSON serialization behavior has the following differences
    from :func:`dumps`:

    1. Single argument: Passed straight through to :func:`dumps`.
    2. Multiple arguments: Converted to an array before being passed to
       :func:`dumps`.
    3. Multiple keyword arguments: Converted to a dict before being passed to
       :func:`dumps`.
    4. Both args and kwargs: Behavior undefined and will throw an exception.

    Example usage::

        from flask import jsonify

        @app.route('/_get_current_user')
        def get_current_user():
            return jsonify(username=g.user.username,
                           email=g.user.email,
                           id=g.user.id)

    This will send a JSON response like this to the browser::

        {
            "username": "admin",
            "email": "admin@localhost",
            "id": 42
        }


    .. versionchanged:: 0.11
       Added support for serializing top-level arrays. This introduces a
       security risk in ancient browsers. See :ref:`json-security` for details.

    This function's response will be pretty printed if it was not requested
    with ``X-Requested-With: XMLHttpRequest`` to simplify debugging unless
    the ``JSONIFY_PRETTYPRINT_REGULAR`` config parameter is set to false.
    Compressed (not pretty) formatting currently means no indents and no
    spaces after separators.

    .. versionadded:: 0.2
    t   ,t   :t   JSONIFY_PRETTYPRINT_REGULARi   s   , s   : s=   jsonify() behavior undefined when passed both args and kwargsi   i    t   indentt
   separatorss   
t   mimetypet   JSONIFY_MIMETYPEN(   RD   RE   (   s   , s   : (	   R<   R   R4   R   t   is_xhrR   t   lent   response_classR   (   t   argsR7   RG   RH   t   data(    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyR   É   s    2	c         K   s   t  t |  |   S(   N(   R   R   (   R>   R7   (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyt   tojson_filter  s    ($   R,   R   R"   t   datetimeR    t   globalsR   R   t   _compatR   R   t   werkzeug.httpR   t   jinja2R   t   itsdangerousR   R&   t   ImportErrorR   R   RC   t   __all__R   R   R   R   R8   R:   R
   R   R   R   R   R   RP   (    (    (    s]   /private/var/folders/3n/6h2rwf155rn1m71wwyxn79n80000gn/T/pip-build-bTXgOW/flask/flask/json.pyt   <module>
   s:   				"									#		F