from pathlib import Path
from typing import Optional
import json


def save_json(data: dict, source: Path, overwrite: Optional[bool]=False) -> bool:
    """
    Save a dictionary as a json file. Return `False` if file `source` already exists and not
    `overwrite`.

    Parameters
    ----------
    data: dict
        Dictionary to be saved
    source: Path
        Path to filename to open
    overwrite: bool
        True to overwrite existing file

    Returns
    -------
    bool
        True if saved, False if already exists without `overwrite`
    """
    if Path(source).exists() and not overwrite:
        e = F"`{source}` already exists. Set `overwrite` to `True`."
        raise FileExistsError(e)
    with open(source, "w") as f:
        json.dump(data, f, indent=4, sort_keys=True, default=str)
    return True