From 3d9055b3b73b1d09f933bfd070f704e9e8bdff2a Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 1 Jun 2013 00:20:00 +0100 Subject: [PATCH] Added the JSONIFY_PRETTYPRINT_REGULAR config variable. This fixes #725 --- CHANGES | 1 + docs/config.rst | 7 ++++++- flask/app.py | 3 ++- flask/json.py | 10 +++++++++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/CHANGES b/CHANGES index 17290290..7be9c77a 100644 --- a/CHANGES +++ b/CHANGES @@ -62,6 +62,7 @@ Release date to be decided. - Changed how the teardown system is informed about exceptions. This is now more reliable in case something handles an exception halfway through the error handling process. +- Added the ``JSONIFY_PRETTYPRINT_REGULAR`` configuration variable. Version 0.9 ----------- diff --git a/docs/config.rst b/docs/config.rst index 134fe36d..0dbb71d7 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -149,6 +149,11 @@ The following configuration values are used internally by Flask: unicode strings. ``jsonfiy`` will automatically encode it in ``utf-8`` 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`` @@ -192,7 +197,7 @@ The following configuration values are used internally by Flask: ``PREFERRED_URL_SCHEME`` .. versionadded:: 0.10 - ``JSON_AS_ASCII`` + ``JSON_AS_ASCII``, ``JSONIFY_PRETTYPRINT_REGULAR`` Configuring from Files ---------------------- diff --git a/flask/app.py b/flask/app.py index 5bd3d1de..86f2a212 100644 --- a/flask/app.py +++ b/flask/app.py @@ -290,7 +290,8 @@ class Flask(_PackageBoundObject): 'TRAP_BAD_REQUEST_ERRORS': False, 'TRAP_HTTP_EXCEPTIONS': False, '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 diff --git a/flask/json.py b/flask/json.py index 9e56073c..e43c4ed9 100644 --- a/flask/json.py +++ b/flask/json.py @@ -194,8 +194,16 @@ def jsonify(*args, **kwargs): 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 """ + 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), - indent=None if request.is_xhr else 2), + indent=indent), mimetype='application/json')