-
-
Notifications
You must be signed in to change notification settings - Fork 16.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for serializing top-level arrays to JSON #1671
Add support for serializing top-level arrays to JSON #1671
Conversation
Shouldn't jsonify with both args and kwargs fail? |
I don't know. I was just going off of the current implementation ( Happy to change that if it should. |
@@ -242,11 +242,21 @@ def get_current_user(): | |||
indent = 2 | |||
separators = (', ', ': ') | |||
|
|||
try: | |||
data = dict(*args, **kwargs) | |||
except: |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
I wonder if the starargs-like syntax is actually useful.. If you have a fixed/low number of list entries, chances are good that you should be using a dict instead. |
Why is that? I've got API endpoints like |
No, I meant the cases where you would do |
Agreed.
However, Overall, I think the divergence in accepted input between Splitting behavior so
I don't care strongly, but if I had to pick I'd choose the latter because it's simpler to think of |
👍 on being consistent with json.dumps, especially since it's a widely copied API. |
BTW, I think the reason why |
Maybe the solution to this mess is to introduce a new function that just invokes |
Is there any reason to create a new function vs just modifying the existing It breaks backwards compatibility, but for a 1.0 release that should be fine. Also, I know in my own projects, I've never intentionally used To me, the path of least surprise is to stay with the current The other nicety that |
Breaking kwargs in While passing |
I hear you. :-) My vote is to stick with my original implementation where both
Still a very valid question, but I don't want it to delay merging this PR. For now, let's just stick with Flask's current behavior which is to accept both |
Any other feedback @mitsuhiko @davidism ? Would like to see this merged so I don't have to keep working around it. Supporting top-level arrays was already discussed ad nauseum in the issues I linked to in the OP, so IMHO this PR should be relatively non-controversial since it's just implementing behavior that already has been decided on. |
What about a test case for all the important cases on how to call |
Good idea. Let me see what I can do. |
@@ -222,8 +222,8 @@ def get_current_user(): | |||
"id": 42 | |||
} | |||
|
|||
For security reasons only objects are supported toplevel. For more | |||
information about this, have a look at :ref:`json-security`. | |||
Support for serializing top-level arrays was added in Flask 1.0. For |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This needs a test case and docs to demonstrate exactly what is accepted by the function now. The docs for the Note that as a side effect, simple types are accepted as well ( Not sure if I like that |
Some updates:
Feedback welcome. |
@davidism any chance you can review this weekend? |
I'll get to this on Sunday. |
Add support for serializing top-level arrays to JSON
OK, Monday morning. 😉 Thanks for your work on this. I might clean up the wording a bit at some point, but other than that this looks good. |
raise TypeError( | ||
"jsonify() behavior undefined when passed both args and kwargs" | ||
) | ||
elif len(args) == 1: # single args are passed directly to dumps() |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Thanks for merging @davidism, really nice to see this finally land. Also, improvements to my wording are always appreciated. |
:func:`dumps`. | ||
3. Multiple keyword arguments: Converted to a dict before being passed to | ||
:func:`dumps`. | ||
4. Both args and kwargs: Behavior undefined and will throw an exception. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
@@ -8,6 +8,9 @@ Version 1.0 | |||
|
|||
(release date to be announced, codename to be selected) | |||
|
|||
- Added support to serializing top-level arrays to :func:`flask.jsonify`. This | |||
introduces a security risk in ancient browsers. See | |||
:ref:`json_security` for details. |
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Major discussion of the issue is in #248.
I'm tired of working around it, so just like to get it fixed.
This solution is similar to #1402 & #1209 with a few changes:
jsonify(*l)
. I changed it so that bothjsonify(1,2,3)
andjsonify([1,2,3])
serialize to[1,2,3]
as I think that's the more intuitive behavior.I know it'd be nice to incorporate a fix for #1443 in here as well, but I don't fully understand the issue there. So let's get this merged, and then tackle that issue in a followup PR.
Fix #170, #248, #510, #673, #1177 and check one of the boxes on #1182