Teleport JavaScript Object (De)serialization
Teleport JavaScript is a specialized utility for the deep serialization and deserialization of JavaScript objects, extending far beyond the capabilities of the native `JSON.stringify` and `JSON.parse` methods. Currently stable at version 1.0.0, this library provides robust handling for a wide array of data types including `Date`, `BigInt`, `RegExp`, `Symbol`, `Set`, `Map`, `Buffer`, `undefined`, and all Typed Arrays (e.g., `Int8Array`, `Float64Array`). A key differentiator is its ability to correctly manage circular references within objects, preventing common serialization errors. While a specific release cadence isn't published, its current version suggests a focus on stability for its comprehensive feature set. It positions itself as a 'super light and fast' alternative for scenarios where standard JSON falls short, making it suitable for inter-process communication, storage, or deep cloning of complex JavaScript data structures. It prioritizes covering edge cases and modern JavaScript types for complete object graph preservation.
Common errors
-
ReferenceError: require is not defined in ES module scope
cause Attempting to use CommonJS `require()` syntax within an ES Module (type: 'module' in package.json or .mjs file).fixChange `const {parse, stringify} = require('teleport-javascript');` to `import { parse, stringify } from 'teleport-javascript';` -
TypeError: stringify is not a function
cause Attempting to call `stringify()` or `parse()` directly after a default import, or if the named imports were missed in destructuring.fixEnsure you are using named imports: `import { parse, stringify } from 'teleport-javascript';` or `const { parse, stringify } = require('teleport-javascript');` -
TypeError: Cannot convert a BigInt value to a JSON string
cause Trying to serialize an object containing a `BigInt` using the native `JSON.stringify()` method, which does not support `BigInt`s by default.fixUse `teleport-javascript`'s `stringify` function instead: `const stringified = stringify(objWithBigInt);`
Warnings
- gotcha When a `Symbol` is serialized and then deserialized, `teleport-javascript` creates a *new* `Symbol` with the same description, rather than preserving the original `Symbol` reference. This is standard behavior for serialization, but important to note if strict reference equality for `Symbol`s is expected.
- gotcha As with any custom deserialization library, parsing stringified data from untrusted sources carries inherent security risks. While `teleport-javascript` reconstructs objects without executing arbitrary code, specially crafted malicious input could potentially lead to unexpected object structures, resource exhaustion, or other application-specific vulnerabilities.
- gotcha `teleport-javascript` extends `JSON.stringify`'s capabilities by supporting types like `undefined`, `BigInt`, and `Set` which `JSON.stringify` either omits or throws on. Users expecting strict JSON-compliant output or the default `JSON.stringify` behavior (e.g., omitting `undefined` properties) should be aware of these differences.
Install
-
npm install teleport-javascript -
yarn add teleport-javascript -
pnpm add teleport-javascript
Imports
- parse, stringify
const {parse, stringify} = require('teleport-javascript');import { parse, stringify } from 'teleport-javascript'; - parse, stringify
import { parse, stringify } from 'teleport-javascript';const { parse, stringify } = require('teleport-javascript'); - teleport
import teleport from 'teleport-javascript';
import * as teleport from 'teleport-javascript';
Quickstart
import { parse, stringify } from 'teleport-javascript';
const obj = {
key: 'value',
undefined: undefined,
regex: /a-z/gi,
set: new Set([-Infinity, NaN, Infinity]),
bigint: 900719925474099123n,
symbol: Symbol('key'),
date: new Date()
};
obj.circular = obj; // Add a circular reference
console.log('Original object:', obj);
const stringified = stringify(obj);
console.log('\nStringified output:', stringified);
const parsed = parse(stringified);
console.log('\nParsed object:', parsed);
// Verify circular reference is re-established
console.log('Are circular references restored?', parsed.circular === parsed);
// Verify BigInt and Date types
console.log('BigInt type preserved?', typeof parsed.bigint === 'bigint');
console.log('Date type preserved?', parsed.date instanceof Date);