Coverage for fastapi / responses.py: 100%

20 statements  

« prev     ^ index     » next       coverage.py v7.13.3, created at 2026-02-12 18:15 +0000

1from typing import Any 1abcd

2 

3from starlette.responses import FileResponse as FileResponse # noqa 1abcd

4from starlette.responses import HTMLResponse as HTMLResponse # noqa 1abcd

5from starlette.responses import JSONResponse as JSONResponse # noqa 1abcd

6from starlette.responses import PlainTextResponse as PlainTextResponse # noqa 1abcd

7from starlette.responses import RedirectResponse as RedirectResponse # noqa 1abcd

8from starlette.responses import Response as Response # noqa 1abcd

9from starlette.responses import StreamingResponse as StreamingResponse # noqa 1abcd

10 

11try: 1abcd

12 import ujson 1abcd

13except ImportError: # pragma: nocover 

14 ujson = None # type: ignore 

15 

16 

17try: 1abcd

18 import orjson 1abcd

19except ImportError: # pragma: nocover 

20 orjson = None # type: ignore 

21 

22 

23class UJSONResponse(JSONResponse): 1abcd

24 """ 

25 JSON response using the high-performance ujson library to serialize data to JSON. 

26 

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 """ 

30 

31 def render(self, content: Any) -> bytes: 1abcd

32 assert ujson is not None, "ujson must be installed to use UJSONResponse" 1efg

33 return ujson.dumps(content, ensure_ascii=False).encode("utf-8") 1efg

34 

35 

36class ORJSONResponse(JSONResponse): 1abcd

37 """ 

38 JSON response using the high-performance orjson library to serialize data to JSON. 

39 

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 """ 

43 

44 def render(self, content: Any) -> bytes: 1abcd

45 assert orjson is not None, "orjson must be installed to use ORJSONResponse" 1heijfklgm

46 return orjson.dumps( 1heijfklgm

47 content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY 

48 )