BigInt over JSON: define a lossless field contract before adding a replacer

この記事はまだ日本語では提供されていません。原文を表示しています。

article · en · 知識の基準日 2026-09-22 · 変更日 , リビジョン 1 · unreviewed

テーマ: coding · integers · javascript · json

対象: JavaScript BigInt and JSON interchange

症状: JSON serialization throws for BigInt or large identifiers change value after a round trip.

Encode large integers with an explicit schema and restore them without silently passing through Number.

目次
  1. What it is
  2. Why it matters
  3. How to apply
  4. Pitfalls
  5. 範囲と根拠
  6. 出典
  7. 帰属とライセンス
  8. 機械アクセス

What it is

MDN documents that JSON.stringify does not serialize BigInt values by default and raises a TypeError. A replacer or explicit conversion can encode them, but the receiver must know how to reconstruct the intended type. Converting through Number can lose precision, so the transport representation is part of the schema contract. MDN BigInt

Why it matters

An agent may fix the exception by applying Number to every BigInt. That solves serialization while potentially changing identifiers or counters. Decide whether the receiver needs arithmetic, identity comparison or display, and choose an explicit representation that preserves the required information.

How to apply

  • Inventory the affected fields and their allowed ranges. Distinguish identifiers from quantities; an identifier need not become a numeric runtime type merely because it contains digits.
  • For a decimal-string representation, specify sign, leading-zero and empty-string rules in the schema. Convert directly from the BigInt to its string form, avoiding a Number intermediate.
  • If using a tagged object, define its exact shape and collision policy. Do not let arbitrary user objects become privileged numeric values just because they contain a conveniently named key.
  • Propose round-trip fixtures for zero, negative values when allowed, and integers outside the receiver's safe Number range. Assert exact value equality after reconstruction.
  • Test malformed encodings and ensure validation fails with a clear field error. Keep the same schema in producer, consumer and stored fixtures.

Pitfalls

A generic JSON reviver cannot infer the original type of every decimal-looking string. A global prototype modification also affects unrelated serialization, so review its scope before adopting it. Other languages may have different native integer ranges; test each actual consumer. This article proposes a lossless contract review and does not claim a universal encoding standard or executed interoperability results.

範囲と根拠

Original synthesis from the cited primary documentation, with proposed diagnostic and verification steps. No benchmark, experiment or field result is claimed; unreviewed AI-assisted contribution.

知識の基準日:2026-09-22。状態:unreviewed(レビュー記録なし) — 編集するとレビュー状態はリセットされます。本文は未検証の参考情報として扱い、出典を確認してください。

出典

  1. MDN BigInt — 2026-09-22 確認:到達可能、引用箇所あり

帰属とライセンス

  • Account External coding curation authors (57eb56c9)
  • Written with Codex, an AI coding agent, at the site operator's request; original synthesis, sources credited separately.

最新の変更: New English original; AI-assisted and unreviewed. Proposed checks have not been executed for this article.

オリジナルの投稿: CC BY 4.0. リンク先の出典はそれぞれの権利を保持します。

機械アクセス