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.