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

@jrc03c/js-serializable

v0.0.4

Published

`js-serializable` is a class that helps to serialize and deserialize objects in a JS OOP context. There are almost certainly better ways to do this, but this is my little attempt at it. 😁

Downloads

1

Readme

Intro

js-serializable is a class that helps to serialize and deserialize objects in a JS OOP context. There are almost certainly better ways to do this, but this is my little attempt at it. 😁

Installation

npm install --save @jrc03c/js-serializable

Usage

The helper class in this package, Serializable makes three assumptions:

  1. That you will subclass it.
  2. That your subclass constructors will accept a single argument that's an object.
  3. That your subclass will implement a serialize method.

Technically, all of those are optional; but this package will probably only do what you want it to do if you follow those three guidelines.

For example, below I've created a Person class that follows the above guidelines.

const Serializable = require("@jrc03c/js-serializable")

class Person extends Serializable {
  name = "Nobody"
  age = 0

  constructor(data) {
    data = data || {}
    super(data)

    if (data.name) {
      this.name = data.name
    }

    if (data.age) {
      this.age = data.age
    }
  }

  serialize() {
    const out = super.serialize()
    out.name = this.name
    out.age = this.age
    return out
  }
}

const p = new Person({ name: "Josh", age: 37 })

When I'm ready to save p to disk, I call p.serialize(), which returns an object that can be passed through JSON.stringify():

const fs = require("fs")
const out = JSON.stringify(p.serialize())
fs.writeFileSync("p.json", out, "utf8")

Then, when I'm ready to get p back from disk, I pass it into the Serialize (or subclass) deserialize method:

const raw = fs.readFileSync("p.json", "utf8")
const data = JSON.parse(raw)
const pReborn = Person.deserialize(data)

What's returned at the end, pReborn, should be in every way identical to p!

NOTE: I said above that you can use the Serialize class's deserialize method or one of its subclasses' deserialize method to bring an object back to life. That's true because they do exactly the same thing! Subclasses just inherit the base class's static deserialize method.

Advanced usage

There may be times where it makes sense to have objects within objects within objects, all of which are instantiated from subclasses of Serializable. Fortunately, things are not much more difficult in these situations. Suppose, for example, that we extend the above Person class with a new class called PersonWithFriends, which will basically just be the Person class but with a property called friends that's an array of Person or PersonWithFriends instances.

class PersonWithFriends extends Person {
  friends = []

  constructor(data) {
    data = data || {}
    super(data)

    if (data.friends) {
      this.friends = data.friends.map(f => PersonWithFriends.deserialize(f))
    }
  }

  serialize() {
    const out = super.serialize()
    out.friends = this.friends.map(f => f.serialize())
    return out
  }
}

The above works even if the friends in the friends array are Person instances rather than PersonWithFriends instances (or a mix of both)!