docs_src/custom_response/tutorial009c_py310.py
raw| 1 | from typing import Any |
| 2 | |
| 3 | import orjson |
| 4 | from fastapi import FastAPI, Response |
| 5 | |
| 6 | app = FastAPI() |
| 7 | |
| 8 | |
| 9 | class CustomORJSONResponse(Response): |
| 10 | media_type = "application/json" |
| 11 | |
| 12 | def render(self, content: Any) -> bytes: |
| 13 | assert orjson is not None, "orjson must be installed" |
| 14 | return orjson.dumps(content, option=orjson.OPT_INDENT_2) |
| 15 | |
| 16 | |
| 17 | @app.get("/", response_class=CustomORJSONResponse) |
| 18 | async def main(): |
| 19 | return {"message": "Hello World"} |