Coverage for fastapi / responses.py: 100%
25 statements
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
« prev ^ index » next coverage.py v7.13.3, created at 2026-04-06 01:24 +0000
1from typing import Any 1abcd
3from fastapi.exceptions import FastAPIDeprecationWarning 1abcd
4from fastapi.sse import EventSourceResponse as EventSourceResponse # noqa 1abcd
5from starlette.responses import FileResponse as FileResponse # noqa 1abcd
6from starlette.responses import HTMLResponse as HTMLResponse # noqa 1abcd
7from starlette.responses import JSONResponse as JSONResponse # noqa 1abcd
8from starlette.responses import PlainTextResponse as PlainTextResponse # noqa 1abcd
9from starlette.responses import RedirectResponse as RedirectResponse # noqa 1abcd
10from starlette.responses import Response as Response # noqa 1abcd
11from starlette.responses import StreamingResponse as StreamingResponse # noqa 1abcd
12from typing_extensions import deprecated 1abcd
14try: 1abcd
15 import ujson 1abcd
16except ImportError: # pragma: nocover
17 ujson = None # type: ignore
20try: 1abcd
21 import orjson 1abcd
22except ImportError: # pragma: nocover
23 orjson = None # type: ignore
26@deprecated( 1abcd
27 "UJSONResponse is deprecated, FastAPI now serializes data directly to JSON "
28 "bytes via Pydantic when a return type or response model is set, which is "
29 "faster and doesn't need a custom response class. Read more in the FastAPI "
30 "docs: https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model "
31 "and https://fastapi.tiangolo.com/tutorial/response-model/",
32 category=FastAPIDeprecationWarning,
33 stacklevel=2,
34)
35class UJSONResponse(JSONResponse): 1abcd
36 """JSON response using the ujson library to serialize data to JSON.
38 **Deprecated**: `UJSONResponse` is deprecated. FastAPI now serializes data
39 directly to JSON bytes via Pydantic when a return type or response model is
40 set, which is faster and doesn't need a custom response class.
42 Read more in the
43 [FastAPI docs for Custom Response](https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model)
44 and the
45 [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
47 **Note**: `ujson` is not included with FastAPI and must be installed
48 separately, e.g. `pip install ujson`.
49 """
51 def render(self, content: Any) -> bytes: 1abcd
52 assert ujson is not None, "ujson must be installed to use UJSONResponse" 1efghijklmno
53 return ujson.dumps(content, ensure_ascii=False).encode("utf-8") 1efghijklmno
56@deprecated( 1abcd
57 "ORJSONResponse is deprecated, FastAPI now serializes data directly to JSON "
58 "bytes via Pydantic when a return type or response model is set, which is "
59 "faster and doesn't need a custom response class. Read more in the FastAPI "
60 "docs: https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model "
61 "and https://fastapi.tiangolo.com/tutorial/response-model/",
62 category=FastAPIDeprecationWarning,
63 stacklevel=2,
64)
65class ORJSONResponse(JSONResponse): 1abcd
66 """JSON response using the orjson library to serialize data to JSON.
68 **Deprecated**: `ORJSONResponse` is deprecated. FastAPI now serializes data
69 directly to JSON bytes via Pydantic when a return type or response model is
70 set, which is faster and doesn't need a custom response class.
72 Read more in the
73 [FastAPI docs for Custom Response](https://fastapi.tiangolo.com/advanced/custom-response/#orjson-or-response-model)
74 and the
75 [FastAPI docs for Response Model](https://fastapi.tiangolo.com/tutorial/response-model/).
77 **Note**: `orjson` is not included with FastAPI and must be installed
78 separately, e.g. `pip install orjson`.
79 """
81 def render(self, content: Any) -> bytes: 1abcd
82 assert orjson is not None, "orjson must be installed to use ORJSONResponse" 1pqrstuvwxyzABC
83 return orjson.dumps( 1pqrstuvwxyzABC
84 content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
85 )