1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
|
from flask import current_app, json, request
def __pad(strdata):
""" Pads `strdata` with a Request's callback argument, if specified, or does
nothing.
"""
if request.args.get('callback'):
return "%s(%s);" % (request.args.get('callback'), strdata)
else:
return strdata
def __dumps(*args, **kwargs):
""" Serializes `args` and `kwargs` as JSON. Supports serializing an array
as the top-level object, if it is the only argument.
"""
indent = None
if current_app.config.get('JSONIFY_PRETTYPRINT_REGULAR', False) \
and not request.is_xhr:
indent = 2
return json.dumps(args[0] if len(args) is 1 else dict(*args, **kwargs), indent=indent)
def jsonpify(*args, **kwargs):
"""Creates a :class:`~flask.Response` with the JSON or JSON-P representation of
the given arguments with an `application/json` mimetype. The arguments
to this function are the same as to the :class:`dict` constructor, but also
accept an array. If a `callback` is specified in the request arguments, the
response is JSON-Padded.
Example usage::
@app.route('/_get_current_user')
def get_current_user():
return jsonify(username=g.user.username,
email=g.user.email,
id=g.user.id)
GET /_get_current_user:
This will send a JSON response like this to the browser::
{
"username": "admin",
"email": "admin@localhost",
"id": 42
}
or, if a callback is specified,
GET /_get_current_user?callback=displayUsers
Will result in a JSON response like this to the browser::
displayUsers({
"username": "admin",
"email": "admin@localhost",
"id": 42
});
This requires Python 2.6 or an installed version of simplejson. For
security reasons only objects are supported toplevel. For more
information about this, have a look at :ref:`json-security`.
.. versionadded:: 0.2
"""
return current_app.response_class(__pad(__dumps(*args, **kwargs)),
mimetype='application/json')
jsonify = jsonpify # allow override of Flask's jsonify.
|