Skip to content

Commit

Permalink
Add JSONIFY_END_WITH_NEWLINE config variable
Browse files Browse the repository at this point in the history
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
  • Loading branch information
msabramo committed Nov 29, 2014
1 parent b24438b commit 843cd67
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ Version 1.0
- Add "pretty" and "compressed" separators definitions in jsonify() method.
Reduces JSON response size when JSONIFY_PRETTYPRINT_REGULAR=False by removing
unnecessary white space included by default after separators.
- Added the ``JSONIFY_END_WITH_NEWLINE`` configuration variable.


Version 0.10.2
Expand Down
3 changes: 3 additions & 0 deletions docs/config.rst
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,9 @@ The following configuration values are used internally by Flask:
if they are not requested by an
XMLHttpRequest object (controlled by
the ``X-Requested-With`` header)
``JSONIFY_END_WITH_NEWLINE`` If this is set to ``True``, ``jsonify``
responses will be terminated with a newline
character.
``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of
the template source and reload it
automatically. By default the value is
Expand Down
1 change: 1 addition & 0 deletions flask/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ def _set_request_globals_class(self, value):
'JSON_AS_ASCII': True,
'JSON_SORT_KEYS': True,
'JSONIFY_PRETTYPRINT_REGULAR': True,
'JSONIFY_END_WITH_NEWLINE': False,
'TEMPLATES_AUTO_RELOAD': None,
})

Expand Down
16 changes: 12 additions & 4 deletions flask/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,8 +200,9 @@ def htmlsafe_dump(obj, fp, **kwargs):

def jsonify(*args, **kwargs):
"""Creates a :class:`~flask.Response` with the JSON representation of
the given arguments with an :mimetype:`application/json` mimetype. The arguments
to this function are the same as to the :class:`dict` constructor.
the given arguments with an :mimetype:`application/json` mimetype. The
arguments to this function are the same as to the :class:`dict`
constructor.
Example usage::
Expand Down Expand Up @@ -231,6 +232,9 @@ def get_current_user():
spaces after separators.
.. versionadded:: 0.2
To ensure that the output is terminated with a newline, set the
``JSONIFY_END_WITH_NEWLINE`` config parameter.
"""

indent = None
Expand All @@ -241,9 +245,13 @@ def get_current_user():
indent = 2
separators = (', ', ': ')

return current_app.response_class(dumps(dict(*args, **kwargs),
indent=indent, separators=separators),
rv = current_app.response_class(
dumps(dict(*args, **kwargs), indent=indent, separators=separators),
mimetype='application/json')
if current_app.config['JSONIFY_END_WITH_NEWLINE']:
if not rv.data.endswith(b'\n'):
rv.data += b'\n'
return rv


def tojson_filter(obj, **kwargs):
Expand Down
13 changes: 13 additions & 0 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -989,6 +989,19 @@ def test_jsonify_prettyprint():
assert rv.data == pretty_response


def test_jsonify_end_with_newline():
app = flask.Flask(__name__)
app.config.update({"JSONIFY_END_WITH_NEWLINE": True})
with app.test_request_context():
compressed_msg = {"msg":{"submsg":"W00t"},"msg2":"foobar"}
pretty_response =\
b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}\n'

rv = flask.make_response(
flask.jsonify(compressed_msg), 200)
assert rv.data == pretty_response


def test_url_generation():
app = flask.Flask(__name__)

Expand Down

0 comments on commit 843cd67

Please sign in to comment.