forked from orbit-oss/flask
Make jsonify terminate responses with a newline
This came up in the context of https://github.com/kennethreitz/httpbin/issues/168
This commit is contained in:
parent
b24438b2c9
commit
d9402fc0c0
3 changed files with 17 additions and 8 deletions
6
CHANGES
6
CHANGES
|
|
@ -53,7 +53,11 @@ Version 1.0
|
||||||
- Add "pretty" and "compressed" separators definitions in jsonify() method.
|
- Add "pretty" and "compressed" separators definitions in jsonify() method.
|
||||||
Reduces JSON response size when JSONIFY_PRETTYPRINT_REGULAR=False by removing
|
Reduces JSON response size when JSONIFY_PRETTYPRINT_REGULAR=False by removing
|
||||||
unnecessary white space included by default after separators.
|
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
|
Version 0.10.2
|
||||||
--------------
|
--------------
|
||||||
|
|
|
||||||
|
|
@ -200,8 +200,9 @@ def htmlsafe_dump(obj, fp, **kwargs):
|
||||||
|
|
||||||
def jsonify(*args, **kwargs):
|
def jsonify(*args, **kwargs):
|
||||||
"""Creates a :class:`~flask.Response` with the JSON representation of
|
"""Creates a :class:`~flask.Response` with the JSON representation of
|
||||||
the given arguments with an :mimetype:`application/json` mimetype. The arguments
|
the given arguments with an :mimetype:`application/json` mimetype. The
|
||||||
to this function are the same as to the :class:`dict` constructor.
|
arguments to this function are the same as to the :class:`dict`
|
||||||
|
constructor.
|
||||||
|
|
||||||
Example usage::
|
Example usage::
|
||||||
|
|
||||||
|
|
@ -241,9 +242,13 @@ def jsonify(*args, **kwargs):
|
||||||
indent = 2
|
indent = 2
|
||||||
separators = (', ', ': ')
|
separators = (', ', ': ')
|
||||||
|
|
||||||
return current_app.response_class(dumps(dict(*args, **kwargs),
|
# Note that we add '\n' to end of response
|
||||||
indent=indent, separators=separators),
|
# (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')
|
mimetype='application/json')
|
||||||
|
return rv
|
||||||
|
|
||||||
|
|
||||||
def tojson_filter(obj, **kwargs):
|
def tojson_filter(obj, **kwargs):
|
||||||
|
|
|
||||||
|
|
@ -942,7 +942,7 @@ def test_make_response_with_response_instance():
|
||||||
rv = flask.make_response(
|
rv = flask.make_response(
|
||||||
flask.jsonify({'msg': 'W00t'}), 400)
|
flask.jsonify({'msg': 'W00t'}), 400)
|
||||||
assert rv.status_code == 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'
|
assert rv.mimetype == 'application/json'
|
||||||
|
|
||||||
rv = flask.make_response(
|
rv = flask.make_response(
|
||||||
|
|
@ -963,7 +963,7 @@ def test_jsonify_no_prettyprint():
|
||||||
app = flask.Flask(__name__)
|
app = flask.Flask(__name__)
|
||||||
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False})
|
app.config.update({"JSONIFY_PRETTYPRINT_REGULAR": False})
|
||||||
with app.test_request_context():
|
with app.test_request_context():
|
||||||
compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}'
|
compressed_msg = b'{"msg":{"submsg":"W00t"},"msg2":"foobar"}\n'
|
||||||
uncompressed_msg = {
|
uncompressed_msg = {
|
||||||
"msg": {
|
"msg": {
|
||||||
"submsg": "W00t"
|
"submsg": "W00t"
|
||||||
|
|
@ -982,7 +982,7 @@ def test_jsonify_prettyprint():
|
||||||
with app.test_request_context():
|
with app.test_request_context():
|
||||||
compressed_msg = {"msg":{"submsg":"W00t"},"msg2":"foobar"}
|
compressed_msg = {"msg":{"submsg":"W00t"},"msg2":"foobar"}
|
||||||
pretty_response =\
|
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(
|
rv = flask.make_response(
|
||||||
flask.jsonify(compressed_msg), 200)
|
flask.jsonify(compressed_msg), 200)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue