Home / Class/ JSONProvider Class — flask Architecture

JSONProvider Class — flask Architecture

Architecture documentation for the JSONProvider class in provider.py from the flask codebase.

Entity Profile

Dependency Diagram

graph TD
  a668812f_fdf3_10bc_0342_d263f9406af1["JSONProvider"]
  64bf3a3f_ca45_015e_bd00_0190cbad6928["provider.py"]
  a668812f_fdf3_10bc_0342_d263f9406af1 -->|defined in| 64bf3a3f_ca45_015e_bd00_0190cbad6928
  c6e53dd0_66e7_628b_6a4f_fd8a49bc2b9c["__init__()"]
  a668812f_fdf3_10bc_0342_d263f9406af1 -->|method| c6e53dd0_66e7_628b_6a4f_fd8a49bc2b9c
  c50f4470_4854_122b_46a8_654e504d1d65["dumps()"]
  a668812f_fdf3_10bc_0342_d263f9406af1 -->|method| c50f4470_4854_122b_46a8_654e504d1d65
  8f13dd58_25bc_a85c_2acf_75a1c258d4e4["dump()"]
  a668812f_fdf3_10bc_0342_d263f9406af1 -->|method| 8f13dd58_25bc_a85c_2acf_75a1c258d4e4
  fc1b0163_014b_ac35_b9ce_96376d1a2f4f["loads()"]
  a668812f_fdf3_10bc_0342_d263f9406af1 -->|method| fc1b0163_014b_ac35_b9ce_96376d1a2f4f
  a11c1ff7_ae84_492d_e6a8_372832d94fbf["load()"]
  a668812f_fdf3_10bc_0342_d263f9406af1 -->|method| a11c1ff7_ae84_492d_e6a8_372832d94fbf
  9104e3d3_6e3d_cb00_d366_f06b8ffa8caa["_prepare_response_obj()"]
  a668812f_fdf3_10bc_0342_d263f9406af1 -->|method| 9104e3d3_6e3d_cb00_d366_f06b8ffa8caa
  aec4f243_b66b_1e41_ea9e_51f8c611233d["response()"]
  a668812f_fdf3_10bc_0342_d263f9406af1 -->|method| aec4f243_b66b_1e41_ea9e_51f8c611233d

Relationship Graph

Source Code

src/flask/json/provider.py lines 19–105

class JSONProvider:
    """A standard set of JSON operations for an application. Subclasses
    of this can be used to customize JSON behavior or use different
    JSON libraries.

    To implement a provider for a specific library, subclass this base
    class and implement at least :meth:`dumps` and :meth:`loads`. All
    other methods have default implementations.

    To use a different provider, either subclass ``Flask`` and set
    :attr:`~flask.Flask.json_provider_class` to a provider class, or set
    :attr:`app.json <flask.Flask.json>` to an instance of the class.

    :param app: An application instance. This will be stored as a
        :class:`weakref.proxy` on the :attr:`_app` attribute.

    .. versionadded:: 2.2
    """

    def __init__(self, app: App) -> None:
        self._app: App = weakref.proxy(app)

    def dumps(self, obj: t.Any, **kwargs: t.Any) -> str:
        """Serialize data as JSON.

        :param obj: The data to serialize.
        :param kwargs: May be passed to the underlying JSON library.
        """
        raise NotImplementedError

    def dump(self, obj: t.Any, fp: t.IO[str], **kwargs: t.Any) -> None:
        """Serialize data as JSON and write to a file.

        :param obj: The data to serialize.
        :param fp: A file opened for writing text. Should use the UTF-8
            encoding to be valid JSON.
        :param kwargs: May be passed to the underlying JSON library.
        """
        fp.write(self.dumps(obj, **kwargs))

    def loads(self, s: str | bytes, **kwargs: t.Any) -> t.Any:
        """Deserialize data as JSON.

        :param s: Text or UTF-8 bytes.
        :param kwargs: May be passed to the underlying JSON library.
        """
        raise NotImplementedError

    def load(self, fp: t.IO[t.AnyStr], **kwargs: t.Any) -> t.Any:
        """Deserialize data as JSON read from a file.

        :param fp: A file opened for reading text or UTF-8 bytes.
        :param kwargs: May be passed to the underlying JSON library.
        """
        return self.loads(fp.read(), **kwargs)

    def _prepare_response_obj(
        self, args: tuple[t.Any, ...], kwargs: dict[str, t.Any]
    ) -> t.Any:
        if args and kwargs:
            raise TypeError("app.json.response() takes either args or kwargs, not both")

        if not args and not kwargs:
            return None

        if len(args) == 1:
            return args[0]

        return args or 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 the ``application/json``
        mimetype.

        The :func:`~flask.json.jsonify` function calls this method for
        the current application.

        Either positional or keyword arguments can be given, not both.
        If no arguments are given, ``None`` is serialized.

Domain

Frequently Asked Questions

What is the JSONProvider class?
JSONProvider is a class in the flask codebase, defined in src/flask/json/provider.py.
Where is JSONProvider defined?
JSONProvider is defined in src/flask/json/provider.py at line 19.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free