responses.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. from typing import Any
  2. from fastapi.exceptions import FastAPIDeprecationWarning
  3. from fastapi.sse import EventSourceResponse as EventSourceResponse # noqa
  4. from starlette.responses import FileResponse as FileResponse # noqa
  5. from starlette.responses import HTMLResponse as HTMLResponse # noqa
  6. from starlette.responses import JSONResponse as JSONResponse # noqa
  7. from starlette.responses import PlainTextResponse as PlainTextResponse # noqa
  8. from starlette.responses import RedirectResponse as RedirectResponse # noqa
  9. from starlette.responses import Response as Response # noqa
  10. from starlette.responses import StreamingResponse as StreamingResponse # noqa
  11. from typing_extensions import deprecated
  12. try:
  13. import ujson
  14. except ImportError: # pragma: nocover
  15. ujson = None # type: ignore
  16. try:
  17. import orjson
  18. except ImportError: # pragma: nocover
  19. orjson = None # type: ignore
  20. @deprecated(
  21. "UJSONResponse is deprecated, FastAPI now serializes data directly to JSON "
  22. "bytes via Pydantic when a return type or response model is set, which is "
  23. "faster and doesn't need a custom response class. Read more in the FastAPI "
  24. "docs: https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model "
  25. "and https://fastapi.tiangolo.com/tutorial/response-model/",
  26. category=FastAPIDeprecationWarning,
  27. stacklevel=2,
  28. )
  29. class UJSONResponse(JSONResponse):
  30. """JSON response using the ujson library to serialize data to JSON.
  31. **Deprecated**: `UJSONResponse` is deprecated. FastAPI now serializes data
  32. directly to JSON bytes via Pydantic when a return type or response model is
  33. set, which is faster and doesn't need a custom response class.
  34. Read more in the
  35. [FastAPI docs for Custom Response](https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model)
  36. and the
  37. [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
  38. **Note**: `ujson` is not included with FastAPI and must be installed
  39. separately, e.g. `pip install ujson`.
  40. """
  41. def render(self, content: Any) -> bytes:
  42. assert ujson is not None, "ujson must be installed to use UJSONResponse"
  43. return ujson.dumps(content, ensure_ascii=False).encode("utf-8")
  44. @deprecated(
  45. "ORJSONResponse is deprecated, FastAPI now serializes data directly to JSON "
  46. "bytes via Pydantic when a return type or response model is set, which is "
  47. "faster and doesn't need a custom response class. Read more in the FastAPI "
  48. "docs: https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model "
  49. "and https://fastapi.tiangolo.com/tutorial/response-model/",
  50. category=FastAPIDeprecationWarning,
  51. stacklevel=2,
  52. )
  53. class ORJSONResponse(JSONResponse):
  54. """JSON response using the orjson library to serialize data to JSON.
  55. **Deprecated**: `ORJSONResponse` is deprecated. FastAPI now serializes data
  56. directly to JSON bytes via Pydantic when a return type or response model is
  57. set, which is faster and doesn't need a custom response class.
  58. Read more in the
  59. [FastAPI docs for Custom Response](https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model)
  60. and the
  61. [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
  62. **Note**: `orjson` is not included with FastAPI and must be installed
  63. separately, e.g. `pip install orjson`.
  64. """
  65. def render(self, content: Any) -> bytes:
  66. assert orjson is not None, "orjson must be installed to use ORJSONResponse"
  67. return orjson.dumps(
  68. content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
  69. )