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] | Apply this function to any object that :meth:json.dumps does not know how to serialize. It should return a valid JSON type or raise a TypeError. |
| ensure_ascii | = True | Replace non-ASCII characters with escape sequences. This may be more compatible with some clients, but can be disabled for better performance and size. |
| sort_keys | = True | Sort the keys in any serialized dicts. This may be useful for some caching situations, but can be disabled for better performance. When enabled, keys must all be strings, they are not converted before sorting. |
| compact | `bool | None` = None |
| mimetype | = "application/json" | The mimetype set in :meth:response. |
Methods
dumps()
@classmethod
def dumps(
obj: t.Any,
kwargs: t.Any
) - > str
Serialize data as JSON to a string. Keyword arguments are passed to :func:json.dumps. Sets some parameter defaults from the :attr:default, :attr:ensure_ascii, and :attr:sort_keys attributes.
Parameters
| Name | Type | Description |
|---|---|---|
| obj | t.Any | The data to serialize. |
| kwargs | t.Any | Passed to :func:json.dumps. |
Returns
| Type | Description |
|---|---|
str | The JSON serialized string. |
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 :func:json.loads. |
Returns
| Type | Description |
|---|---|
t.Any | The deserialized Python object. |
response()
@classmethod
def response(
args: t.Any,
kwargs: t.Any
) - > [Response](../../wrappers/response.md?sid=flask_wrappers_response)
Serialize the given arguments as JSON, and return a :class:~flask.Response object with it. The response mimetype will be "application/json" and can be changed with :attr:mimetype. If :attr:compact is False or debug mode is enabled, the output will be formatted to be easier to read. Either positional or keyword arguments can be given, not both. If no arguments are given, None is serialized.
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=flask_wrappers_response) | A Flask Response object containing the JSON serialized data. |