Skip to content
Myr

Improve compression by removing whitespace from separators when using jsonify() and JSONIFY_PRETTYPRINT_REGULAR is False.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Commit includes Changelog entry and two new tests in test_basic.py.

authored by Gilman Callsen · committed by Markus Unterwaditzer · parent 050a659 · Browse files · 3 files+39 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

3 files+39 added−1 removed

  • tests:+28 added−0 removed
  • other:+11 added−1 removed

3 files+39 added−1 removed

modified:flask/json.py+8 added-1 removed
225 unmodified lines
226227 This function's response will be pretty printed if it was not requested228 with ``X-Requested-With: XMLHttpRequest`` to simplify debugging unless229 the ``JSONIFY_PRETTYPRINT_REGULAR`` config parameter is set to false.230 Compressed (not pretty) formatting currently means no indents and no231 spaces after separators.232233 .. versionadded:: 0.2234 """235236 indent = None237 separators = (',', ':')238239 if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] \240 and not request.is_xhr:241 indent = 2242 separators = (', ', ': ')243244 return current_app.response_class(dumps(dict(*args, **kwargs),238 indent=indent),245 indent=indent, separators=separators),246 mimetype='application/json')247248249def tojson_filter(obj, **kwargs):
1 unmodified line
modified:tests/test_basic.py+28 added
957 unmodified lines
958 assert rv.headers['Content-Type'] == 'text/html'959 assert rv.headers['X-Foo'] == 'bar'960961962def test_jsonify_no_prettyprint():963 app = flask.Flask(__name__)964 app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False})965 with app.test_request_context():966 compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}'967 uncompressed_msg = {968 "msg": {969 "submsg": "W00t"970 },971 "msg2": "foobar"972 }973974 rv = flask.make_response(975 flask.jsonify(uncompressed_msg), 200)976 assert rv.data == compressed_msg977978def test_jsonify_prettyprint():979 app = flask.Flask(__name__)980 app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True})981 with app.test_request_context():982 compressed_msg = {"msg":{"submsg":"W00t"},"msg2":"foobar"}983 pretty_response =\984 b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}'985986 rv = flask.make_response(987 flask.jsonify(compressed_msg), 200)988 assert rv.data == pretty_response989990def test_url_generation():991 app = flask.Flask(__name__)992993 @app.route('/hello/<name>', methods=['POST'])
571 unmodified lines
modified:CHANGES+3 added
49 unmodified lines
50- Enforce blueprint handling in the order they were registered for template51 loading.52- Ported testsuite to py.test.53- Deprecated ``request.json`` in favour of ``request.get_json()``.54- Add "pretty" and "compressed" separators definitions in jsonify() method.55 Reduces JSON response size when JSONIFY_PRETTYPRINT_REGULAR=False by removing56 unnecessary white space included by default after separators.575859Version 0.10.260--------------
479 unmodified lines