| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- from typing import Any
- from fastapi.exceptions import FastAPIDeprecationWarning
- from fastapi.sse import EventSourceResponse as EventSourceResponse # noqa
- from starlette.responses import FileResponse as FileResponse # noqa
- from starlette.responses import HTMLResponse as HTMLResponse # noqa
- from starlette.responses import JSONResponse as JSONResponse # noqa
- from starlette.responses import PlainTextResponse as PlainTextResponse # noqa
- from starlette.responses import RedirectResponse as RedirectResponse # noqa
- from starlette.responses import Response as Response # noqa
- from starlette.responses import StreamingResponse as StreamingResponse # noqa
- from typing_extensions import deprecated
- try:
- import ujson
- except ImportError: # pragma: nocover
- ujson = None # type: ignore
- try:
- import orjson
- except ImportError: # pragma: nocover
- orjson = None # type: ignore
- @deprecated(
- "UJSONResponse is deprecated, FastAPI now serializes data directly to JSON "
- "bytes via Pydantic when a return type or response model is set, which is "
- "faster and doesn't need a custom response class. Read more in the FastAPI "
- "docs: https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model "
- "and https://fastapi.tiangolo.com/tutorial/response-model/",
- category=FastAPIDeprecationWarning,
- stacklevel=2,
- )
- class UJSONResponse(JSONResponse):
- """JSON response using the ujson library to serialize data to JSON.
- **Deprecated**: `UJSONResponse` is deprecated. FastAPI now serializes data
- directly to JSON bytes via Pydantic when a return type or response model is
- set, which is faster and doesn't need a custom response class.
- Read more in the
- [FastAPI docs for Custom Response](https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model)
- and the
- [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
- **Note**: `ujson` is not included with FastAPI and must be installed
- separately, e.g. `pip install ujson`.
- """
- def render(self, content: Any) -> bytes:
- assert ujson is not None, "ujson must be installed to use UJSONResponse"
- return ujson.dumps(content, ensure_ascii=False).encode("utf-8")
- @deprecated(
- "ORJSONResponse is deprecated, FastAPI now serializes data directly to JSON "
- "bytes via Pydantic when a return type or response model is set, which is "
- "faster and doesn't need a custom response class. Read more in the FastAPI "
- "docs: https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model "
- "and https://fastapi.tiangolo.com/tutorial/response-model/",
- category=FastAPIDeprecationWarning,
- stacklevel=2,
- )
- class ORJSONResponse(JSONResponse):
- """JSON response using the orjson library to serialize data to JSON.
- **Deprecated**: `ORJSONResponse` is deprecated. FastAPI now serializes data
- directly to JSON bytes via Pydantic when a return type or response model is
- set, which is faster and doesn't need a custom response class.
- Read more in the
- [FastAPI docs for Custom Response](https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model)
- and the
- [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
- **Note**: `orjson` is not included with FastAPI and must be installed
- separately, e.g. `pip install orjson`.
- """
- def render(self, content: Any) -> bytes:
- assert orjson is not None, "orjson must be installed to use ORJSONResponse"
- return orjson.dumps(
- content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
- )
|