Coverage for fastapi/responses.py: 100%
20 statements
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2025-05-05 00:03 +0000
1from typing import Any 1abcdef
3from starlette.responses import FileResponse as FileResponse # noqa 1abcdef
4from starlette.responses import HTMLResponse as HTMLResponse # noqa 1abcdef
5from starlette.responses import JSONResponse as JSONResponse # noqa 1abcdef
6from starlette.responses import PlainTextResponse as PlainTextResponse # noqa 1abcdef
7from starlette.responses import RedirectResponse as RedirectResponse # noqa 1abcdef
8from starlette.responses import Response as Response # noqa 1abcdef
9from starlette.responses import StreamingResponse as StreamingResponse # noqa 1abcdef
11try: 1abcdef
12 import ujson 1abcdef
13except ImportError: # pragma: nocover
14 ujson = None # type: ignore
17try: 1abcdef
18 import orjson 1abcdef
19except ImportError: # pragma: nocover
20 orjson = None # type: ignore
23class UJSONResponse(JSONResponse): 1abcdef
24 """
25 JSON response using the high-performance ujson library to serialize data to JSON.
27 Read more about it in the
28 [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).
29 """
31 def render(self, content: Any) -> bytes: 1abcdef
32 assert ujson is not None, "ujson must be installed to use UJSONResponse" 1ghijkl
33 return ujson.dumps(content, ensure_ascii=False).encode("utf-8") 1ghijkl
36class ORJSONResponse(JSONResponse): 1abcdef
37 """
38 JSON response using the high-performance orjson library to serialize data to JSON.
40 Read more about it in the
41 [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).
42 """
44 def render(self, content: Any) -> bytes: 1abcdef
45 assert orjson is not None, "orjson must be installed to use ORJSONResponse" 1mnopqrstuvwx
46 return orjson.dumps( 1mnopqrstuvwx
47 content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
48 )