DefaultJSONProvider
Provide JSON operations using Python's built-in :mod:json library. Serializes the following additional data types:
- :class:
datetime.datetimeand :class:datetime.dateare serialized to :rfc:822strings. This is the same as the HTTP date format. - :class:
uuid.UUIDis serialized to a string. - :class:
dataclasses.dataclassis passed to :func:dataclasses.asdict. - :class:
~markupsafe.Markup(or any object with a__html__method) will call the__html__method to get a string.
Attributes
| Attribute | Type | Description |
|---|---|---|
| default | t.Callable[[t.Any], t.Any] = _default | Apply this function to any object that json.dumps does not know how to serialize. |
| ensure_ascii | boolean = true | Replace non-ASCII characters with escape sequences. |
| sort_keys | boolean = true | Sort the keys in any serialized dicts. |
| compact | `bool | None` = null |
| mimetype | string = application/json | The mimetype set in response. |
Constructor
Signature
def DefaultJSONProvider()
Methods
dumps()
@classmethod
def dumps(
obj: t.Any,
kwargs: t.Any
) - > string
Serialize data as JSON to a string. Keyword arguments are passed to json.dumps. Sets some parameter defaults from the default, ensure_ascii, and sort_keys attributes.
Parameters
| Name | Type | Description |
|---|---|---|
| obj | t.Any | The data to serialize. |
| kwargs | t.Any | Passed to json.dumps. |
Returns
| Type | Description |
|---|---|
string | A JSON-formatted string representing the input object |
loads()
@classmethod
def loads(
s: str | bytes,
kwargs: t.Any
) - > t.Any
Deserialize data as JSON from a string or bytes.
Parameters
| Name | Type | Description |
|---|---|---|
| s | `str | bytes` |
| kwargs | t.Any | Passed to json.loads. |
Returns
| Type | Description |
|---|---|
t.Any | The Python object representation of the JSON input |
response()
@classmethod
def response(
args: t.Any,
kwargs: t.Any
) - > [Response](../../wrappers/response.md?sid=src_flask_wrappers_response)
Serialize the given arguments as JSON, and return a Response object with it. The response mimetype will be "application/json" and can be changed with mimetype.
Parameters
| Name | Type | Description |
|---|---|---|
| args | t.Any | A single value to serialize, or multiple values to treat as a list to serialize. |
| kwargs | t.Any | Treat as a dict to serialize. |
Returns
| Type | Description |
|---|---|
[Response](../../wrappers/response.md?sid=src_flask_wrappers_response) | A Flask Response object containing the serialized JSON data and the configured mimetype |