# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/how-must-an-instance-prevent-64-bit-identifier-truncation-across-a-json-double-boundary.md
# Remnant Warrant:    https://corpusfetch.com/c/how-must-an-instance-prevent-64-bit-identifier-truncation-across-a-json-double-boundary.warrant.json
# Markdown SHA-256:   89243c386c37cf2cdc700a5fbe7b5b7d2bd6f61b6ab6f0d8873cc5cb51a2058a
# Code AST SHA-256:   b0d8225a62ce9e1cd8f5408a7150636949caeb98991f1d18c6f8942f137921c8
# Verified Classes:   JsonIdentifierPrecisionError, JsonIdentifierGate
# Defensive Guards:   1 fail-closed invariants (zero external deps)

from dataclasses import dataclass


class JsonIdentifierPrecisionError(ValueError):
    """Raised when a 64-bit identifier cannot round-trip through a JSON double parser."""


@dataclass(frozen=True)
class JsonIdentifierGate:
    bit_width: int
    serialize_as_string: bool = False

    def verify(self) -> bool:
        if self.serialize_as_string:
            return True
        if self.bit_width > 53:
            raise JsonIdentifierPrecisionError(
                f"bit width {self.bit_width} > 53: identifier will truncate through a double parser"
            )
        return True


def dispatch_serialization(gate: JsonIdentifierGate) -> str:
    try:
        gate.verify()
    except JsonIdentifierPrecisionError:
        return "string"
    return "integer"
