Serialize and deserialize any object and all of its references šŸ„£

Overview

Super Cereal šŸ„£

Serialize and deserialize any object and all of its references. Supports:

  • Class (with inheritance set-up)
  • Object
  • Array
  • Function
  • Map
  • Set
  • Date

TL;DR: You get to keep your lovely graph structure (even if its circular šŸ¤Æ ) and all of your lovely class methods too.

But how?

import { Store, Model } from "./mod.ts";

const storeObj: Record<string, string> = {};

const store = new Store({
  get: (id: string) => storeObj[id],
  set: (id:string, value: string) => storeObj[id] = value
});

class Person extends Model {
  name: string;
  friends: Person[] = [];

  constructor(name: string) {
    super(store, arguments);
    this.name = name;
  }

  addFriend(friend: Person) {
    this.friends.push(friend);
    friend.friends.push(this);
  }
}

const jim = new Person("Jim");
const bob = new Person("Bob");
jim.addFriend(bob);

const jimId = jim.save();
const freshJim = store.load(jimId) as Person;

console.log(freshJim.friends);

const steve = new Person("Steve");
freshJim.addFriend(steve);

console.log(freshJim.friends);

The Al-Gore-ithm does a depth-first-search, leaving unique IDs on non-primitive values. It then serializes and stores objects by ID, replacing all refs with the corresponding ID to "unlink" the structure so it never gets stuck in a circular reference loop.

First instantiate a Store, then make your classes extend Model and call super(store, arguments) in their constuctors. This allows the store to reinstantiate them with their initial arguments before using Object.assign to apply deserialized property values.

But why?

I wanted a tool that could serialize any in-memory data structure with classes and store it in a key-value store. This allows for browser-based storage of OOP software state that can extend to the edge/cloud.

The goal was to require minimal additional class boilerplate and I think this fits the bill nicely, just extend from Model and call super(store, arguments) in constructors. The only caveat is that you have use store.load(id) as Classname to keep accurate TS syntax highlighting (there is currently no way for TS to infer this and generic types don't work with nested structures).

You might also like...

Unread-Messages.js is a lightweight library that lets a user add floating number notifications to any object.

Unread-Messages.js is a lightweight library that lets a user add floating number notifications to any object.

Unread-Messages.js About Unread-Messages.js is a lightweight library that lets a user add mobile-like notification counter badge to any object with ju

Dec 18, 2021

"Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make simple calculations and read random math-related quotes. Its built using react

Math Magician "Math magicians" is a website for all fans of mathematics. It is a Single Page App (SPA) that allows users to make simple calculations a

Feb 23, 2022

P.S Its easy is a website to cater to all your PS allotment needs

P.S Its easy is a website to cater to all your PS allotment needs

P.S. It's Easy All-in-one Web App for all your Practice School Allotment needs! Note: Developers trying to fork and test. Please wait, we'll set up a

Sep 26, 2022

A nuxt 2 wrapper around derrickreimer/fathom-client to be able to use usefathom.com in all its glory

A nuxt 2 wrapper around derrickreimer/fathom-client to be able to use usefathom.com in all its glory

This package is a nuxt 2 wrapper around derrickreimer/fathom-client to be able to use usefathom.com in all its glory. Thanks to @derrickreimer for this framework agnostic library ā¤ļøā€šŸ”„.

Aug 18, 2022

šŸ“œ Sharable eslint configuration rimac technology uses in all of its projects.

Eslint Config Usage Install the library as a dev dependency alongside required dependencies using yarn add -D @rimac-technology/eslint-config

Nov 23, 2022

Tenzi is a dice game. The player needs to roll dice until they are all the same. Clicking on a dice, freezes it at its current value between rolls. Best scores are saved to local storage.

Tenzi is a dice game. The player needs to roll dice until they are all the same. Clicking on a dice, freezes it at its current value between rolls.  Best scores are saved to local storage.

Roll until all dice are the same Try me! Technologies Used Description Tenzi is a dice game used to demonstrate the use of React Hooks (useState, useE

Nov 23, 2022

A simple code that creates a string of random characters displayed in an html object, all saving in a json file.

A simple code that creates a string of random characters displayed in an html object, all saving in a json file.

I'm 17 Years Old Developer / Lead Developer. šŸ’» I'm wroking on AdrenalinaRP, GrandRDM. šŸŖ™ Iā€™m currently learning JavaScript. šŸ«‚ Iā€™m looking to collabo

Nov 17, 2022

Hacktoberfest is all about meeting up all brains. In this repository we are planning to come with many ideas and works. You all can share your ides/works here.

Hacktoberfest is all about meeting up all brains. In this repository we are planning to come with many ideas and works. You all can share your ides/works here.

Hacktoberfest Submit your Work Hacktoberfest is all about meeting up all brains. In this repository we are planning to come with many ideas and works.

Oct 5, 2022
Releases(0.1.2)
  • 0.1.2(Dec 26, 2022)

  • v0.1.1(Dec 24, 2022)

    What's Changed

    • Response serialization! by @sebringrose in https://github.com/sebringrose/super_cereal/pull/1

    New Contributors

    • @sebringrose made their first contribution in https://github.com/sebringrose/super_cereal/pull/1

    Full Changelog: https://github.com/sebringrose/super_cereal/compare/v0.1.0...v0.1.1

    Source code(tar.gz)
    Source code(zip)
  • v0.1.0(Nov 10, 2022)

  • v0.0.1(Oct 17, 2022)

Owner
Seb Ringrose
A friendly-neighbourhood web developer šŸ‘¾
Seb Ringrose
Serialize an HTML Form to a JavaScript Object, supporting nested attributes and arrays.

jquery.serializeJSON Adds the method .serializeJSON() to jQuery to serializes a form into a JavaScript Object. Supports the same format for nested par

Mario Izquierdo 1.7k Dec 12, 2022
Serialize arbitrary NodeJS closures and customize serialization behavior.

Closure Serializer This is a fork of the Pulumi Closure Serializer. @pulumi/pulumi. Motivation Functionless allows developers to write cloud applicati

null 4 Jul 19, 2022
It's a repository to studies. Its idea is to learn about Nx and its plugins.

StudyingNx This project was generated using Nx. ?? Smart, Fast and Extensible Build System Adding capabilities to your workspace Nx supports many plug

Open-ish 4 May 13, 2022
Can see everything, beware of its omniscience, kneel before its greatness.

Can see everything, beware of its omniscience, kneel before its greatness. Summary Presentation Installation Removing Credits Presentation Main goal T

Duc Justin 3 Sep 30, 2022
Chappe - šŸ§‘ā€šŸ’» Developer Docs builder. Write guides in Markdown and references in API Blueprint. Comes with a built-in search engine.

Chappe Developer Docs builder. Write guides in Markdown and references in API Blueprint. Comes with a built-in search engine. Chappe is a Developer Do

Valerian Saliou 146 Jan 1, 2023
šŸ§‘ā€šŸ’» Developer Docs builder. Write guides in Markdown and references in API Blueprint. Comes with a built-in search engine.

Developer Docs builder. Write guides in Markdown and references in API Blueprint. Comes with a built-in search engine. Chappe is a Developer Docs buil

Crisp (OSS) 146 Jan 1, 2023
This Plugin is For Logseq. If you're using wide monitors, you can place journals, linked references, and journal queries side by side.

Logseq Column-Layout Plugin Journals, linked references, and journal queries can be placed side by side if the minimum screen width is "1850px" or mor

YU 14 Dec 14, 2022
This Repository consist of Daily learning JS, Assignments, coding challenge, projects, references, tutorial

?? A Tour of JavaScript This Repository consist of Daily learning, Assignments, coding challenge, projects, references, tutorial. ??ā€?? ??ā€?? alert(

null 23 Sep 7, 2022
Types generator will help user to create TS types from JSON. Just paste your single object JSON the Types generator will auto-generate the interfaces for you. You can give a name for the root object

Types generator Types generator is a utility tool that will help User to create TS Interfaces from JSON. All you have to do is paste your single objec

Vineeth.TR 16 Dec 6, 2022
Simple and Extensible Markdown Parser for Svelte, however its simplicity can be extended to any framework.

svelte-simple-markdown This is a fork of Simple-Markdown, modified to target Svelte, however due to separating the parsing and outputting steps, it ca

Dave Caruso 3 May 22, 2022