Skip to content

Commit

Permalink
Merge pull request #1262 from msabramo/JSONIFY_END_WITH_NEWLINE
Browse files Browse the repository at this point in the history
Add JSONIFY_END_WITH_NEWLINE config variable
  • Loading branch information
untitaker committed Dec 7, 2014
2 parents f166894 + d9402fc commit eca5306
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ 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.

- JSON responses are now terminated with a newline character, because it is a
convention that UNIX text files end with a newline and some clients don't
deal well when this newline is missing. See
https://github.com/mitsuhiko/flask/pull/1262 -- this came up originally as a
part of https://github.com/kennethreitz/httpbin/issues/168

Version 0.10.2
--------------
Expand Down
13 changes: 9 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 @@ -241,9 +242,13 @@ def get_current_user():
indent = 2
separators = (', ', ': ')

return current_app.response_class(dumps(dict(*args, **kwargs),
indent=indent, separators=separators),
# Note that we add '\n' to end of response
# (see https://github.com/mitsuhiko/flask/pull/1262)
rv = current_app.response_class(
(dumps(dict(*args, **kwargs), indent=indent, separators=separators),
'\n'),
mimetype='application/json')
return rv


def tojson_filter(obj, **kwargs):
Expand Down
6 changes: 3 additions & 3 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ def test_make_response_with_response_instance():
rv = flask.make_response(
flask.jsonify({'msg': 'W00t'}), 400)
assert rv.status_code == 400
assert rv.data == b'{\n "msg": "W00t"\n}'
assert rv.data == b'{\n "msg": "W00t"\n}\n'
assert rv.mimetype == 'application/json'

rv = flask.make_response(
Expand All @@ -963,7 +963,7 @@ def test_jsonify_no_prettyprint():
app = flask.Flask(__name__)
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False})
with app.test_request_context():
compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}'
compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n'
uncompressed_msg = {
"msg": {
"submsg": "W00t"
Expand All @@ -982,7 +982,7 @@ def test_jsonify_prettyprint():
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}'
b'{\n "msg": {\n "submsg": "W00t"\n }, \n "msg2": "foobar"\n}\n'

rv = flask.make_response(
flask.jsonify(compressed_msg), 200)
Expand Down

0 comments on commit eca5306

Please sign in to comment.