Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@j-o-r/jsonext

j-o-r43Apache-2.01.0.1TypeScript support: included

Extended JSON, keep typed arrays in their orginal form, backwards compatible with original JSON

JSON, typed arrays, preserve

readme

JSONEXT

JSONEXT is an extension to JSON that preserves the data type of typed arrays and BigInts.

Installation

npm i @j-o-r/jsonext --save

Usage

Stringify

To stringify an object with typed arrays:

import JSONEXT from '@j-o-r/jsonext'
const typedArray = new Float32Array([1.1, 2.2, 3.3]);
const jsonString = JSONEXT.stringify({ array: typedArray });
console.log(jsonString); // Output: {"array":{"_dT_":"f32a","_dA_":[1.1,2.2,3.3]}}

Parse

To parse a JSON string back into its original form:

const parsedObject = JSONEXT.parse(jsonString);
console.log(parsedObject.array instanceof Float32Array); // true

Features

  • Typed Arrays: Preserves the type of arrays like Float32Array, Int8Array, etc.
  • BigInt: Converts BigInt to string representation for JSON compatibility.
  • Compatibility: Works with standard JSON methods but extends functionality.

Limitations and Notes:

  • Ensure that the _dT_ and _dA_ keys do not conflict with your data keys.
  • Performance might be impacted for large objects due to type checking.
  • Default handling for handling known types in parsing.

License

This project is licensed under the APACHE 2.0 License. See the LICENSE file for details.