Why should I return ORJSONResponse #11265
-
First Check
Commit to Help
DescriptionIn the documentation, the example is: I do not understand why I need to do It also works with class objects and is more readable and still (If I understand correctly) orjson is the one doing the serialization. Operating SystemmacOS Operating System DetailsNo response FastAPI Version0.109.2 Pydantic Version2.6.1 Python VersionPython 3.10.13 Additional ContextNo response |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
It's for optimization purposes. This can be useful if your return data is big and you are sure that it's serializable with JSON. As it's said in the documentation, by returning
https://fastapi.tiangolo.com/advanced/custom-response/#use-orjsonresponse
|
Beta Was this translation helpful? Give feedback.
It's for optimization purposes. This can be useful if your return data is big and you are sure that it's serializable with JSON.
As it's said in the documentation, by returning
ORJSONResponse([{"item_id": "Foo"}])you canhttps://fastapi.tiangolo.com/advanced/custom-response/#use-orjsonresponse
ORJSONResponseis inherited from theResponseclass. So, in this case you will save some execution time by not passing your return content through thejsonable_encoder. If you just return[{"item_id": "Foo"}], FastAPI will pass it through thejsonab…