lllin000_PaperForge/paperforge/core/io.py
Research Assistant ed95e0f565 feat: runtime contract hardening + skill/command truth alignment (Package A+B)
Atomic snapshots, canonical index mutation serialization, sync post-clean truth,
plugin path config-awareness, embed stop signal honesty, full snapshot bootstrap,
pf_ prefix unification, workflow command/lifecycle/path corrections,
mechanical/cognitive route separation with unknown-command guard.
2026-05-16 22:38:43 +08:00

22 lines
664 B
Python

from __future__ import annotations
import json
from pathlib import Path
def read_json(path: Path):
"""Read and parse a JSON file."""
return json.loads(path.read_text(encoding="utf-8"))
def write_json_atomic(path: Path, data) -> None:
"""Write JSON atomically using a temp file and replace."""
path.parent.mkdir(parents=True, exist_ok=True)
tmp = path.with_suffix(f"{path.suffix}.tmp")
tmp.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding="utf-8")
tmp.replace(path)
def write_json(path: Path, data) -> None:
"""Write data as JSON, creating parent directories as needed."""
write_json_atomic(path, data)