A typescript library to serialize/deserialize classes to/from string in a flat format. Supports inheritance, circular reference and more
When I was developing a electron app I needed a serializer that fill some requirements to use with IPC:
- After serialize I wanted that the object keep the methods (right prototype)
- I needed inheritance
- Supports circular reference is always good
npm install typescript-flat-serializer --save
If you want to use our decorators you also need to set experimentalDecorators and emitDecoratorMetadata to true into the tsconfig.json file.
For example:
{
"compilerOptions": {
...
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
...
}
}
First let's create some models
import { TSFlatCollection, TSFlatObject } from "typescript-flat-serializer";
@TSFlatObject()
export abstract class Animal {
protected constructor(public name: string) {
}
}
// This decorator NEEDS to be placed on every class that you want to serialize.
// Without this decorator the behavior will be like stringify/parse from JSON.
@TSFlatObject()
export class Dog extends Animal {
// This decorator will take care of serialize/deserialize our collection
@TSFlatCollection({collectionType: "set"})
favoriteFoods: Set<Food>;
constructor(name: string, public beautiful: boolean, favoriteFoods: Set<Food>) {
super(name);
this.favoriteFoods = favoriteFoods;
}
}
@TSFlatObject()
export class Food extends Animal {
constructor(name: string) {
super(name);
}
}
Now we only need to serialize/deserialize the animal.
import { parse, stringify } from "typescript-flat-serializer";
const foods = new Set([new Food('all')])
const animal: Animal = new Dog('Luffy', true, foods);
// Let's go serialize our animal
const str = stringify(animal)
// value of str: [{"name":"Luffy","beautiful":true,"favoriteFoods":"#_1_#","__class__":"Dog"},["#_2_#"],{"name":"all","__class__":"Food"}]
// And now we can deserialize the animal
const parsedAnimal = parse<Animal>(str);
You can find another examples of utilisation on tests.
First let's create some models
import { registerTSFlat } from "typescript-flat-serializer";
export abstract class Animal {
protected constructor(public name: string) {
registerTSFlat(
{
target: Animal,
}
);
}
}
export class Dog extends Animal {
favoriteFoods: Set<Food>;
constructor(name: string, public beautiful: boolean, favoriteFoods: Set<Food>) {
super(name);
registerTSFlat(
{
target: Dog,
},
{ collectionName: 'favoriteFoods', options: {collectionType: 'set'} }
)
this.favoriteFoods = favoriteFoods;
}
}
export class Food extends Animal {
constructor(name: string) {
super(name);
registerTSFlat(
{
target: Food,
},
)
}
}
Now we only need to serialize/deserialize the animal.
import {parse, stringify} from "typescript-flat-serializer";
const foods = new Set([new Food('all')])
const animal: Animal = new Dog('Luffy', true, foods);
// Let's go serialize our animal
const str = stringify(animal)
// value of str: [{"name":"Luffy","beautiful":true,"favoriteFoods":"#_1_#","__class__":"Dog"},["#_2_#"],{"name":"all","__class__":"Food"}]
// And now we can deserialize the animal
const parsedAnimal = parse<Animal>(str);
Used to make a class serializable.
@TSFlatObject()
export class Dog {
}
Used do make a Array|Set|Map|Dictionary
@TSFlatObject()
export class Dog {
@TSFlatCollection({collectionType: "map"})
placesVisited: Map<string, boolean>
}
collectionType
Type: CollectionTypeString
Optional: false
Description: The way to specify the type of collection
Used modify the serialization/deserialization of a property.
@TSFlatObject()
export class Dog {
@TSFlatCollection({collectionType: "map"})
@TSFlatProperty({
beforeStringify: (m) => {
m.set('Brazil', true);
}
})
placesVisited: Map<string, boolean>
}
options
Type: TSFlatPropertyOptions
Optional: true
Description: The option to customize the serialization/deserialization of the target property.
Used do register an object and its items.
registerTSFlat<T>(objectRegistration: FlatObjectRegistration<T>, ...items: FlatItem<T>[])
objectRegistration
Type: FlatObjectRegistration<T>
Optional: false
Description: Information about the class that we want to make serializable.
Type: FlatItem<T>[]
Optional: true
Description: Items that this class have that we want to make serializable.
Used to serialize a object.
stringify(obj: any, options?: StringifyOptions): string
obj
Type: any
Optional: false
Description: The object that will be serialized
options
Type: StringifyOptions
Optional: true
Description: Custom options to the serialization
string
Used to deserialize a string into a object.
parse<T>(str: string): T
T
export type FlatObjectRegistration<T> = {
target: Type<T>,
options?: TSFlatObjectProperties
}
export type FlatItem<T> = FlatPropertyRegistration<T> | FlatCollectionRegistration<T>;
export type FlatPropertyRegistration<T> = {
propertyName: (keyof T), options?: TSFlatPropertyOptions,
}
export type FlatCollectionRegistration<T> = {
collectionName: (keyof T), options: TSFlatCollectionOptions
}
export type CollectionTypeString = 'array' | 'dictionary' | 'map' | 'set';
export type PropertyTransformer = (property: any) => any;
export interface TSFlatPropertyMetadata {
beforeStringify?: PropertyTransformer;
afterParse?: PropertyTransformer;
}
export interface TSFlatPropertyOptions extends TSFlatPropertyMetadata {
}
export type StringifyOptions = {
rFDCOptions?: RFDCOptions
}
export type CustomWayOfCloningObjectMap = Map<Type<any>, (obj: any) => any>;
export type RFDCOptions = {
customWayOfCloningObject?: CustomWayOfCloningObjectMap
}
This library before the serialization makes a clone of the object. By default the cloning supports the types:
- Object
- Array
- Number
- String
- null
- Date
- undefined
- Buffer
- TypedArray
- Map
- Set
- Function
- AsyncFunction
- GeneratorFunction
- arguments
To support other type, like DateTime of Luxon, you should do something like that:
const customWayOfCloningObject: CustomWayOfCloningObjectMap = new Map();
const rFDCOptions: RFDCOptions = {
customWayOfCloningObject
}
customWayOfCloningObject.set(DateTime, (obj) => DateTime.fromMillis(obj.toMillis()));
const str = stringify(obj, {rFDCOptions});