Custom JSON Response

class MsgSpecJSONResponse(content: Any, status_code: int = 200, headers: Mapping[str, str] | None = None, media_type: str = 'application/json', background: BackgroundTask | None = None)[source]

Bases: Response

High-performance JSON response using msgspec for serialization.

__init__(content: Any, status_code: int = 200, headers: Mapping[str, str] | None = None, media_type: str = 'application/json', background: BackgroundTask | None = None) None[source]

Initializes the custom response class.

Parameters:
  • content – The content to be serialized.

  • status_code (int) – HTTP status code.

  • headers (Mapping[str, str] | None) – HTTP headers.

  • media_type (str) – Media type (default: application/json).

  • background (BackgroundTask | None) – Background task to run after the response is sent.

render(content: Any) bytes[source]

Serialize the content using the msgspec encoder.

Parameters:

content – The data to serialize.

Returns:

Serialized JSON content as bytes.

Return type:

bytes