Skip to content

Commit 6afb233

Browse files
committed
Add JSONIFY_END_WITH_NEWLINE config variable
When ``JSONIFY_END_WITH_NEWLINE`` is set to ``True``, ``jsonify`` responses will be terminated with a newline character. Defaults to False for backward compatibility. This came up in the context of https://github.com/kennethreitz/httpbin/issues/168
1 parent b24438b commit 6afb233

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

CHANGES

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ Version 1.0
5353
- Add "pretty" and "compressed" separators definitions in jsonify() method.
5454
Reduces JSON response size when JSONIFY_PRETTYPRINT_REGULAR=False by removing
5555
unnecessary white space included by default after separators.
56+
- Added the ``JSONIFY_END_WITH_NEWLINE`` configuration variable.
5657

5758

5859
Version 0.10.2

docs/config.rst

+3
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ The following configuration values are used internally by Flask:
182182
if they are not requested by an
183183
XMLHttpRequest object (controlled by
184184
the ``X-Requested-With`` header)
185+
``JSONIFY_END_WITH_NEWLINE`` If this is set to ``True``, ``jsonify``
186+
responses will be terminated with a newline
187+
character.
185188
``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of
186189
the template source and reload it
187190
automatically. By default the value is

flask/app.py

+1
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,7 @@ def _set_request_globals_class(self, value):
297297
'JSON_AS_ASCII': True,
298298
'JSON_SORT_KEYS': True,
299299
'JSONIFY_PRETTYPRINT_REGULAR': True,
300+
'JSONIFY_END_WITH_NEWLINE': False,
300301
'TEMPLATES_AUTO_RELOAD': None,
301302
})
302303

flask/json.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,9 @@ def htmlsafe_dump(obj, fp, **kwargs):
200200

201201
def jsonify(*args, **kwargs):
202202
"""Creates a :class:`~flask.Response` with the JSON representation of
203-
the given arguments with an :mimetype:`application/json` mimetype. The arguments
204-
to this function are the same as to the :class:`dict` constructor.
203+
the given arguments with an :mimetype:`application/json` mimetype. The
204+
arguments to this function are the same as to the :class:`dict`
205+
constructor.
205206
206207
Example usage::
207208
@@ -231,6 +232,9 @@ def get_current_user():
231232
spaces after separators.
232233
233234
.. versionadded:: 0.2
235+
236+
To ensure that the output is terminated with a newline, set the
237+
``JSONIFY_END_WITH_NEWLINE`` config parameter.
234238
"""
235239

236240
indent = None
@@ -241,9 +245,13 @@ def get_current_user():
241245
indent = 2
242246
separators = (', ', ': ')
243247

244-
return current_app.response_class(dumps(dict(*args, **kwargs),
245-
indent=indent, separators=separators),
248+
rv = current_app.response_class(
249+
dumps(dict(*args, **kwargs), indent=indent, separators=separators),
246250
mimetype='application/json')
251+
if current_app.config['JSONIFY_END_WITH_NEWLINE']:
252+
if not rv.data.endswith(b'\n'):
253+
rv.data += b'\n'
254+
return rv
247255

248256

249257
def tojson_filter(obj, **kwargs):

tests/test_basic.py

+13
Original file line numberDiff line numberDiff line change
@@ -989,6 +989,19 @@ def test_jsonify_prettyprint():
989989
assert rv.data == pretty_response
990990

991991

992+
def test_jsonify_end_with_newline():
993+
app = flask.Flask(__name__)
994+
app.config.update({"JSONIFY_END_WITH_NEWLINE": True})
995+
with app.test_request_context():
996+
compressed_msg = {"msg":{"submsg":"W00t"},"msg2":"foobar"}
997+
pretty_response =\
998+
b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}\n'
999+
1000+
rv = flask.make_response(
1001+
flask.jsonify(compressed_msg), 200)
1002+
assert rv.data == pretty_response
1003+
1004+
9921005
def test_url_generation():
9931006
app = flask.Flask(__name__)
9941007

0 commit comments

Comments
 (0)