Skip to content

declearn.utils.json_dump

Dump a given object to a JSON file, using extended types support.

This function is merely a shortcut to run the following code:

>>> with open(path, "w", encoding=encoding) as file:
>>>     json.dump(obj, file, default=declearn.utils.json_pack)

See declearn.utils.add_json_support to extend the behaviour of JSON (de)serialization to non-standard types, that will be used as part of this function.

See declearn.utils.json_load for the counterpart method.

Source code in declearn/utils/_json.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
def json_dump(
    obj: Any,
    path: str,
    encoding: str = "utf-8",
    indent: Optional[int] = None,
) -> None:
    """Dump a given object to a JSON file, using extended types support.

    This function is merely a shortcut to run the following code:
    ```
    >>> with open(path, "w", encoding=encoding) as file:
    >>>     json.dump(obj, file, default=declearn.utils.json_pack)
    ```

    See `declearn.utils.add_json_support` to extend the behaviour
    of JSON (de)serialization to non-standard types, that will be
    used as part of this function.

    See `declearn.utils.json_load` for the counterpart method.
    """
    with open(path, "w", encoding=encoding) as file:
        json.dump(obj, file, default=json_pack, indent=indent)