Added the JSONIFY_PRETTYPRINT_REGULAR config variable. This fixes #725

This commit is contained in:
Armin Ronacher 2013-06-01 00:20:00 +01:00
parent 1c8c21abd5
commit 3d9055b3b7
4 changed files with 18 additions and 3 deletions

View file

@ -62,6 +62,7 @@ Release date to be decided.
- Changed how the teardown system is informed about exceptions. This is now - Changed how the teardown system is informed about exceptions. This is now
more reliable in case something handles an exception halfway through more reliable in case something handles an exception halfway through
the error handling process. the error handling process.
- Added the ``JSONIFY_PRETTYPRINT_REGULAR`` configuration variable.
Version 0.9 Version 0.9
----------- -----------

View file

@ -149,6 +149,11 @@ The following configuration values are used internally by Flask:
unicode strings. ``jsonfiy`` will unicode strings. ``jsonfiy`` will
automatically encode it in ``utf-8`` automatically encode it in ``utf-8``
then for transport for instance. then for transport for instance.
``JSONIFY_PRETTYPRINT_REGULAR`` If this is set to ``True`` (the default)
jsonify responses will be pretty printed
if they are not requested by an
XMLHttpRequest object (controlled by
the ``X-Requested-With`` header)
================================= ========================================= ================================= =========================================
.. admonition:: More on ``SERVER_NAME`` .. admonition:: More on ``SERVER_NAME``
@ -192,7 +197,7 @@ The following configuration values are used internally by Flask:
``PREFERRED_URL_SCHEME`` ``PREFERRED_URL_SCHEME``
.. versionadded:: 0.10 .. versionadded:: 0.10
``JSON_AS_ASCII`` ``JSON_AS_ASCII``, ``JSONIFY_PRETTYPRINT_REGULAR``
Configuring from Files Configuring from Files
---------------------- ----------------------

View file

@ -290,7 +290,8 @@ class Flask(_PackageBoundObject):
'TRAP_BAD_REQUEST_ERRORS': False, 'TRAP_BAD_REQUEST_ERRORS': False,
'TRAP_HTTP_EXCEPTIONS': False, 'TRAP_HTTP_EXCEPTIONS': False,
'PREFERRED_URL_SCHEME': 'http', 'PREFERRED_URL_SCHEME': 'http',
'JSON_AS_ASCII': True 'JSON_AS_ASCII': True,
'JSONIFY_PRETTYPRINT_REGULAR': True,
}) })
#: The rule object to use for URL rules created. This is used by #: The rule object to use for URL rules created. This is used by

View file

@ -194,8 +194,16 @@ def jsonify(*args, **kwargs):
For security reasons only objects are supported toplevel. For more For security reasons only objects are supported toplevel. For more
information about this, have a look at :ref:`json-security`. 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 .. versionadded:: 0.2
""" """
indent = None
if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] \
and not request.is_xhr:
indent = 2
return current_app.response_class(dumps(dict(*args, **kwargs), return current_app.response_class(dumps(dict(*args, **kwargs),
indent=None if request.is_xhr else 2), indent=indent),
mimetype='application/json') mimetype='application/json')