JSPM

@puttest.io/put-json-bigint

0.0.1
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • 0
  • Score
    100M100P100Q13539F
  • License MIT

JSON.parse with bigints support

Package Exports

  • @puttest.io/put-json-bigint
  • @puttest.io/put-json-bigint/dist/index.js

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@puttest.io/put-json-bigint) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

json-bigint-patch

NPM

JSON.parse/stringify with bigints support. Based on Douglas Crockford JSON.js package and BigInt feature of JavaScript.

While most JSON parsers assume numeric values have same precision restrictions as IEEE 754 double, JSON specification does not say anything about number precision. Any floating point number in decimal (optionally scientific) notation is valid JSON value. It's a good idea to serialize values which might fall out of IEEE 754 integer precision as strings in your JSON api, but { "value" : 9223372036854775807}, for example, is still a valid RFC4627 JSON string, and in most JS runtimes the result of JSON.parse is this object: { value: 9223372036854776000 }

==========

example:

var json = '{ "value" : 9223372036854775807, "v2": 123 }';
console.log('Input:', json);
console.log('');

console.log('JavaScript built-in JSON:')
var r = JSON.parse(json);
console.log('Native JSON.parse(input).value : ', r.value.toString());
console.log('Native JSON.stringify(JSON.parse(input)):', JSON.stringify(r));

require('json-bigint-patch');

console.log('\n\nPatched JSON:');
var r1 = JSON.parse(json);
console.log('Patched JSON.parse(input).value : ', r1.value.toString());
console.log('Patched JSON.stringify(JSON.parse(input)):', JSON.stringify(r1));

Output:

Input: { "value" : 9223372036854775807, "v2": 123 }

JavaScript built-in JSON:
Native JSON.parse(input).value :  9223372036854776000
Native JSON.stringify(JSON.parse(input)): {"value":9223372036854776000,"v2":123}


Patched JSON:
Patched JSON.parse(input).value :  9223372036854775807
Patched JSON.stringify(JSON.parse(input)): {"value":9223372036854775807,"v2":123}