Coverage for fastapi/responses.py: 100%
20 statements
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
« prev ^ index » next coverage.py v7.6.1, created at 2024-08-08 03:53 +0000
1from typing import Any 1abcde
3from starlette.responses import FileResponse as FileResponse # noqa 1abcde
4from starlette.responses import HTMLResponse as HTMLResponse # noqa 1abcde
5from starlette.responses import JSONResponse as JSONResponse # noqa 1abcde
6from starlette.responses import PlainTextResponse as PlainTextResponse # noqa 1abcde
7from starlette.responses import RedirectResponse as RedirectResponse # noqa 1abcde
8from starlette.responses import Response as Response # noqa 1abcde
9from starlette.responses import StreamingResponse as StreamingResponse # noqa 1abcde
11try: 1abcde
12 import ujson 1abcde
13except ImportError: # pragma: nocover
14 ujson = None # type: ignore
17try: 1abcde
18 import orjson 1abcde
19except ImportError: # pragma: nocover
20 orjson = None # type: ignore
23class UJSONResponse(JSONResponse): 1abcde
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: 1abcde
32 assert ujson is not None, "ujson must be installed to use UJSONResponse" 1abcde
33 return ujson.dumps(content, ensure_ascii=False).encode("utf-8") 1abcde
36class ORJSONResponse(JSONResponse): 1abcde
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: 1abcde
45 assert orjson is not None, "orjson must be installed to use ORJSONResponse" 1abcde
46 return orjson.dumps( 1abcde
47 content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
48 )