Universal Serialize and Deserialize

1.0.10 · abandoned · verified Sun Apr 19

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

Warnings

Install

Imports

Quickstart

This example demonstrates basic serialization and deserialization of an object containing strings, dates, errors, and regular expressions, showing how `universal-serialize` preserves their types.

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

view raw JSON →