Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.
 
 
 
 

49 строки
1.7 KiB

  1. from typing import Any
  2. from starlette.responses import FileResponse as FileResponse # noqa
  3. from starlette.responses import HTMLResponse as HTMLResponse # noqa
  4. from starlette.responses import JSONResponse as JSONResponse # noqa
  5. from starlette.responses import PlainTextResponse as PlainTextResponse # noqa
  6. from starlette.responses import RedirectResponse as RedirectResponse # noqa
  7. from starlette.responses import Response as Response # noqa
  8. from starlette.responses import StreamingResponse as StreamingResponse # noqa
  9. try:
  10. import ujson
  11. except ImportError: # pragma: nocover
  12. ujson = None # type: ignore
  13. try:
  14. import orjson
  15. except ImportError: # pragma: nocover
  16. orjson = None # type: ignore
  17. class UJSONResponse(JSONResponse):
  18. """
  19. JSON response using the high-performance ujson library to serialize data to JSON.
  20. Read more about it in the
  21. [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).
  22. """
  23. def render(self, content: Any) -> bytes:
  24. assert ujson is not None, "ujson must be installed to use UJSONResponse"
  25. return ujson.dumps(content, ensure_ascii=False).encode("utf-8")
  26. class ORJSONResponse(JSONResponse):
  27. """
  28. JSON response using the high-performance orjson library to serialize data to JSON.
  29. Read more about it in the
  30. [FastAPI docs for Custom Response - HTML, Stream, File, others](https://fastapi.tiangolo.com/advanced/custom-response/).
  31. """
  32. def render(self, content: Any) -> bytes:
  33. assert orjson is not None, "orjson must be installed to use ORJSONResponse"
  34. return orjson.dumps(
  35. content, option=orjson.OPT_NON_STR_KEYS | orjson.OPT_SERIALIZE_NUMPY
  36. )