| 12345678910111213141516171819202122232425262728293031323334353637 |
- """
- JSON file I/O utilities.
- Provides read_json and write_json as the single source of truth
- for JSON file operations across all Trellis scripts.
- """
- from __future__ import annotations
- import json
- from pathlib import Path
- def read_json(path: Path) -> dict | None:
- """Read and parse a JSON file.
- Returns None if the file doesn't exist, is invalid JSON, or can't be read.
- """
- try:
- return json.loads(path.read_text(encoding="utf-8"))
- except (FileNotFoundError, json.JSONDecodeError, OSError):
- return None
- def write_json(path: Path, data: dict) -> bool:
- """Write dict to JSON file with pretty formatting.
- Returns True on success, False on error.
- """
- try:
- path.write_text(
- json.dumps(data, indent=2, ensure_ascii=False),
- encoding="utf-8",
- )
- return True
- except (OSError, IOError):
- return False
|