Skip to content
Myr

Merge branch 'compressed-jsonify'

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

Fix #1203

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents 050a659, bd232e5 · Browse files · 3 files+41 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+41 added−1 removed

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

3 files+41 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+30 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_msg977978979def test_jsonify_prettyprint():980 app = flask.Flask(__name__)981 app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": True})982 with app.test_request_context():983 compressed_msg = {"msg":{"submsg":"W00t"},"msg2":"foobar"}984 pretty_response =\985 b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}'986987 rv = flask.make_response(988 flask.jsonify(compressed_msg), 200)989 assert rv.data == pretty_response990991992def test_url_generation():993 app = flask.Flask(__name__)994995 @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