Trending

World technology news

→
Preserving long order IDs in JSON: RFC 8259 and numeric precision
Software Engineering · 2017
Software Engineering2017

Preserving long order IDs in JSON: RFC 8259 and numeric precision

A runnable Node.js example shows how numeric rounding can merge two distinct order identifiers, and how to carry opaque decimal IDs as strings across an API.

Archive retrospective · Topic year 2017

Developer's JournalPublished: Dec 4, 20266 min read
Reading progress0%
In this article
  1. When two orders become one row
  2. What the 2017 specification establishes
  3. Reproduce the loss and the repair
  4. Roll out a string identifier contract
  5. Choose the right ordering explicitly
  6. Repair previously rounded data
  7. Acceptance checks across the pipeline

When two orders become one row

A warehouse API sends two order IDs. The first is 9007199254740992 and the second is 9007199254740993. The warehouse database treats them as distinct identifiers. A browser can parse the numeric values into the same JavaScript number, then collapse them into one entry when it builds a map. That might remove one row from the screen, suppress an update, or direct a return to the wrong order. The useful first step is to compare the original HTTP body with the values the parser produces. If the body already contains the wrong digits, trace the producer. If the digits change only after parsing, the receiving boundary needs a different representation. An order number used for equality and lookup can have a different wire type from a quantity used in arithmetic.

What the 2017 specification establishes

Section 6 of RFC 8259 describes the expected interoperability of integers in the range from negative 2 to the power of 53 plus one through positive 2 to the power of 53 minus one when implementations use binary64 arithmetic. The high endpoint is 9007199254740991. The document describes JSON syntax separately from an implementation's limits on precision and range. A JSON token may be syntactically valid and still become a different value in a particular runtime. Some larger integers can be represented exactly. The spacing between representable values, however, means that adjacent integers are no longer guaranteed to remain distinct. The range is therefore a useful boundary for an API contract, even when test data happens to contain a few larger values that round trip.

Service architectureConceptual sketch · summary of the article's technical flow

Reproduce the loss and the repair

Save the example as verify-json-identifiers.mjs and run node verify-json-identifiers.mjs. In a local run on Node.js 24.19.0, parsing 9007199254740993 as a JSON number produced 9007199254740992. Re-encoding the parsed object preserved the rounded value rather than the original digits. Parsing the quoted version retained all sixteen digits and a set built from the two string identifiers retained two entries. The BigInt assertion demonstrates that the validated string can be converted locally when arithmetic is required. Its use is optional for an opaque order ID. Calling String on a number after it has rounded cannot restore the original value. The test uses a modern Node runtime to demonstrate behavior. December 2017 is the publication date of the interchange specification, not the version date of this test runtime.

import assert from 'node:assert/strict';
const source = '{"id":9007199254740993}';
assert.equal(JSON.parse(source).id, 9007199254740992);
assert.equal(JSON.stringify(JSON.parse(source)), '{"id":9007199254740992}');
const inbound = JSON.parse('{"id":"9007199254740993"}');
assert.match(inbound.id, /^(0|[1-9][0-9]*)$/u);
assert.equal(BigInt(inbound.id), 9007199254740993n);
assert.equal(JSON.stringify(inbound), '{"id":"9007199254740993"}');
assert.equal(new Set(['9007199254740992', inbound.id]).size, 2);
console.log(process.version, JSON.stringify({numeric: JSON.parse(source).id, preserved: inbound.id, distinctIds: 2}));

Roll out a string identifier contract

Define the wire field as a decimal string and document its accepted shape. The sample disallows signs, whitespace, fractions and leading zeros except for the value zero. An existing ID system can choose different rules, especially if leading zeros are significant. Introduce a versioned field if existing consumers require a number. Generate the string from the original exact value at the producer. Update join keys, browser list keys, logs and write requests to read the same field. State which field is authoritative during the transition and what happens when both are present but disagree. Inspect the database client as well. A database column can store an exact integer while a driver converts it to a JavaScript number before the HTTP encoder sees it. An end to end test should travel through that boundary.

Choose the right ordering explicitly

String equality preserves identifier distinctions. Lexical sorting can place 10 before 2, which is often immaterial for an opaque ID. If the product needs chronological display, sort by an event time instead of deriving order from an identifier. For a numeric ordering requirement, compare the lengths and digits of validated unsigned decimal strings or convert them to a suitable exact integer type for local comparison. Preserve the original string for serialization. Plain JSON.stringify does not serialize a bare BigInt as a JSON number. A string comparison also avoids silently stripping leading zeros when the format uses them. Keep comparison, storage and serialization rules explicit so another service does not undo the API contract by coercion.

Repair previously rounded data

Changing the contract prevents a new loss of precision, but it cannot reveal a digit that has already been lost. Compare affected records against the original order service or a retained raw delivery log. Two orders with a rounded collision should not be distinguished provisionally by a display name or a time that might have been shared. Record which authoritative value resolved the discrepancy, and keep unresolved rows out of an automatic correction batch. That makes a replay of the correction predictable.

Acceptance checks across the pipeline

Exercise the upper safe integer, the next integer and its neighbor in the same test fixture. Send two adjacent out of range IDs through the real producer, database driver and HTTP handler. Assert that the consumer renders two rows, puts the correct key on each action and returns the exact digits on an update request. Test invalid shapes and record how long earlier clients will be supported. If the numeric field has already damaged stored values, recover the identifier from the authoritative producer. The result shown here verifies the example with a modern Node.js parser. Run the same boundary cases in any additional runtime or connector used in production. The acceptance criterion is that two records remain distinguishable from the first stored value through the last consumer action.

370 reads ✎ Write a comment

Reader discussion

Discussion prompt

What would you change first for maintainability and testing in your own project?

No comments yet. Be the first to contribute.

Write a comment

Comments are real visitor contributions. Repeated, link-heavy or abusive submissions may be limited.