Skip to content

UJSON class

esmerald.datastructures.encoders.UJSON

UJSON(content=None, status_code=None, **kwargs)

Bases: ResponseContainer[UJSONResponse]

PARAMETER DESCRIPTION
content

TYPE: Optional[Dict[str, Any]] DEFAULT: None

status_code

TYPE: Optional[int] DEFAULT: None

**kwargs

TYPE: Any DEFAULT: {}

Source code in esmerald/datastructures/encoders.py
110
111
112
113
114
115
116
117
118
def __init__(
    self,
    content: Optional[Dict[str, Any]] = None,
    status_code: Optional[int] = None,
    **kwargs: Any,
) -> None:
    super().__init__(**kwargs)
    self.content = content
    self.status_code = status_code

media_type class-attribute instance-attribute

media_type = 'application/json'

The media type of the response.

content class-attribute instance-attribute

content = content

The content being sent to the response.

status_code class-attribute instance-attribute

status_code = status_code

The status code of the response. It will default to the handler if none is provided.

to_response

to_response(headers, media_type, status_code, app)
PARAMETER DESCRIPTION
headers

TYPE: Dict[str, Any]

media_type

TYPE: Union[MediaType, str]

status_code

TYPE: int

app

TYPE: Type[Esmerald]

Source code in esmerald/datastructures/encoders.py
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
def to_response(
    self,
    headers: Dict[str, Any],
    media_type: Union["MediaType", str],
    status_code: int,
    app: Type["Esmerald"],
) -> UJSONResponse:
    assert (
        UJSONResponse is not None
    ), "You must install the encoders or ujson to use UJSONResponse"
    status = self.status_code or status_code

    return UJSONResponse(
        content=self.content,
        headers=headers,
        status_code=status,
        media_type=media_type,
        background=self.background,
    )