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

serialijse

v0.3.0

Published

serialize and deserialize your javascript objects, preserve your object model

Downloads

6,882

Readme

serialijse

serialize and deserialize javascript object, preserve your object model. persistance and serialization in javascript and nodejs.

Build Status Coverage Status Code Climate

NPM browser support

Intro

serialijse is an simple persistence framework for JavaScript that overcomes two main limitation of JSON persistence:

  • serialijse deals well with cyclic objects.
  • serialijse preserve object class upon deserialization.

serialijse can be used in nodejs or in the browser. It makes possible to pass data accross the network and recreate on the client side the same rich object model that exists on the server side.

installation

Using serialijse in nodejs

npm install serialijse

Using serialijse in browser

  • install serialijse component:
bower install serialijse
  • in your html file:
<script src="components/serialijse/dist/serialijse.bundle.js">

<script>
const {
    serialize,
    deserialize,
    declarePersistable,
    serializeZ,
    deserializeZ,
} = serialijse;

const vehicule = new Vehicule();
...
const serializationString = serialize(vehicule);
...
const reconstructedObject = deserialize(serializationString);

</script>

Examples

// var s = require("serialijse");
var s = require("./index.js");
var assert = require("assert");


// testing serialization of a simple javascript object with date
function testing_javascript_serialization_object_with_date() {

    var o = {
        date: new Date(),
        name: "foo"
    };
    console.log(o.name, o.date.toISOString());

    // JSON will fail as JSON doesn't preserve dates
    try {
        var jstr = JSON.stringify(o);
        var jo = JSON.parse(jstr);
        console.log(jo.name, jo.date.toISOString());
    } catch (err) {
        console.log(" JSON has failed to preserve Date during stringify/parse ");
        console.log("  and has generated the following error message", err.message);
    }
    console.log("");



    var str = s.serialize(o);
    var so = s.deserialize(str);
    console.log(" However Serialijse knows how to preserve date during serialization/deserialization :");
    console.log(so.name, so.date.toISOString());
    console.log("");
}
testing_javascript_serialization_object_with_date();


// serializing a instance of a class
function testing_javascript_serialization_instance_of_a_class() {

    function Person() {
        this.firstName = "Joe";
        this.lastName = "Doe";
        this.age = 42;
    }

    Person.prototype.fullName = function () {
        return this.firstName + " " + this.lastName;
    };


    // testing serialization using  JSON.stringify/JSON.parse
    var o = new Person();
    console.log(o.fullName(), " age=", o.age);

    try {
        var jstr = JSON.stringify(o);
        var jo = JSON.parse(jstr);
        console.log(jo.fullName(), " age=", jo.age);

    } catch (err) {
        console.log(" JSON has failed to preserve the object class ");
        console.log("  and has generated the following error message", err.message);
    }
    console.log("");

    // now testing serialization using serialijse  serialize/deserialize
    s.declarePersistable(Person);
    var str = s.serialize(o);
    var so = s.deserialize(str);

    console.log(" However Serialijse knows how to preserve object classes serialization/deserialization :");
    console.log(so.fullName(), " age=", so.age);
}
testing_javascript_serialization_instance_of_a_class();


// serializing an object with cyclic dependencies
function testing_javascript_serialization_objects_with_cyclic_dependencies() {

    var Mary = { name: "Mary", friends: [] };
    var Bob = { name: "Bob", friends: [] };

    Mary.friends.push(Bob);
    Bob.friends.push(Mary);

    var group = [ Mary, Bob];
    console.log(group);

    // testing serialization using  JSON.stringify/JSON.parse
    try {
        var jstr = JSON.stringify(group);
        var jo = JSON.parse(jstr);
        console.log(jo);

    } catch (err) {
        console.log(" JSON has failed to manage object with cyclic deps");
        console.log("  and has generated the following error message", err.message);
    }

    // now testing serialization using serialijse  serialize/deserialize
    var str = s.serialize(group);
    var so = s.deserialize(str);
    console.log(" However Serialijse knows to manage object with cyclic deps !");
    console.log(so);
    assert(so[0].friends[0] == so[1]); // Mary's friend is Bob
}
testing_javascript_serialization_objects_with_cyclic_dependencies();
   
   

TypeScript example

import * as serialijse from "serialijse";

class Greeter {
  constructor(
    private myName: string
  ) {}

  greet(name: string): void {
    console.log(`${this.myName} says: Hello ${name}`);
  }
}

let greeter = new Greeter('Spock');
greeter.greet('Scotty');

// serialize
serialijse.declarePersistable(Greeter);
let greeterJson: string = serialijse.serialize(greeter);

// deserialize
serialijse.declarePersistable(Greeter); // not necessary in this example, but needed if deserializing in a new js context
let greeter1: Greeter = serialijse.deserialize<Greeter>(greeterJson);

greeter1.greet('Jean-Luc');

ignoring some members during serialization

Sometime, you may want to ignore some members in serialization

class MyClassWithUnpersistableMembers {
    constructor() {
        this.name = "unset";
        this._cache = [];
        this.$someOtherStuff = 0;
    }
}

MyClassWithUnpersistableMembers.serialijseOptions = {
    ignored: [
        "_cache",  // list here the mebmer you want to ignore
        /$.*/      // use regExp if you need to as well.
    ]
};
declarePersistable(MyClassWithUnpersistableMembers);