# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-does-a-64-bit-identifier-lose-precision-when-serialized-as-json.md
# Remnant Warrant:    https://corpusfetch.com/c/when-does-a-64-bit-identifier-lose-precision-when-serialized-as-json.warrant.json
# Markdown SHA-256:   72086428ba0ba393d2d74af03786479277a1cc82ad94b3db6d275efdf43398f3
# Code AST SHA-256:   ed516423fe185c2596f23ca9d3938f1d9285f49e691f05224698deadd9c49edc
# 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} exceeds 53: value cannot round-trip through IEEE-754"
            )
        return True


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