INNER CODE UNIT · Python

save2json

DogsTailFarmer/martin-binance · martin_binance/backup.py:115

def save2json(model_instance, file_path: Path) -> None:
    """
    Universally formats a Pydantic model into JSON with keys sorted alphabetically.
    Uses ultra-fast orjson to serialize nested structures.
    Performs an ATOMIC (safe) overwrite of the file on disk
    """
    data = model_instance.model_dump(mode='json')

    lines = ["{"]
    sorted_keys = sorted(data.keys())

    for idx, key in enumerate(sorted_keys):
        value = data[key]
        is_last = idx == len(sorted_keys) - 1
        comma = "" if is_last else ","

        if key.startswith("orders") and isinstance(value, dict):
            lines.append(f'    "{key}": {{')

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…