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 🙏

© 2026 – Pkg Stats / Ryan Hefner

jsclass-serializer

v0.2.5

Published

Srialize es6 class based object, and desrialize to its original class.

Readme

Build Status

jsclass-serializer

Yet another serialize module for Node.js, with unique feature that deserialize to its original class based Object.

What Makes class-serializer Unique

Amoung many serializers, jsclass-serializer module is unique for its class restore ability. That is, deserializing does not result in creating Object object, but original class-based object are restored.

How to Use

Just extend your class based on "Serializable" class. And everything works.

Save and/or Load from File (new @0.2.0)

You can save your serialized json text to file, and load it back easily. You can also load all objects under storage path.

const Serializable = require('jsclass-serializer');

// Save and load to/from file
class SampleClass extends Serializable{};

let source = new SampleClass();

Serializable.setStoragePath('./data/');
source.saveToFile();

let target = new SampleClass();
target.loadFromFile(source.uuid);

//load all objects under storage path
let array_of_object = Serializable.loadAll();

Date Ready

"jsclass-serializer" can de/serialize built-in Date type object.

Works with "jsclass-mixin"

When developer decide to extend his/her class from Serializable, developer should give up to extend from other classes. That is a huge limitation, while Java Script allows only single inheritance. Now its not a limitation any more! "jsclass-serializable" can be used with "jsclass-mixin"!
Check out the code below!

// Serializable with jsclass-mixin
const Serializable = require('jsclass-serializer');
const mix = require('jsclass-mixin');

class B {};

class A extends mix(B, Serializable) {
  constructor() {
    super();

    //call Serializable constructor with "this" makes class A serializable
    Serializable.new(this);
  }
};


let source = new A();
let json = source.serialize();
let target = Serializable.deserialize(json);

//below code return true!
console.log(target instanceof A);

Some Notes

Use of Global Namespace

Class serializer consumes global with namespace "serializable_classes". Within this name space, every constructor of Serializable subclasses are stored. This is because of its module-scoped nature of node, by default class-serializer can not recognize target class definition. Please note that this results in extra node instance memory space consuming.

Property "classname" and "uuid" Are Attached Automatically

Serializable attaches property "classname" and "uuid" to its subclasses.
"classname" is to retrieve original class info, and "uuid" is use as filename when you save/load to/from files.

Code Usage

Please take a look at test/test.js for more sample codes.

usage1:

const Serializable = require('jsclass-serializer');

// Serialize and Deserialize Using Static Methods

class SampleClass extends Serializable{};
let c = new SampleClass();

let json = Serializable.serialize(c);
let o = Serializable.deserialize(json);

//o is not a [Object object], but an instance of SampleClass
//below code returns, true.
console.log(o instanceof SampleClass);

usage2:

const Serializable = require('jsclass-serializer');

// Serialize and Deserialize Using Normal Methods
//
class SampleClass extends Serializable{};
let c1 = new SampleClass();
c1.a = "A";

let json = c1.serialize();

let c2 = new SampleClass();
c2.deserialize(json);

//You can deserialize back to its original class.
//below code returns "A"
console.log(c2.a);

API

Modules

Classes

Functions

jsclass-serializer

A module for de/serializing objects.

Serializable

Kind: global class

new Serializable()

Serializable object and deserialize back to its original class instance. Also supports save/load to/from file system.

serializable.serialize() ⇒ json

Serialize object to json format.

Kind: instance method of Serializable
Returns: json - Json text.

serializable.saveToFile() ⇒ json

Save serialized json object to file. Where directory path would be the path previously set by setStoragePath(), and file name would be set equally to given objects uuid.

Kind: instance method of Serializable
Returns: json - Json text.

serializable.deserialize(json)

Deserialize json text to object

Kind: instance method of Serializable

| Param | Type | Description | | --- | --- | --- | | json | json | [description] |

serializable.loadFromFile(uuid)

Load json text from file and convert to object. Where directory path would be the path previously set by setStoragePath().

Kind: instance method of Serializable

| Param | Type | Description | | --- | --- | --- | | uuid | string | Unique identifier to specify the file to load from. |

Serializable.setStoragePath(p)

Set directory path to save/load serialized information to/from file.

Kind: static method of Serializable

| Param | Type | Description | | --- | --- | --- | | p | string | Absolute or relative directory path |

Serializable.saveToFile(o, filename) ⇒ json

Save serialized json object to file. Where directory path would be the path previously set by setStoragePath(). This method can serialize any type of object.

Kind: static method of Serializable
Returns: json - Json text.

| Param | Type | Description | | --- | --- | --- | | o | any | Object to serialize. | | filename | string | Filename to save object. |

Serializable.loadFromFile(filename) ⇒ any

Load json text from file and convert to object. Where directory path would be the path previously set by setStoragePath(). This method can deserialize any file with json text.

Kind: static method of Serializable
Returns: any - Deserialized object.

| Param | Type | Description | | --- | --- | --- | | filename | string | Filename to load json from. |

Serializable.loadAll(Callback) ⇒ Array

Load all json files under storage directory.

Kind: static method of Serializable
Returns: Array - Retrieved objects.

| Param | Type | Description | | --- | --- | --- | | Callback | function | function applies to retrieved objects. |

Serializable.serialize(o) ⇒ json

Serialize object to json format. This method can serialize any type of object.

Kind: static method of Serializable
Returns: json - Json text.

| Param | Type | Description | | --- | --- | --- | | o | any | Object to serialize. |

Serializable.deserialize(json)

Deserialize json text to object This method can deserialize any file with json text.

Kind: static method of Serializable

| Param | Type | Description | | --- | --- | --- | | json | json | [description] |

constructor(baseclass)

"jsclass-serializer" provides features to serialize and deserialize to memory and to file in json format. Deserializing returns instance of original class.

Kind: global function

| Param | Type | Description | | --- | --- | --- | | baseclass | any | Set "this", when use with jsclass-mixin. |