"""Canonical JSON — the one serialization used for every ``generated/`` file.

Sorted keys, UTF-8, two-space indent, trailing newline. Deterministic so that
``regenerate -> byte-identical`` is a meaningful equality and ``sha256`` is stable.
"""

from __future__ import annotations

import json
from typing import Any


def dumps(obj: Any) -> str:
    """Serialize to canonical JSON text (stable across runs and machines)."""
    return json.dumps(obj, sort_keys=True, ensure_ascii=False, indent=2) + "\n"


def dumpb(obj: Any) -> bytes:
    """Serialize to canonical JSON bytes (what we hash and write)."""
    return dumps(obj).encode("utf-8")
