This adds support to serializing list return values to JSON, like what we did for the dictionary:
@app.route('/') def index(): return ['this', 'will', 'become', 'JSON']
There are some discusses related to this feature in #3111, and the concern is the list return value looks too much like the tuple. Maybe we can rethink about this feature? I propose this because the following reasons:
- List as JSON response is a common use case for Web APIs. We support returning dict as JSON, but not list. This makes people feel inconsistent (Could list responses also be converted to JSON automatically? #4533, Flask return a list directly #4659).
- With fix #2736 - remove explicit check for list as a return value in make_response #2737, we only treat tuple as a valid shortcut for return values, so there will be no program conflicts to allow returning list (we also throw an error for list return value currently).
- In terms of semantics, there is no concept of "tuple" in JSON. Although list and tuple are both sequences in Python, there are differences in syntax and meaning. We can add docs to address the different meaning of list and tuple in return values.
- In terms of visuals, most people will not mix
[1, 2, 3]and(1, 2, 3), and people usually return tuple items without the bracket in the view function.
If this change is acceptable, I will update the docs and changelog.
Sorry for break the "zero status". :P
Checklist:
- Add tests that demonstrate the correct behavior of the change. Tests should fail without the change.
- Add or update relevant docs, in the docs folder and in code.
- Add an entry in
CHANGES.rstsummarizing the change and linking to the issue. - Add
.. versionchanged::entries in any relevant code docs. - Run
pre-commithooks and fix any issues. - Run
pytestandtox, no tests failed.