Table of contents
Advanced JavaScript object serializer written in TypeScript. Packs objects into a flat, JSON-friendly structure, keeping track of circular references and object constructors.
npm i flatbed
# or
yarn add flatbed
Browser builds aren't available yet, but they are planned for the future.
See information about breaking changes and release notes here.
Flatbed provides serialize and deserialize function that convert objects to and from a flat JSON-friendly
structure.
It also provides convenience stringify and parse functions that directly turn serialized objects to
strings and viceversa, and can be used for the majority of use cases.
import { stringify, parse } from 'flatbed';
const foo = { hello: 'world!', circularReference: [] };
const root = { listOfThings: [foo] };
foo.circularReference = root.listOfThings;
const json = stringify(root);
// JSON can be transferred to disk, memory, network, etc.
const deserialized = parse(json);
console.log(`Hello ${deserialized.listOfThings[0].circularReference[0].hello}`);
// Outputs: "Hello world!"
The most up-to-date documentation for this package is automatically generated from code and available at https://danielegarciav.github.io/flatbed/.
serialize(obj: object): objectSerializes the given object into a flat JSON-friendly structure.
deserialize(obj: object, classes?: class[]): objectDeserializes a previously serialized object into its original state. An array can be supplied with the classes/constructors of the serialized objects.
stringify(obj: object): stringSerializes the given object and stringifies it into JSON.
parse(json: string, classes?: class[]): objectParses the given JSON string as a serialized object, deserializes it, and returns the object in its original state. An array can be supplied with the classes/constructors of the serialized objects.
Flatbed already knows how to construct the following built-in JS objects and won't require you to pass them in:
ObjectArrayMapSetFlatbed will generate structures that keep track of the constructors of the serialized objects. When
deserializing objects of custom classes, you must pass these constructors to deserialize.
This example is written in TypeScript for clarity, but works the same in plain JavaScript.
import { serialize, deserialize } from 'flatbed';
class Player {
x = 0;
y = 0;
name: string;
target?: Player;
constructor(name: string) {
this.name = name;
}
setTarget(t: Player) {
this.target = t;
}
sayTarget() {
this.target && console.log(`My target is ${this.target.name}`);
}
}
class World {
players = new Set<Player>();
logPlayers() {
console.log(`Players: ${[...this.players].map(p => p.name).join(', ')}`);
}
}
const world = new World();
const player1 = new Player('Alice');
const player2 = new Player('Bob');
player1.setTarget(player2);
player2.setTarget(player1);
world.players.add(player1);
world.players.add(player2);
const json = stringify(serialized);
// JSON can be transferred to disk, memory, network, etc.
const deserialized = parse<World>(json, [World, Player]);
deserialized instanceof World; // true
deserialized.logPlayers(); // "Players: Alice, Bob"
deserialized.players instanceof Set; // true
[...deserialized.players][0] instanceof Player; // true
[...deserialized.players][0].sayTarget(); // "My target is Bob"
Check package.json to find scripts related to installing dependencies, building, testing, linting and generating documentation. I am open to new issues and pull requests!
MIT
Generated using TypeDoc