forked from orbit-oss/flask
Merge pull request #1703 from jeffwidman/master
Cleanup jsonify() function
This commit is contained in:
commit
7df543d5dc
2 changed files with 7 additions and 9 deletions
2
CHANGES
2
CHANGES
|
|
@ -10,7 +10,7 @@ Version 1.0
|
||||||
|
|
||||||
- Added support to serializing top-level arrays to :func:`flask.jsonify`. This
|
- Added support to serializing top-level arrays to :func:`flask.jsonify`. This
|
||||||
introduces a security risk in ancient browsers. See
|
introduces a security risk in ancient browsers. See
|
||||||
:ref:`json_security` for details.
|
:ref:`json-security` for details.
|
||||||
- Added before_render_template signal.
|
- Added before_render_template signal.
|
||||||
- Added `**kwargs` to :meth:`flask.Test.test_client` to support passing
|
- Added `**kwargs` to :meth:`flask.Test.test_client` to support passing
|
||||||
additional keyword arguments to the constructor of
|
additional keyword arguments to the constructor of
|
||||||
|
|
|
||||||
|
|
@ -211,9 +211,9 @@ def jsonify(*args, **kwargs):
|
||||||
|
|
||||||
1. Single argument: Passed straight through to :func:`dumps`.
|
1. Single argument: Passed straight through to :func:`dumps`.
|
||||||
2. Multiple arguments: Converted to an array before being passed to
|
2. Multiple arguments: Converted to an array before being passed to
|
||||||
:func:`dumps`.
|
:func:`dumps`.
|
||||||
3. Multiple keyword arguments: Converted to a dict before being passed to
|
3. Multiple keyword arguments: Converted to a dict before being passed to
|
||||||
:func:`dumps`.
|
:func:`dumps`.
|
||||||
4. Both args and kwargs: Behavior undefined and will throw an exception.
|
4. Both args and kwargs: Behavior undefined and will throw an exception.
|
||||||
|
|
||||||
Example usage::
|
Example usage::
|
||||||
|
|
@ -237,7 +237,7 @@ def jsonify(*args, **kwargs):
|
||||||
|
|
||||||
.. versionchanged:: 1.0
|
.. versionchanged:: 1.0
|
||||||
Added support for serializing top-level arrays. This introduces a
|
Added support for serializing top-level arrays. This introduces a
|
||||||
security risk in ancient browsers. See :ref:`json_security` for details.
|
security risk in ancient browsers. See :ref:`json-security` for details.
|
||||||
|
|
||||||
This function's response will be pretty printed if it was not requested
|
This function's response will be pretty printed if it was not requested
|
||||||
with ``X-Requested-With: XMLHttpRequest`` to simplify debugging unless
|
with ``X-Requested-With: XMLHttpRequest`` to simplify debugging unless
|
||||||
|
|
@ -260,12 +260,10 @@ def jsonify(*args, **kwargs):
|
||||||
raise TypeError(
|
raise TypeError(
|
||||||
"jsonify() behavior undefined when passed both args and kwargs"
|
"jsonify() behavior undefined when passed both args and kwargs"
|
||||||
)
|
)
|
||||||
elif len(args) == 1: # single args are passed directly to dumps()
|
elif len(args) == 1: # single args are passed directly to dumps()
|
||||||
data = args[0]
|
data = args[0]
|
||||||
elif args: # convert multiple args into an array
|
else:
|
||||||
data = list(args)
|
data = args or kwargs
|
||||||
else: # convert kwargs to a dict
|
|
||||||
data = dict(kwargs)
|
|
||||||
|
|
||||||
# Note that we add '\n' to end of response
|
# Note that we add '\n' to end of response
|
||||||
# (see https://github.com/mitsuhiko/flask/pull/1262)
|
# (see https://github.com/mitsuhiko/flask/pull/1262)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue