rawJSON Preserves Exact Number Text

JSON.rawJSON embeds the exact digit text into JSON output, keeping 9007199254740993 and 1.00 intact where regular numbers round and normalize.

JavascriptData Integrity

Code

javascriptsnippet
21 lines
// JSON.rawJSON stores the exact digit text instead of a rounded float.
const ledger = {
  id: JSON.rawJSON('9007199254740993'),
  balance: JSON.rawJSON('0.1'),
};

console.log('rawJSON    :', JSON.stringify(ledger));

// A regular number is silently rounded past Number.MAX_SAFE_INTEGER
const plain = JSON.stringify({ id: 9007199254740993, balance: 0.1 });
console.log('plain value:', plain);

// The reviver's third argument carries the original source text
const back = JSON.parse(JSON.stringify(ledger), (key, value, context) =>
  key === 'id' && typeof context?.source === 'string' ? context.source : value,
);

console.log('round trip :', back.id, typeof back.id);

// Trailing zeros survive too: 1.0 and 1.00 stay distinct texts
console.log('zeros kept  :', JSON.stringify([JSON.rawJSON('1.0'), JSON.rawJSON('1.00')]));

Output

Captured from a sandboxed run of this snippet in the build pipeline.

rawJSON    : {"id":9007199254740993,"balance":0.1}
plain value: {"id":9007199254740992,"balance":0.1}
round trip : 9007199254740993 string
zeros kept  : [1.0,1.00]

How it works

Top-level bindings ledger, plain, back hold the demo state — nothing sits inside a function scope.

What to watch for

  • Nothing to vet in a supply-chain review — the file pulls in no packages.

Applying it

The demo output is labeled (rawJSON :, plain value:, round trip :), so every line of output can be traced to the operation that made it.

Statements that matter

const back = JSON.parse(JSON.stringify(ledger), (key, value, context) =>
  key === 'id' && typeof context?.source === 'string' ? context.source : value,
);
const plain = JSON.stringify({ id: 9007199254740993, balance: 0.1 });

Interactive Tools for This Topic

FAQ

Will the snippet run without installing packages?
Yes, it runs as-is — nothing outside the built-in APIs is referenced. Its state lives in module-level bindings: `ledger`, `plain`, `back`.