@ton.js/json-parse-polyfill
TypeScript icon, indicating that this package has built-in type declarations

0.0.0-beta.1 • Public • Published

Type definitions License
npm (scoped with tag) node-lts (scoped with tag) npm bundle size (scoped)
GitHub issues GitHub pull requests GitHub last commit


@ton.js/json-parser

A customizable JSON parser that is 100% compatible with native implementation (JSON.parse()), can be used as a polyfill, adds TC39 source access proposal and additional features, have better security like secure-json-parse and is… 25 times slower…

Rationale

The native implementation of JSON parser in JavaScript (i.e. JSON.parse()) doesn't allow to fully customize the parsing behavior. The JSON specification allows documents to include numbers of arbitrary and unlimited size. However, EcmaScript is using IEEE 754 standard to represent numbers internally and doesn't support numbers of the unlimited size. This leads to the data loss when JSON.parse() is attempting to read big numbers from the JSON documents.

Modern versions of JavaScript support a special BigInt data type specifically designed to represent integer numbers of unlimited size, but there is no way to tell JSON.parse() to use BigInt for parsing numbers.

Web 3.0 community tends to use very big numbers to represent cryptocurrency values, like the number of nanocoins in transaction. This library was designed as a workaround that allows to read big integer numbers from the JSON documents. However, considering that it's a full-fledged customized JSON parser you can use it for other cases as well.

Contents

Features

  • modern cross-platform multi-format package that can be used in any JavaScript environment,

  • written in pure super-strict TypeScript with 100% type coverage,

  • minimum size package with zero dependencies,

  • robust security practices,

  • 100% compatible with the JSON standard and native JSON.parse() implementation, can be used as a polyfill,

  • future-compatible by implementing the Stage-3 TC39 source access proposal and additional features,

  • adds special handling for __proto__ and constructor.prototype object properties to implement better security,

  • extensively covered by unit tests and tested out on multiple real-life JSON samples,

  • parses 1 MB of nested JSON in ~40 ms. (25 times slower than native implementation).

Normal usage (ponyfill)

Ponyfill is a side effects free package that provides an alternative implementation and doesn't affect the native behavior of the system.

Install the package first:

npm install --save @ton.js/json-parser

Please see the examples project for the complete examples.

Simple parsing

import { parseJson } from '@ton.js/json-parser';

interface DocumentType {
 // …
}

const object = parseJson<DocumentType>('{ … }');
// object type will be: DocumentType

Using native reviver

interface DocumentType {
  birthDate: Date;
}

const content = '{ "birthDate": "1989-08-16T10:20:30.123Z" }';

const object = parseJson<DocumentType>(content, (key, value) => (
  (key.endsWith('Date') ? new Date(value) : value)
));

assert(object.birthDate instanceof Date);

Using reviver with source text access

interface DocumentType {
  valueBN: bigint;
}

const content = '{ "valueBN": 12345678901234567890 }';

const object = parseJson<DocumentType>(content, (key, value, context) => (
  (key.endsWith('BN') ? BigInt(context.source) : value)
));

assert.equal(typeof object.valueBN, 'bigint');

Using key path

interface DocumentType {
  foo: {
    bar: {
      value: bigint;
    };
  };
}

const content = '{ "foo": { "bar": { "value": 12345678901234567890 } } }';

const object = parseJson<DocumentType>(content, (key, value, context) => (
  (context.keys.join('.') === 'foo.bar.value' ? BigInt(context.source) : value)
));

assert.equal(typeof object.foo.bar.value, 'bigint');

Throwing on prototypes

In order to prevent parsed object prototype override the JSON parser will automatically skip __proto__ and constructor.prototype properties. However, you can use the throwOnProto: true option to make this behavior more explicit — the parser will throw an error instead:

const content = '{ "foo": true, "__proto__": {} }';

try {
  parseJson(content, undefined, {
    throwOnProto: true,
  });

} catch (error: any) {
  // Forbidden object property name: "__proto__"
  console.log(error);

}

Polyfill usage

The polyfill implements the Stage-3 TC39 source access proposal and adds some additional useful features.

Polyfill is a package that when globally imported, overrides the behavior of the native JSON.parse() method. The polyfill will detect the number of arguments that your reviver function has and will use custom JSON parser implementation only when it has three parameters (i.e. context), in other case the native implementation will be used instead.

Please see the polyfill examples project for the complete examples.

Be advised, that polyfill can't detect if you are using arguments[2] to access the context, so make sure to use a normal function parameter.

Prerequisites

1). Install the polyfill package:

npm install --save @ton.js/json-parse-polyfill

2). Import the package only once as close to the beginning of your program as possible:

import '@ton.js/json-parse-polyfill';

(function main() {
  // …
})();

Simple parsing

interface DocumentType {
 // …
}

// Native implementation will be used
const object = <DocumentType> JSON.parse('{ … }');

Using native reviver

interface DocumentType {
  birthDate: Date;
}

const content = '{ "birthDate": "1989-08-16T10:20:30.123Z" }';

// Native implementation will be used
const object = <DocumentType> JSON.parse(content, (key, value) => (
  (key.endsWith('Date') ? new Date(value) : value)
));

assert(object.birthDate instanceof Date);

Using reviver with source text access

import type { ReviverFunc } from '@ton.js/json-parse-polyfill';

interface DocumentType {
  valueBN: bigint;
}

const content = '{ "valueBN": 12345678901234567890 }';

// Custom implementation will be used
const reviver: ReviverFunc = (key, value, context) => (
  (key.endsWith('BN') ? BigInt(context.source) : value)
);

const object = <DocumentType> (
  JSON.parse(content, reviver as any)
);

assert.equal(typeof object.valueBN, 'bigint');

Using key path

import type { ReviverFunc } from '@ton.js/json-parse-polyfill';

interface DocumentType {
  foo: {
    bar: {
      value: bigint;
    };
  };
}

const content = '{ "foo": { "bar": { "value": 12345678901234567890 } } }';

const reviver: ReviverFunc = (key, value, context) => (
  ((context.keys.join('.') === 'foo.bar.value')
    ? BigInt(context.source)
    : value
  )
);

// Custom implementation will be used
const object = <DocumentType> (
  JSON.parse(content, reviver as any)
);

assert.equal(typeof object.foo.bar.value, 'bigint');

Benchmarks

Normal dataset

name ops margin percentSlower
native 12279 0.34 0
parse-json 444 1.57 96.38

Big dataset

name ops margin percentSlower
native 31 0.52 0
parse-json 3 2.65 90.32

Security

Please see our security policy.

Contributing

Want to help? Please see the contributing guide.

Support

If you have any questions regarding this library or TON development in general — feel free to join our official TON development Telegram group.

The MIT License (MIT)

Copyright © 2023 TON FOUNDATION

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Package Sidebar

Install

npm i @ton.js/json-parse-polyfill

Weekly Downloads

3

Version

0.0.0-beta.1

License

MIT

Unpacked Size

71.2 kB

Total Files

12

Last publish

Collaborators

  • sfomin