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
Provenance
- Commit
a8c36fb- Tree
0a6b52a- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent 1
050a659- Parent 2
bd232e5
3 files changed+41 added−1 removed
modified:flask/json.py+8 added-1 removed
225 unmodified lines226 227 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.232 233 .. versionadded:: 0.2234 """235+236 indent = None237+ separators = (',', ':')238+239 if current_app.config['JSONIFY_PRETTYPRINT_REGULAR'] \240 and not request.is_xhr:241 indent = 2242+ separators = (', ', ': ')243+244 return current_app.response_class(dumps(dict(*args, **kwargs),238- indent=indent),245+ indent=indent, separators=separators),246 mimetype='application/json')247 248 249 def tojson_filter(obj, **kwargs):1 unmodified linemodified:tests/test_basic.py+30 added
957 unmodified lines958 assert rv.headers['Content-Type'] == 'text/html'959 assert rv.headers['X-Foo'] == 'bar'960 961 962+def 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+ }973+974+ rv = flask.make_response(975+ flask.jsonify(uncompressed_msg), 200)976+ assert rv.data == compressed_msg977+978+979+def 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}'986+987+ rv = flask.make_response(988+ flask.jsonify(compressed_msg), 200)989+ assert rv.data == pretty_response990+991+992 def test_url_generation():993 app = flask.Flask(__name__)994 995 @app.route('/hello/<name>', methods=['POST'])571 unmodified linesmodified:CHANGES+3 added
49 unmodified lines50 - 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.57 58 59 Version 0.10.260 --------------479 unmodified lines