Skip to content

Conversation

@greyli
Copy link
Member

@greyli greyli commented Jul 3, 2022

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:

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.rst summarizing the change and linking to the issue.
  • Add .. versionchanged:: entries in any relevant code docs.
  • Run pre-commit hooks and fix any issues.
  • Run pytest and tox, no tests failed.

@greyli
Copy link
Member Author

greyli commented Jul 3, 2022

The failing "Pallets Development Versions" check is due to a missing import in Werkzueg (werkzeug.routing.RoutingException), I will make a PR to fix it later.

@davidism
Copy link
Member

davidism commented Jul 3, 2022

Looks like that fix should be made in Werkzeug @pgjones

@davidism
Copy link
Member

davidism commented Jul 3, 2022

I think my original hesitation was that returning a dict is explicitly different from returning a database model. Returning a list of database results is probably something a lot more users will try. But I guess it's not that different to say they have to do obj.to_json() and also support [obj.to_json() for obj in objs].

@greyli
Copy link
Member Author

greyli commented Jul 3, 2022

I see. If they don't return a list of objects directly and call jsonify for the list, they will get the same error. Maybe we could update the docs to mention that the ORM/ODM object is not jsonify-able, they need to convert them to a dict or use a serialization library.

@davidism davidism added this to the 2.2.0 milestone Jul 3, 2022
@davidism
Copy link
Member

davidism commented Jul 3, 2022

Another potential confusion now that we support returning iterators/generators is that you're going to get very different results doing return ["a", "b", "c"] or return iter(["a", "b", "c"]).

@davidism davidism merged commit 559a845 into pallets:main Jul 3, 2022
@greyli greyli deleted the return-list branch July 3, 2022 04:49
@davidism davidism added the json label Jul 3, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 18, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants