update docs about json
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by David Lord · committed by David Lord · parent ca2bfbb · Browse files · 3 files·+31 added−23 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+31 added−23 removed
- other:+31 added−23 removed
Provenance
- Commit
f8cb0b0- Tree
7f6c473- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
ca2bfbb
3 files changed+31 added−23 removed
modified:docs/quickstart.rst+22 added-19 removed
679 unmodified lines680 The return value from a view function is automatically converted into681 a response object for you. If the return value is a string it's682 converted into a response object with the string as response body, a683 ``200 OK`` status code and a :mimetype:`text/html` mimetype. If the684-return value is a dict, :func:`jsonify` is called to produce a response.685-The logic that Flask applies to converting return values into response686-objects is as follows:684+return value is a dict or list, :func:`jsonify` is called to produce a685+response. The logic that Flask applies to converting return values into686+response objects is as follows:687 688 1. If a response object of the correct type is returned it's directly689 returned from the view.690 2. If it's a string, a response object is created with that data and691 the default parameters.692-3. If it's a dict, a response object is created using ``jsonify``.693-4. If a tuple is returned the items in the tuple can provide extra692+3. If it's an iterator or generator returning strings or bytes, it is693+ treated as a streaming response.694+4. If it's a dict or list, a response object is created using695+ :func:`~flask.json.jsonify`.696+5. If a tuple is returned the items in the tuple can provide extra697 information. Such tuples have to be in the form698 ``(response, status)``, ``(response, headers)``, or699 ``(response, status, headers)``. The ``status`` value will override700 the status code and ``headers`` can be a list or dictionary of701 additional header values.699-5. If none of that works, Flask will assume the return value is a702+6. If none of that works, Flask will assume the return value is a703 valid WSGI application and convert that into a response object.704 705 If you want to get hold of the resulting response object inside the view706 you can use the :func:`~flask.make_response` function.22 unmodified lines729 APIs with JSON730 ``````````````731 732 A common response format when writing an API is JSON. It's easy to get730-started writing such an API with Flask. If you return a ``dict`` from a731-view, it will be converted to a JSON response.733+started writing such an API with Flask. If you return a ``dict`` or734+``list`` from a view, it will be converted to a JSON response.735 736 .. code-block:: python737 738 @app.route("/me")4 unmodified lines743 "theme": user.theme,744 "image": url_for("user_image", filename=user.image),745 }746 744-Depending on your API design, you may want to create JSON responses for745-types other than ``dict``. In that case, use the746-:func:`~flask.json.jsonify` function, which will serialize any supported747-JSON data type. Or look into Flask community extensions that support748-more complex applications.749-750-.. code-block:: python751-752- from flask import jsonify753-747 @app.route("/users")748 def users_api():749 users = get_all_users()757- return jsonify([user.to_json() for user in users])750+ return [user.to_json() for user in users]751 752+This is a shortcut to passing the data to the753+:func:`~flask.json.jsonify` function, which will serialize any supported754+JSON data type. That means that all the data in the dict or list must be755+JSON serializable.756 757+For complex types such as database models, you'll want to use a758+serialization library to convert the data to valid JSON types first.759+There are many serialization libraries and Flask API extensions760+maintained by the community that support more complex applications.761+762+763 .. _sessions:764 765 Sessions766 --------136 unmodified linesmodified:src/flask/app.py+6 added-4 removed
1922 unmodified lines1923 except TypeError as e:1924 raise TypeError(1925 f"{e}\nThe view function did not return a valid"1926 " response. The return type must be a string,"1927- " dict, list, tuple, Response instance, or WSGI"1928- f" callable, but it was a {type(rv).__name__}."1927+ " dict, list, tuple with headers or status,"1928+ " Response instance, or WSGI callable, but it"1929+ f" was a {type(rv).__name__}."1930 ).with_traceback(sys.exc_info()[2]) from None1931 else:1932 raise TypeError(1933 "The view function did not return a valid"1934 " response. The return type must be a string,"1934- " dict, list, tuple, Response instance, or WSGI"1935- f" callable, but it was a {type(rv).__name__}."1935+ " dict, list, tuple with headers or status,"1936+ " Response instance, or WSGI callable, but it was a"1937+ f" {type(rv).__name__}."1938 )1939 1940 rv = t.cast(Response, rv)1941 # prefer the status if it was provided369 unmodified linesmodified:CHANGES.rst+3 added
63 unmodified lines64 - ``stream_with_context`` preserves context separately from a65 ``with client`` block. It will be cleaned up when66 ``response.get_data()`` or ``response.close()`` is called.67 68+- Allow returning a list from a view function, to convert it to a69+ JSON response like a dict is. :issue:`4672`70 71+72 Version 2.1.373 -------------74 75 Unreleased1289 unmodified lines