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

0.0.0-beta.0 • 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()) adds TC39 source access proposal have better security like secure-json-parse and… 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,

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

  • 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).

Install

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

Usage

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

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

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

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');

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.

Readme

Keywords

Package Sidebar

Install

npm i @ton.js/json-parser

Weekly Downloads

6

Version

0.0.0-beta.0

License

MIT

Unpacked Size

65.2 kB

Total Files

12

Last publish

Collaborators

  • sfomin