npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

serde-ts

v1.0.2

Published

A TypeScript library for serialization and deserialization.

Downloads

225

Readme

SerDe: Serialization and Deserialization Library

SerDe is a powerful TypeScript library designed to serialize and deserialize complex, nested data structures, while ensuring that the integrity of your objects is maintained throughout the process. This library supports the serialization of custom classes, circular references, and nested objects, and it ensures that during deserialization, existing objects are not redundantly recreated—thus preserving the uniqueness of instances.

Key Features

  • Preserves Object Uniqueness: During deserialization, SerDe ensures that objects that were serialized and referenced multiple times are not recreated multiple times. This preserves the uniqueness of instances, meaning if two references in your data structure pointed to the same object before serialization, they will continue to do so after deserialization.

  • Custom Class Serialization: Supports serialization and deserialization of user-defined classes. To utilize this feature, classes need to be registered with SerDe to ensure they can be correctly reconstructed during deserialization.

  • Circular Reference Handling: Efficiently handles circular references in data structures, ensuring that complex graphs of interconnected objects can be serialized and deserialized without issues.

  • Support for Initialization Functions: Classes can include an initFn (default name: _initFn) method that will be automatically invoked during deserialization, allowing for custom initialization logic to be executed when an object is reconstructed.

Installation

You can install SerDe via npm:

npm install serde-ts

Usage

Registering Classes

Before you can serialize and deserialize custom classes, you need to register them with SerDe. This is necessary to allow SerDe to know how to properly reconstruct instances of these classes during deserialization.

import { SerDe } from './path-to-serde';
import { MyClass } from './path-to-myclass';

SerDe.classRegistration([MyClass]);

Serialization Example

const myObject = new MyClass('Example', 123);
const serialized = SerDe.serialise(myObject);
console.log(JSON.stringify(serialized, null, 2));

Deserialization Example

const deserialized = SerDe.deserialize(serialized);
console.log(deserialized instanceof MyClass); // true

Using initFn for Custom Initialization

If your class requires custom initialization logic upon deserialization, you can define an initFn method in your class. SerDe will automatically call this method after the object is reconstructed.

class MyClass {
    constructor(public name: string, public value: number) {}
    
    _initFn() {
        console.log('MyClass instance has been deserialized and initialized!');
        // Custom logic here...
    }
}

SerDe.classRegistration([MyClass]);

When an object of MyClass is deserialized, the _initFn method will be invoked, allowing you to perform any necessary setup that your class requires.

Contributing

Feel free to open issues or pull requests to improve the library or add new features.


This README.md description should help users understand how to use the SerDe library and highlight its unique features. The emphasis on object uniqueness, class registration, and the initFn mechanism will clarify the powerful capabilities and requirements of the library.