DefaultJSONProvider Class — flask Architecture
Architecture documentation for the DefaultJSONProvider class in provider.py from the flask codebase.
Entity Profile
Dependency Diagram
graph TD 7dc7dbfa_612a_e2fa_73ef_39f4b6b74ab2["DefaultJSONProvider"] a668812f_fdf3_10bc_0342_d263f9406af1["JSONProvider"] 7dc7dbfa_612a_e2fa_73ef_39f4b6b74ab2 -->|extends| a668812f_fdf3_10bc_0342_d263f9406af1 64bf3a3f_ca45_015e_bd00_0190cbad6928["provider.py"] 7dc7dbfa_612a_e2fa_73ef_39f4b6b74ab2 -->|defined in| 64bf3a3f_ca45_015e_bd00_0190cbad6928 298532b3_e403_bc7d_d6ab_a15e33dea870["dumps()"] 7dc7dbfa_612a_e2fa_73ef_39f4b6b74ab2 -->|method| 298532b3_e403_bc7d_d6ab_a15e33dea870 8b885d9c_52b6_0466_0055_5ab6516c615c["loads()"] 7dc7dbfa_612a_e2fa_73ef_39f4b6b74ab2 -->|method| 8b885d9c_52b6_0466_0055_5ab6516c615c 41f60dba_c742_8eff_04cc_8577b192c235["response()"] 7dc7dbfa_612a_e2fa_73ef_39f4b6b74ab2 -->|method| 41f60dba_c742_8eff_04cc_8577b192c235
Relationship Graph
Source Code
src/flask/json/provider.py lines 124–215
class DefaultJSONProvider(JSONProvider):
"""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.
"""
default: t.Callable[[t.Any], t.Any] = staticmethod(_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``.
"""
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
"""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.
"""
mimetype = "application/json"
"""The mimetype set in :meth:`response`."""
def dumps(self, 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.
:param obj: The data to serialize.
:param kwargs: Passed to :func:`json.dumps`.
"""
kwargs.setdefault("default", self.default)
kwargs.setdefault("ensure_ascii", self.ensure_ascii)
kwargs.setdefault("sort_keys", self.sort_keys)
return json.dumps(obj, **kwargs)
def loads(self, s: str | bytes, **kwargs: t.Any) -> t.Any:
"""Deserialize data as JSON from a string or bytes.
:param s: Text or UTF-8 bytes.
:param kwargs: Passed to :func:`json.loads`.
"""
return json.loads(s, **kwargs)
def response(self, *args: t.Any, **kwargs: t.Any) -> 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.
: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.
"""
Domain
Defined In
Extends
Source
Frequently Asked Questions
What is the DefaultJSONProvider class?
DefaultJSONProvider is a class in the flask codebase, defined in src/flask/json/provider.py.
Where is DefaultJSONProvider defined?
DefaultJSONProvider is defined in src/flask/json/provider.py at line 124.
What does DefaultJSONProvider extend?
DefaultJSONProvider extends JSONProvider.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free