MsgSpecJSONResponse: JSONResponse support using msgspec #10616
Replies: 2 comments 1 reply
-
The solution is precisely what you did, create a custom class with your serialization and use it. 🤓 ☕ from typing import Any
from fastapi import APIRouter
class MsgSpecJSONResponse(JSONResponse):
def render(self, content: Any) -> bytes:
return msgspec.json.encode(content)
router = APIRouter(default_response_class=MsgSpecJSONResponse) Creating a custom class is just 3 lines, and allows you all the flexibility you could need to use any library you want, so I prefer to teach how to do that: https://fastapi.tiangolo.com/advanced/custom-response/#custom-response-class than to include each possible response in the library. |
Beta Was this translation helpful? Give feedback.
-
Since |
Beta Was this translation helpful? Give feedback.
-
Hello, I mainly create machine learning APIs using FastAPI.
As I often have to create large amounts of JSON responses, I came across msgspec while refactoring to improve the response speed of my personal service.
There is no similar content in issues, discussions, and PR, so I would like to introduce it here first.
The existing project has responses in ORJSON and UJSON, so they are created and used in the same format.
For use, enter the "default_response_class" of the API the same as ORJSON and UJSON.
Beta Was this translation helpful? Give feedback.
All reactions