ESSerializer

1.3.11 · active · verified Tue Apr 21

ESSerializer is a JavaScript serialization and deserialization utility. It enables the conversion of JavaScript class instances into JSON format and their subsequent reconstruction back into fully functional objects, ensuring that all Class, Property, and Method information is retained recursively. The library operates without relying on `eval()`, which eliminates a common security vulnerability associated with many serialization methods. ESSerializer supports a broad spectrum of built-in JavaScript classes and types, including various Array types, Date, RegExp, BigInt, and all standard Error classes. It is designed to work seamlessly in both browser and Node.js environments. The current stable version, 1.3.11, receives ongoing maintenance with recent releases focusing on minor bug fixes and security vulnerability patches, indicating an active development and stable release cadence. Its key differentiators include robust recursive class retention, automatic class definition detection during serialization and deserialization, comprehensive support for getter/setter properties and `instanceof` checks, and compatibility with both modern ES6 classes and older function-style class definitions.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates the serialization and deserialization of nested class instances, verifying method and type retention.

import ESSerializer from 'esserializer';

class ClassC {
  constructor(public value: number) {}
  getDoubled(): number { return this.value * 2; }
}

class ClassB {
  constructor(public name: string, public cInstance: ClassC) {}
  greet(): string { return `Hello from ${this.name}! C's doubled value: ${this.cInstance.getDoubled()}`; }
}

class ClassA {
  private id: string;
  public date: Date;
  public bInstance: ClassB;
  public myArray: number[];

  constructor(id: string) {
    this.id = id;
    this.date = new Date();
    this.bInstance = new ClassB('Nested B', new ClassC(10));
    this.myArray = [1, 2, 3];
  }

  getId(): string { return this.id; }
  getDetails(): string {
    return `ID: ${this.id}, Date: ${this.date.toISOString()}, B's greeting: ${this.bInstance.greet()}, Array: ${this.myArray.join(',')}`;
  }
}

const originalObj = new ClassA('unique-123');
console.log('Original object details:', originalObj.getDetails());
console.log('Original object B instance greet:', originalObj.bInstance.greet());

// Serialize the object
const serializedString = ESSerializer.serialize(originalObj);
console.log('Serialized string (truncated):', serializedString.substring(0, 100) + '...');

// Deserialize the object, providing custom classes
const deserializedObj = ESSerializer.deserialize(serializedString, [ClassA, ClassB, ClassC]);

console.log('Deserialized object details:', deserializedObj.getDetails());
console.log('Deserialized object B instance greet:', deserializedObj.bInstance.greet());

// Verify instance types and methods
console.assert(deserializedObj instanceof ClassA, 'Deserialized object is not an instance of ClassA');
console.assert(deserializedObj.bInstance instanceof ClassB, 'Nested object is not an instance of ClassB');
console.assert(deserializedObj.bInstance.cInstance instanceof ClassC, 'Deeply nested object is not an instance of ClassC');
console.assert(typeof deserializedObj.getId === 'function', 'getId method missing');
console.assert(deserializedObj.getId() === 'unique-123', 'ID mismatch');
console.assert(deserializedObj.date instanceof Date, 'Date object not retained');

console.log('Serialization and deserialization successful!');

view raw JSON →