ó
`¾Tc        	   @   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) 2012 by Armin Ronacher.
    :license: BSD, see LICENSE for more details.
iÿÿÿÿN(   t   datetimei   (   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(    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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   (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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   sg   t  | t ƒ r t | ƒ St  | t j ƒ r5 t | ƒ St | d ƒ rT 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 ``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   uuidt   UUIDt   strt   hasattrR   R    t   _jsonR   t   default(   t   selft   o(    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/json.pyR&   <   s    

(   t   __name__t
   __module__t   __doc__R&   (    (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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 encoder.  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+   (    (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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(    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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   R1   t   json_decoderR   (   R6   (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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 overriden by the simplejson ``ensure_ascii`` parameter.
    R   N(   R7   t   popt   NoneR%   R   R   R   t   encode(   t   objR6   R   t   rv(    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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(   R7   R:   R;   R   R%   R
   (   R=   R   R6   R   (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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(   R9   R   R   t   decodeR:   R;   R%   R   (   t   sR6   (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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(   R9   R   R   R:   R;   R%   R   (   R   R6   (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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=   R6   R>   (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/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   t   unicodeR   (   R=   R   R6   (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/json.pyR   Ä   s    c          O   sN   d } t j d r& t j r& d } n  t j t t |  | Ž  d | ƒd d ƒS(   s"  Creates a :class:`~flask.Response` with the JSON representation of
    the given arguments with an `application/json` mimetype.  The arguments
    to this function are the same as to the :class:`dict` constructor.

    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
        }

    For security reasons only objects are supported toplevel.  For more
    information about this, have a look at :ref:`json-security`.

    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.

    .. versionadded:: 0.2
    t   JSONIFY_PRETTYPRINT_REGULARi   t   indentt   mimetypes   application/jsonN(   R;   R   R3   R   t   is_xhrt   response_classR   t   dict(   t   argsR6   RE   (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/json.pyR   É   s     
		c         K   s   t  t |  |  ƒ S(   N(   R   R   (   R=   R6   (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/json.pyt   tojson_filterò   s    (#   R+   R   R!   R    t   globalsR   R   t   _compatR   R   t   werkzeug.httpR   t   jinja2R   t   itsdangerousR   R%   t   ImportErrorR   R   RB   t   __all__R   R   R   R   R7   R9   R
   R   R   R   R   R   RK   (    (    (    sP   /var/www/send.findwatt.com/datamanager/lib/python2.7/site-packages/flask/json.pyt   <module>
   s:   				"									#		)