@nopu/bigint-json

A JSON implementation that supports built-in object BigInt.

Usage no npm install needed!

<script type="module">
  import nopuBigintJson from 'https://cdn.skypack.dev/@nopu/bigint-json';
</script>

README

!! This package has been deprecated, please do not continue to use it! !!

BigInt-JSON @Nopu (Deprecated)

A JSON implementation that supports built-in object BigInt.

Install

You can install it from npm.

npm i @nopu/bigint-json

Use

import BigIntJSON from '@nopu/bigint-json';

/**
 * Converts a JavaScript Object Notation (JSON) string into an object.
 * @param {string} string Provides the string to be converted.
 * @param {Object} config
 * +default+: {hasBigInt:true,bigIntOutputType:'bigint'};
 * [hasBigInt]: +default+: true; Indicates whether BigInt exists in data to
 * be converted, if so, additional processing will be performed;
 * [bigIntOutputType]: +default+: 'bigint'; Indicates the actual type of BigInt
 * value in the origin string after being parsed.
 * @param {boolean} [config.hasBigInt]
 * @param {'bigint'|'string'} [config.bigIntOutputType]
 * @returns {Object}
 * @throws {SyntaxError|Error}
 * Throws a SyntaxError exception if the string to parse is not a valid
 * JavaScript Object Notation (JSON) string; Throws an Error exception if this
 * function itself has an unknown bug.
 */

BigIntJSON.parse('{ "bigint": 12345678987654321 }');
// Output: { bigint: 12345678987654321n }

/**
 * Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
 * @param {Object} object Provides the object to be converted.
 * @param {Object} config
 * +default+: {hasBigInt:true,bigIntInputType:'bigint'};
 * [hasBigInt]: +default+: true; Indicates whether BigInt exists in data to be
 * converted, if so, additional processing will be performed;
 * [bigIntInputType]: +default+: 'bigint'; Indicates the actual type of BigInt
 * value in the origin object after being parsed.
 * @param {boolean} [config.hasBigInt]
 * @param {'bigint'|'string'} [config.bigIntInputType]
 * @returns {string}
 * @throws {TypeError}
 * Throws a TypeError ("cyclic object value") exception when a circular
 * reference is found or a TypeError ("BigInt value can't be serialized in
 * JSON") when trying to stringify a BigInt value but "hasBigInt" is set as
 * "false".
 */

BigIntJSON.stringify({ bigint: 12345678987654321n });
// Output: '{"bigint":12345678987654321}'

If you are using Visual Studio Code, then after importing this package, you will get enough instructions from the editor when use this JSON.

Test

npm run test

This test will show you the difference between the built-in JSON at runtime and this JSON, and how this JSON performs differently when different arguments are specified.

Support

Runtime that supports the keyword class and the built-in object BigInt are required for this package to work fully properly.

For their compatibility, you can refer to this.

class - JavaScript | MDN

BigInt - JavaScript | MDN

FAQ

Why did you develop this package?

Because I need a JSON that supports the runtime built-in object BigInt.

How does this JSON perform compared to the built-in JSON of runtime?

Since the algorithm implementation of this JSON is not heavily optimized, the performance of this JSON will be much worse than the runtime built-in JSON.

Can I use this package if I am using a runtime that does not support the built-in object BigInt at all?

If you just want to convert a JavaScript Object Notation (JSON) string into a JavaScript value, you can export values that are originally BigInt type as string type by using this package.

If you have other needs, perhaps you can refer to this.

JSON.stringify() - JavaScript | MDN

Is there anything else I should be aware of when using this package?

Since the code in this package uses the syntax and APIs of the newer ES standards, and it is not precompiled and precompressed, if you have compatibility requirements for your project, you will need to set the relevant entries in your project to ensure that this package is compiled and compressed like any other code in your project.

License

MIT License