Universal Serialize and Deserialize
universal-serialize is a JavaScript utility for serializing and deserializing complex objects, including built-in types like `Date`, `Error`, and `RegExp` that standard `JSON.stringify`/`parse` do not handle natively. It also provides a mechanism for developers to define custom serialization and deserialization logic for any bespoke type. The package is currently at version 1.0.10, with its last known update several years ago, indicating it is likely in an abandoned state and not actively maintained or receiving new features/security patches. Its key differentiators include out-of-the-box support for common non-primitive types and an extensible API for custom types, making it more robust than simple JSON operations for certain use cases.
Common errors
-
SyntaxError: Unexpected token 'u' in JSON at position X
cause Attempting to deserialize an invalid JSON string, or a string that is not valid JSON produced by `universal-serialize`.fixEnsure the input to `deserialize()` is a valid JSON string. Check that the string was correctly produced by `serialize()` and not corrupted or truncated. Use a JSON linter to validate the string if necessary. -
TypeError: Cannot read properties of undefined (reading 'stack')
cause An `Error` object was serialized, but its prototype chain or properties were not fully restored during deserialization, leading to a plain object that lacks `Error` methods.fixWhile `universal-serialize` claims to handle `Error` objects, ensure that the deserialized `error` property is correctly re-instantiated as an `Error` object. If custom serialization was used, verify the `deserialize` handler correctly reconstructs the `Error` instance. -
ReferenceError: TYPE is not defined
cause `TYPE` constant was used in a custom serialization/deserialization handler without being imported.fixAdd `import { TYPE } from 'universal-serialize';` to the top of your file where `TYPE` is used.
Warnings
- breaking The package appears to be abandoned with its last release over seven years ago. There will be no further updates, bug fixes, or security patches, which could lead to compatibility issues with newer JavaScript features or runtime environments.
- gotcha Using `eval()` in custom deserialization handlers (as shown in the README example for functions) is a significant security risk. Maliciously crafted serialized data could execute arbitrary code on the deserializing system.
- gotcha The library does not explicitly mention handling circular references. Serializing objects with circular structures using this library (or any serializer without specific handling) can lead to infinite loops or stack overflows during serialization.
- gotcha Lack of support for newer JavaScript types such as `BigInt`, `Symbol`, `Map`, `Set`, `Promise`, `WeakMap`, `WeakSet`, or custom classes beyond the explicitly defined custom types. These will likely not be serialized or deserialized correctly by default.
Install
-
npm install universal-serialize -
yarn add universal-serialize -
pnpm add universal-serialize
Imports
- serialize
const serialize = require('universal-serialize').serialize;import { serialize } from 'universal-serialize'; - deserialize
const deserialize = require('universal-serialize').deserialize;import { deserialize } from 'universal-serialize'; - TYPE
const TYPE = require('universal-serialize').TYPE;import { TYPE } from 'universal-serialize';
Quickstart
import { serialize, deserialize } from 'universal-serialize';
// Define a complex object with built-in non-primitive types
const originalObject = {
foo: 'bar',
date: new Date('2023-01-01T12:00:00.000Z'),
error: new Error('Something went wrong'),
regex: /test/gi
};
// Serialize the complex object into a JSON string
const jsonString = serialize(originalObject);
console.log('Serialized JSON:', jsonString);
// Deserialize the JSON string back into an object
const deserializedObject = deserialize(jsonString);
// Verify the types and values of the deserialized objects
console.log('Deserialized foo:', deserializedObject.foo);
console.log('Deserialized date (instanceof Date):', deserializedObject.date instanceof Date);
console.log('Deserialized date (value):', deserializedObject.date.toISOString());
console.log('Deserialized error (instanceof Error):', deserializedObject.error instanceof Error);
console.log('Deserialized error (message):', deserializedObject.error.message);
console.log('Deserialized regex (instanceof RegExp):', deserializedObject.regex instanceof RegExp);
console.log('Deserialized regex (value):', deserializedObject.regex.source);