class documentation

class DefaultJSONProvider(JSONProvider): (source)

View In Hierarchy

Provide JSON operations using Python's built-in :mod:`json` library. Serializes the following additional data types: - :class:`datetime.datetime` and :class:`datetime.date` are serialized to :rfc:`822` strings. This is the same as the HTTP date format. - :class:`uuid.UUID` is serialized to a string. - :class:`dataclasses.dataclass` is passed to :func:`dataclasses.asdict`. - :class:`~markupsafe.Markup` (or any object with a ``__html__`` method) will call the ``__html__`` method to get a string.

Method dumps Serialize data as JSON to a string.
Method loads Deserialize data as JSON from a string or bytes.
Method 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`.
Class Variable compact If ``True``, or ``None`` out of debug mode, the :meth:`response` output will not add indentation, newlines, or spaces. If ``False``, or ``None`` in debug mode, it will use a non-compact representation.
Class Variable default 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``.
Class Variable ensure_ascii Replace non-ASCII characters with escape sequences. This may be more compatible with some clients, but can be disabled for better performance and size.
Class Variable mimetype The mimetype set in :meth:`response`.
Class Variable sort_keys 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.

Inherited from JSONProvider:

Method __init__ Undocumented
Method dump Serialize data as JSON and write to a file.
Method load Deserialize data as JSON read from a file.
Method _prepare_response_obj Undocumented
Instance Variable _app Undocumented
def dumps(self, obj: t.Any, **kwargs: t.Any) -> str: (source)

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. :param obj: The data to serialize. :param kwargs: Passed to :func:`json.dumps`.

def loads(self, s: str|bytes, **kwargs: t.Any) -> t.Any: (source)

Deserialize data as JSON from a string or bytes. :param s: Text or UTF-8 bytes. :param kwargs: Passed to :func:`json.loads`.

def response(self, *args: t.Any, **kwargs: t.Any) -> Response: (source)

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. :param args: A single value to serialize, or multiple values to treat as a list to serialize. :param kwargs: Treat as a dict to serialize.

If ``True``, or ``None`` out of debug mode, the :meth:`response` output will not add indentation, newlines, or spaces. If ``False``, or ``None`` in debug mode, it will use a non-compact representation.

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: bool = (source)

Replace non-ASCII characters with escape sequences. This may be more compatible with some clients, but can be disabled for better performance and size.

mimetype: str = (source)

The mimetype set in :meth:`response`.

sort_keys: bool = (source)

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.