Giter VIP home page Giter VIP logo

picklify's Introduction

Build Status codecov Greenkeeper badge

Serialize/Deserialize objects maintaining references (circular refs too)

Install

npm install picklify

Basic Usage

You can serialize an object using the picklify.picklify function. The only requirement for your custom objects is to have an empty constructor (or make your constructor does not crash when calling it with no arguments).

const { picklify, unpicklify } = require('picklify');
let serializedData = picklify(someObject);
// serializedData is JSON object that can be saved or being sent through the network

let originalObject = unpicklify(serializedData);

/* unpicklify need access to constructors for building the original objects.
** So they must be on the global scope or being passed as a second argument to unpicklify

let originalObject = unpicklify(serializedData, [class1, class2]);
*/

Examples

Keep references

const aSharedList = [4, 5, 6];
const object1 = {'a': 1, 'l1': aSharedList};
const object2 = {'b': 2, 'l2': aSharedList};
const input = [object1, object2, object1, object2];

const serializedData = picklify.picklify(input);
const reconstructedObject = picklify.unpicklify(serializedData);

reconstructedObject[0].l1 ==  reconstructedObject[1].l2 // true
reconstructedObject[0] == reconstructedObject[2]; // true
reconstructedObject[1] == reconstructedObject[3]; // true

Allow recursion

let object1 = {'a': 1};
const object2 = {'b': 2, 'o1': object1}; //object2 references object1
object1['o2'] = object2; //object1 references object2


const serializedData = picklify.picklify(object1);
const reconstructedObject = picklify.unpicklify(serializedData);

Custom classes

class Musician {
    constructor(name, age) {
        this.name = name
        this.age = age
    }
}

class Band {
    constructor(name, members) {
        this.name = name
        this.members = members
    }
}

let slash = new Musician('Slash', 44);
let gnr = new Band('GnR', [slash]);
let velvetRevolver = new Band('Velvet Revolver', [slash]);

const bands = [gnr, velvetRevolver];
const serializedData = picklify.picklify(bands);

const reconstructedBands = picklify.unpicklify(
    serializedData, [Band, Musician]
);

// evaulates to true
reconstructedBands[0].members[0] == reconstructedBands[1].members[0]

Only Serialize specific attributes on objects

class Musician {

    constructor(name, age, instrument) {
        this.name = name
        this.age = age
        this.instrument = instrument
    }

    propsToSerialize() {
        return ["name",]
    }
}


const slash = new Musician('Slash', 44, "Guitar");
const serializedData = picklify.picklify(slash);

const reconstructedSlash = picklify.unpicklify(
    serializedData, [Musician]
);

// evaulates to true
reconstructedSlash.age === undefined
reconstructedSlash.instrument === undefined

Reference

see test.js

picklify's People

Contributors

lwoites avatar greenkeeper[bot] avatar dependabot-preview[bot] avatar jperelli avatar dependabot[bot] avatar

Stargazers

Francisco Svrznjak Spinelli avatar Braden Napier avatar  avatar

picklify's Issues

Disallow serializing of specific types/classes

Allow a new setting to the picklifyfunction that allows to ignore serialization of certain types of objects.
A common use case would be to avoid serializing Promises objects, which cannot be created calling a empty constructor (new Promise())

picklify.picklify(tooObject, {"ignored_types": {Promise} })

Disallow serializing of properties matching a re

Add new option to the picklify function that allows you to ignore all obect properties that match a regular expression

It could be something like picklify.picklify(rootObject, {"ignored_properties": /^_/ })

Store classes paths when serializing

Right now you must pass your custom class objects to the unpicklify function. That's needed because picklify doesn't store the path to your classes so it can't requirethem and instantiate them.

Add a new options to the picklify function that allows you to store the class paths. That will allow calling unpicklify without passing the externalClasses array

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.