Skip to content

json_file_operations

Functions for storing dicts to and loading dicts from json files.

load_json

load_json(path)

Load the info in the json file given by path and return as dict

Source code in src/application_settings/_private/json_file_operations.py
12
13
14
15
16
17
18
19
20
21
22
def load_json(path: Path) -> dict[str, Any]:
    """Load the info in the json file given by path and return as dict"""
    data_stored: dict[str, Any] = {}
    if (
        path.stat().st_size > 0
    ):  # this evaluates to false if the file does not exist or is empty
        with path.open(mode="r") as fptr:
            data_stored = json.load(fptr)
    else:
        logger.warning(f"File {path} does not exist or is empty.")
    return data_stored

save_json

save_json(path, data)

Update the json file given by path with the data

Source code in src/application_settings/_private/json_file_operations.py
25
26
27
28
29
30
def save_json(path: Path, data: dict[str, Any]) -> None:
    """Update the json file given by path with the data"""
    old_data = load_json(path)
    updated_data = deep_update(old_data, data)
    with path.open(mode="w") as fptr:
        json.dump(updated_data, fptr)