Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork16.7k
Comments
Conversation
| :param kwargs: Treat as a dict to serialize. | ||
| """ | ||
| obj = self._prepare_response_obj(args, kwargs) | ||
| return self._app.response_class(self.dumps(obj), mimetype="application/json") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Worth a cls default_mimetype here to save extensions having to override this method and as matching with responses?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Discussed this more here:#1728 (comment)
I'm not sure this should actually be configurable at all. The original issue seemed to be about a specific type of response, not all JSON responses.Maybe if you want your whole API to have a different vendor type, like GitHub does, but even GitHub applies different mimetypes to different parts. APIs complex enough to use vendor types usually have versioning as well, so they still wouldn't apply globally.
I did originally have this as aJSONProvider.mimetype attribute, but I ended up moving all existing behavior toDefaultProvider and keeping the base very simple.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
This comment was marked as off-topic.
This comment was marked as off-topic.
Sorry, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
| not know how to serialize. It should return a valid JSON type or | ||
| raise a ``TypeError``. | ||
| """ | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
What do you think about adding a dict_to_object hook here as well (for the loads side). Allows something like this,
classMoneyJSONProvider(DefaultJSONProvider):@staticmethoddefdefault(object_):ifisinstance(object_,date):returnhttp_date(object_)ifisinstance(object_, (Decimal,UUID)):returnstr(object_)ifis_dataclass(object_):returnasdict(object_)ifhasattr(object_,"__html__"):returnstr(object_.__html__())ifisinstance(object_,Money):return {'amount':object_.amount,'currency':object_.currency}raiseTypeError(f"Object of type{type(object_).__name__} is not JSON serializable")@staticmethoddefdict_to_object(dict_):if'amount'indict_and'currency'indict_:returnMoney(Decimal(dict_['amount']),dict_['currency'])else:returndict_
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I left it out for a few reasons.object_hook is not as consistently supported by different libraries asdefault is, and I didn't want to put a perceived requirement for it on all other providers. You usually want to perform validation when deserializing, and that gets very messy trying to cram it all inobject_hook along with proper error collection. Instead, any project should use a serialization library, leaving the provider to only handle the JSON and basic types.
Uh oh!
There was an error while loading.Please reload this page.
This adds the ability to fully customize the JSON implementation used by a Flask application. Using a different JSON implementation can greatly speed up API applications that need to work with JSON in most requests.
app.jsonis an instance ofFlask.json_provider_class.flask.json.provider.JSONProvideris the base class that definesdumps,dump,loads,load, andresponsemethods, of which onlydumpsandloadsneed to be implemented. For example, here's a provider fororjson:The methods in
flask.jsoncall the methods onapp.jsonif an app context is active, or fall back to thejsonlibrary.jsonifycallsapp.json.response. The|tojsonfilter usesapp.json.dumps.Request.jsonusesapp.json.loadsandResponse.jsonusesapp.json.dumps; the test client uses these as well.Customizing
json_encoderorjson_decoderon an app or blueprint, and theJSONEncoderandJSONDecoderclasses, are deprecated. This was not an effective way to use other libraries. Customizing per blueprint was requested by an API extension that is no longer maintained and didn't appear to use the feature. It's not clear how it would work with the new provider interface and added overhead to every request. Instead, API frameworks should be using a dedicated object serialization library, then taking advantage of a fast JSON serializer at the application level.The
DefaultJSONProvideris the existing implementation using the built-injsonlibrary. Theapp.configkeysJSON_AS_ASCII,JSON_SORT_KEYS,JSONIFY_MIMETYPE, andJSONIFY_PRETTYPRINT_REGULARare deprecated and have moved to attributes on the default provider. Other providers are not required to support these options.