@jrc03c/js-serializable

0.0.4Β β€’Β PublicΒ β€’Β Published

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)!

Readme

Keywords

none

Package Sidebar

Install

npm i @jrc03c/js-serializable

Weekly Downloads

2

Version

0.0.4

License

ISC

Unpacked Size

9.92 kB

Total Files

6

Last publish

Collaborators

  • jrc03c