{
  "id": 22287451,
  "title": "Serialize BigInt Stock Quantities With an Explicit JSON Contract",
  "url": "https://urgent.news/2026/11/28/serialize-bigint-stock-quantities-with-an-explicit-json-contract",
  "topic": "finance",
  "section": "Finance & Markets",
  "published": "2026-11-28T02:35:11.000Z",
  "source": {
    "name": "Dev.to",
    "slug": "dev-to",
    "url": "https://dev.to/tom_8d160ea57bf5c0395981d/serialize-bigint-stock-quantities-with-an-explicit-json-contract-1847"
  },
  "original_language": "en",
  "account": "When dealing with large integer quantities, such as those used in stock calculations, it is important to define an explicit JSON contract to serialize the BigInt values. Converting every value to a Number can result in a loss of precision, as demonstrated by the TypeError thrown when attempting to stringify a BigInt value. The provided JavaScript code defines a small demonstration contract that ensures the internal units field is a nonnegative bigint and represents it as a decimal string in the outgoing payload. This contract can be utilized by a Shopify 3PL adapter, but agreement must be reached with the receiving system before implementation. The code includes various checks to ensure the integrity of the input data, throwing TypeError exceptions when invalid data is encountered. When decoding the serialized JSON payload, the receiver must validate the decimal string representation of the units field and decode it accordingly. It is crucial for a drop shipping supplier using an unrelated schema to have its own adapter to avoid conflicts with the agreed representation. The provided unit tests confirm the expected behavior of the encodeStock function and demonstrate that the original bigint values are preserved during the serialization process.",
  "summary": "JSON.stringify({ units: 1n }) throws a TypeError . If a stock calculation deliberately uses BigInt , define its outgoing representation before sending the result to another service. Converting every value to Number can lose the exact integer that the calculation was intended to preserve. MDN explains the serialization error and conversion options . This AI-written learning example is prepared for…",
  "summary_source": "publisher",
  "key_points": [
    "Define explicit JSON contract for BigInt serialization",
    "Ensure nonnegative bigint units in outgoing payload",
    "Receiver validates and decodes decimal string representation"
  ],
  "editors_take": "Implementing explicit JSON contracts for BigInt values prevents precision loss during stock calculations and requires synchronized data standards between suppliers and receiving systems to ensure seamless integration.",
  "opinion": false,
  "illustration": null,
  "coverage": {
    "outlets": 1,
    "also_reported_by": []
  },
  "machine_translated": false,
  "ai_generated": true,
  "disclaimer": "Accounts, briefs, key points, the editor’s take and translations are written by software from other outlets’ reporting and may contain errors. A summary whose summary_source is \"publisher\" is the outlet’s own feed text. Always check the linked original."
}