AndesOps-AI / backend /app /services /persistence.py
Álvaro Valenzuela Valdes
deploy: v24 final avatar, localization and HF config hotfix
5e52bd7
raw
history blame contribute delete
799 Bytes
import json
from pathlib import Path
from typing import List, Type, TypeVar
from pydantic import BaseModel
T = TypeVar("T", bound=BaseModel)
DATA_DIR = Path(__file__).resolve().parent.parent / "data"
DATA_DIR.mkdir(exist_ok=True)
def save_to_json(data: List[BaseModel], filename: str):
path = DATA_DIR / filename
with path.open("w", encoding="utf-8") as f:
json.dump([item.model_dump(mode="json") for item in data], f, indent=2, ensure_ascii=False)
def load_from_json(model_class: Type[T], filename: str) -> List[T]:
path = DATA_DIR / filename
if not path.exists():
return []
with path.open("r", encoding="utf-8") as f:
try:
raw = json.load(f)
return [model_class(**item) for item in raw]
except:
return []