mirror of
https://github.com/nihilvux/bancho.py.git
synced 2025-09-16 10:38:39 -07:00
30 lines
732 B
Python
30 lines
732 B
Python
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
import orjson
|
|
from fastapi.responses import JSONResponse
|
|
from pydantic import BaseModel
|
|
|
|
|
|
def _default_processor(data: Any) -> Any:
|
|
if isinstance(data, BaseModel):
|
|
return _default_processor(data.dict())
|
|
elif isinstance(data, dict):
|
|
return {k: _default_processor(v) for k, v in data.items()}
|
|
elif isinstance(data, list):
|
|
return [_default_processor(v) for v in data]
|
|
else:
|
|
return data
|
|
|
|
|
|
def dumps(data: Any) -> bytes:
|
|
return orjson.dumps(data, default=_default_processor)
|
|
|
|
|
|
class ORJSONResponse(JSONResponse):
|
|
media_type = "application/json"
|
|
|
|
def render(self, content: Any) -> bytes:
|
|
return dumps(content)
|