forked from mengyxu/noob-components
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
909 B
37 lines
909 B
""" |
|
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
|
|
|