Skip to content

config_io

tit.config_io

JSON serialization for config dataclasses.

Provides helpers to serialize typed config dataclasses (with Enum fields, nested dataclasses, and union-typed ROI/electrode specs) to JSON files and reconstruct them back.

Usage::

from tit.config_io import write_config_json, read_config_json
path = write_config_json(my_flex_config, prefix="flex")
data = read_config_json(path)

serialize_config

serialize_config(config: Any) -> dict[str, Any]

Convert a dataclass to a JSON-serializable dict.

Handles: - Enum fields (uses .value) - Nested dataclasses (recursed) - Union-typed ROI / electrode specs (adds _type discriminator) - None values (preserved)

Source code in tit/config_io.py
def serialize_config(config: Any) -> dict[str, Any]:
    """Convert a dataclass to a JSON-serializable dict.

    Handles:
    - Enum fields (uses ``.value``)
    - Nested dataclasses (recursed)
    - Union-typed ROI / electrode specs (adds ``_type`` discriminator)
    - None values (preserved)
    """
    return _serialize(config)

write_config_json

write_config_json(config: Any, prefix: str = 'config') -> str

Serialize config dataclass to a temporary JSON file.

Returns the absolute file path.

Source code in tit/config_io.py
def write_config_json(config: Any, prefix: str = "config") -> str:
    """Serialize config dataclass to a temporary JSON file.

    Returns the absolute file path.
    """
    data = serialize_config(config)
    fd, path = tempfile.mkstemp(prefix=f"{prefix}_", suffix=".json")
    with os.fdopen(fd, "w") as f:
        json.dump(data, f, indent=2)
    return path

read_config_json

read_config_json(path: str) -> dict[str, Any]

Read a JSON config file and return the parsed dict.

Source code in tit/config_io.py
def read_config_json(path: str) -> dict[str, Any]:
    """Read a JSON config file and return the parsed dict."""
    with open(path) as f:
        return json.load(f)