From d425279650b392faf679db3245b8ac7f8c84cf54 Mon Sep 17 00:00:00 2001 From: Gilman Callsen Date: Mon, 13 Oct 2014 16:30:45 -0400 Subject: [PATCH 1/2] Improve compression by removing whitespace from separators when using jsonify() and JSONIFY_PRETTYPRINT_REGULAR is False. Commit includes Changelog entry and two new tests in test_basic.py. --- CHANGES | 3 +++ flask/json.py | 9 ++++++++- tests/test_basic.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGES b/CHANGES index 5ea2baae..5df663ea 100644 --- a/CHANGES +++ b/CHANGES @@ -51,6 +51,9 @@ Version 1.0 loading. - Ported testsuite to py.test. - Deprecated ``request.json`` in favour of ``request.get_json()``. +- Add "pretty" and "compressed" separators definitions in jsonify() method. + Reduces JSON response size when JSONIFY_PRETTYPRINT_REGULAR=False by removing + unnecessary white space included by default after separators. Version 0.10.2 diff --git a/flask/json.py b/flask/json.py index a0ed7041..318fe28b 100644 --- a/flask/json.py +++ b/flask/json.py @@ -227,15 +227,22 @@ def jsonify(*args, **kwargs): 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 """ + indent = None + separators = (',', ':') + if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] \ and not request.is_xhr: indent = 2 + separators = (', ', ': ') + return current_app.response_class(dumps(dict(*args, **kwargs), - indent=indent), + indent=indent, separators=separators), mimetype='application/json') diff --git a/tests/test_basic.py b/tests/test_basic.py index 55fd155a..61c5a0ba 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -959,6 +959,34 @@ def test_make_response_with_response_instance(): assert rv.headers['X-Foo'] == 'bar' +def test_jsonify_no_prettyprint(): + app = flask.Flask(__name__) + app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False}) + with app.test_request_context(): + compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}' + uncompressed_msg = { + "msg": { + "submsg": "W00t" + }, + "msg2": "foobar" + } + + rv = flask.make_response( + flask.jsonify(uncompressed_msg), 200) + assert rv.data == compressed_msg + +def test_jsonify_prettyprint(): + app = flask.Flask(__name__) + app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True}) + with app.test_request_context(): + compressed_msg = {"msg":{"submsg":"W00t"},"msg2":"foobar"} + pretty_response =\ + b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}' + + rv = flask.make_response( + flask.jsonify(compressed_msg), 200) + assert rv.data == pretty_response + def test_url_generation(): app = flask.Flask(__name__) From bd232e5c82cca0c4445b3fed1a460c0bb0a66082 Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Tue, 21 Oct 2014 19:11:54 +0200 Subject: [PATCH 2/2] PEP8 --- tests/test_basic.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_basic.py b/tests/test_basic.py index 61c5a0ba..2f024a5d 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -975,6 +975,7 @@ def test_jsonify_no_prettyprint(): flask.jsonify(uncompressed_msg), 200) assert rv.data == compressed_msg + def test_jsonify_prettyprint(): app = flask.Flask(__name__) app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True}) @@ -987,6 +988,7 @@ def test_jsonify_prettyprint(): flask.jsonify(compressed_msg), 200) assert rv.data == pretty_response + def test_url_generation(): app = flask.Flask(__name__)