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

serialeazy

v2.0.8

Published

An easy-to-use serialization library with support for references, type information, generics, inheritance, and more!

Downloads

59

Readme

Serialeazy

npm deps Install Size Downloads

Serialeazy is a utility library that can serialize complex model-graphs and deserialize them with all references and type information intact.

Serialize and deserialize arbitrarily complex objects into JSON

Serialeazy is feature complete and supports serialization of references, type information, generics, inheritance, function types, and much much more.

Features

  • (De)serialize primitive values
  • (De)serialize any object (tagged as serializable) and have it saved as a reference
  • (De)serialize nested objects, maps, sets and arrays (more coming soon)
  • Supports inheritance
  • Supports generics
  • Support type information
  • Works on any ES5 environment (if ES3 is needed file a feature request)
  • Convenience decorators for ESNext / Typescript
  • Ships with typescript
  • Generic solution that works well with any arbitrary data structure

Installation

$ npm install serialeazy --save

Quick example:

import {serialize, serializable, Serialize, Deserialize} from 'serialeazy';

// Example model classes
@serializable("Animal")
class Animal {
    @serialize
    private name: string;

    public constructor(name: string) {
        this.name = name;
    }

    public getName(): string {
        return this.name;
    }
}

@serializable("Zoo")
class Zoo {
    @serialize
    private animals: Animal[];

    public constructor() {
        this.animals = [];
    }

    public addAnimal(animal: Animal): void {
        this.animals.push(animal);
    }

    public listAnimals(): void {
        console.log("Animals in the zoo: ");
        for (const animal of this.animals)
            console.log("\t" + animal.getName());
    }

    public getAnimals(): Animal[] {
        return this.animals;
    }
}


const zoo = new Zoo();
zoo.addAnimal(new Animal("Giraffe"));
zoo.addAnimal(new Animal("Lion"));
zoo.addAnimal(new Animal("Wombat"));


const json = Serialize(zoo); // serialize into string

const zoo_copy = Deserialize<Zoo>(json); // deserialize back into Zoo

zoo_copy.listAnimals();
console.log(zoo_copy instanceof Zoo);
console.log(zoo_copy.getAnimals().every(animal => animal instanceof Animal));

Output:

Animals in the zoo
    Giraffe
    Lion
    Wombat
true
true

Enabling decorators

TypeScript

Enable the compiler option experimentalDecorators in tsconfig.json or pass it as flag --experimentalDecorators to the compiler. Also make sure to enable the emitDecoratorMetadata flag.

More Examples

1. Basic serialization

@serializable("Person")
class Person {
    /* by default, all properties are automatically marked as '@serialize' so it can be omitted */
    public name: string;
    public age: number;
    public height: string;

    public constructor(name: string, age: number, height: string) {
        this.name = name;
        this.age = age;
        this.height = height;
    }
}

const p1 = new Person("Leon", "20", "6' 1");

const p1_copy = Deserialize<Person>(Serialize(p1));

console.log(p1.name   == p1_copy.name);   // true
console.log(p1.age    == p1_copy.age);    // true
console.log(p1.height == p1_copy.height); // true
console.log(p1_copy instanceof Person);   // true

2. References

@serializable("Person")
class Person {
    /* by default, all properties are automatically marked as '@serialize' so it can be omitted */
    public name: string;
    public age: number;
    public height: string;

    public constructor(name: string, age: number, height: string) {
        this.name = name;
        this.age = age;
        this.height = height;
    }
}

const p1 = new Person("Leon", "20", "6' 1");
const p2 = new Person("Tash", "20", "5' 1");
const p3 = new Perons("Daniel", "21", "6' 0");

const people = [p1, p2, p3, p1, p2, p3];

const people_copy = Deserialize<Person[]>(Serialize(people));

console.log(people_copy.length); // 6
console.log(people_copy[0] === people_copy[3]); // true
console.log(people_copy[1] === people_copy[4]); // true
console.log(people_copy[2] === people_copy[5]); // true