Skip to content
Myr

Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINE

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

Add JSONIFY_END_WITH_NEWLINE config variable

authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parents f166894, d9402fc · Browse files · 3 files+17 added−8 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+17 added−8 removed

  • tests:+3 added−3 removed
  • other:+14 added−5 removed

3 files+17 added−8 removed

modified:flask/json.py+9 added-4 removed
198 unmodified lines
199200201def jsonify(*args, **kwargs):202 """Creates a :class:`~flask.Response` with the JSON representation of203 the given arguments with an :mimetype:`application/json` mimetype. The arguments204 to this function are the same as to the :class:`dict` constructor.203 the given arguments with an :mimetype:`application/json` mimetype. The204 arguments to this function are the same as to the :class:`dict`205 constructor.206207 Example usage::208209 from flask import jsonify
31 unmodified lines
241 and not request.is_xhr:242 indent = 2243 separators = (', ', ': ')244244 return current_app.response_class(dumps(dict(*args, **kwargs),245 indent=indent, separators=separators),245 # Note that we add '\n' to end of response246 # (see https://github.com/mitsuhiko/flask/pull/1262)247 rv = current_app.response_class(248 (dumps(dict(*args, **kwargs), indent=indent, separators=separators),249 '\n'),250 mimetype='application/json')251 return rv252253254def tojson_filter(obj, **kwargs):255 return Markup(htmlsafe_dumps(obj, **kwargs))
modified:tests/test_basic.py+3 added-3 removed
940 unmodified lines
941 with app.test_request_context():942 rv = flask.make_response(943 flask.jsonify({'msg': 'W00t'}), 400)944 assert rv.status_code == 400945 assert rv.data == b'{\n "msg": "W00t"\n}'945 assert rv.data == b'{\n "msg": "W00t"\n}\n'946 assert rv.mimetype == 'application/json'947948 rv = flask.make_response(949 flask.Response(''), 400)
12 unmodified lines
962def 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"}'966 compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n'967 uncompressed_msg = {968 "msg": {969 "submsg": "W00t"970 },
10 unmodified lines
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}'985 b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}\n'986987 rv = flask.make_response(988 flask.jsonify(compressed_msg), 200)989 assert rv.data == pretty_response
577 unmodified lines
modified:CHANGES+5 added-1 removed
51 unmodified lines
52- Deprecated ``request.json`` in favour of ``request.get_json()``.53- Add "pretty" and "compressed" separators definitions in jsonify() method.54 Reduces JSON response size when JSONIFY_PRETTYPRINT_REGULAR=False by removing55 unnecessary white space included by default after separators.56- JSON responses are now terminated with a newline character, because it is a57 convention that UNIX text files end with a newline and some clients don't58 deal well when this newline is missing. See59 https://github.com/mitsuhiko/flask/pull/1262 -- this came up originally as a60 part of https://github.com/kennethreitz/httpbin/issues/168615762Version 0.10.263--------------6465(bugfix release, release date to be announced)
477 unmodified lines