Serializer is a serialization library written in Typescript made to handle typing in deserialized objects.
Install through npm:
npm install --save @kaiu/serializer
import { Serializer } from '@kaiu/serializer';
const serializer = new Serializer();
class Foo {
bar: string;
public getUpperCaseBar(): string {
return this.bar.toUpperCase();
}
}
const foo = serializer.deserialize<Foo>({ bar: 'baz' }, Foo);
console.log(foo.getUpperCaseBar()); // Will print "BAZ"
More details: Class Serializer
import { Serializer, Transient } from '@kaiu/serializer';
const serializer = new Serializer();
class Foo {
bar: string;
@Transient()
secret: string;
public getUpperCaseBar(): string {
return this.bar.toUpperCase();
}
}
const foo = new Foo();
foo.bar = 'baz';
foo.secret = 's3cr3t';
console.log(serializer.serialize(foo)); // Will print '{ "bar": "baz" }'
More details: Class Serializer
In order to use the serializer properly inside an Angular application, we created an angular wrapper to provide this serializer as an Injectable service: https://github.com/kaiu-lab/ng-serializer
import { Serializer, DeserializeAs } from '@kaiu/serializer';
class Bar {
baz: string;
public getUpperCaseBaz(): string {
return this.baz.toUpperCase();
}
}
class Foo {
@DeserializeAs(Bar)
bar: Bar;
}
const foo = serializer.deserialize<Foo>({ bar: { baz: 'baz' } }, Foo);
console.log(foo.bar.getUpperCaseBar()); // Will print "BAZ"
More details: DeserializeAs
import { Serializer } from '@kaiu/serializer';
const serializer = new Serializer();
class Foo {
bar: string;
public getUpperCaseBar(): string {
return this.bar.toUpperCase();
}
}
const foo = serializer.deserialize<Foo>([{ bar: 'baz' }, { bar: 'buz' }], [Foo]);
console.log(foos[1].getUpperCaseBar()); // Will print "BUZ"
import { Serializer, Registry, Parent } from '@kaiu/serializer';
@Parent({
discriminatorField: 'type',
allowSelf: true // This one is optional.
})
export class Vehicle {
type: string;
color: string;
public getDescription(): string {
return 'I am just a vehicle';
}
}
export class Car extends Vehicle {
public getDescription(): string {
return 'I am a car, I can move using wheels';
}
}
const registry = new Registry();
registry.add([
{
parent: Vehicle,
children: {
car: Car
}
}
]);
const serializer = new Serializer(registry);
const foo = serializer.deserialize<Vehicle>({type: 'car', color: 'red'}, Vehicle);
console.log(foo.getDescription()); // Will print "I am a car, I can move using wheels"
More details: Class Registry
export class Example{
@DeserializeFieldName('bar')
foo: string;
}
const result = serializer.deserialize<Example>({ bar: 'hey' }, Example);
console.log(result.foo); // Will print 'hey'
More details: DeserializeFieldName
Everything is detailed on our documentation website.
npm install
while current directory is this repoRun npm test
to run tests once or npm run test:watch
to continually run tests.
npm run release
MIT
Generated using TypeDoc