Options
All
  • Public
  • Public/Protected
  • All
Menu

@kaiu/serializer

serializer

Build Status codecov npm version devDependency Status GitHub issues GitHub stars GitHub license

Table of contents

About

Serializer is a serialization library written in Typescript made to handle typing in deserialized objects.

Installation

Install through npm:

npm install --save @kaiu/serializer

Usage

Deserialize

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

Serialize

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

Usage with Angular

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

Advanced Usages

Deep class fields

    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

Arrays

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"

Discriminant field

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

Property mapping

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

Documentation

Everything is detailed on our documentation website.

Development

Prepare your environment

  • Install Node.js and NPM
  • Install local dev dependencies: npm install while current directory is this repo

Testing

Run npm test to run tests once or npm run test:watch to continually run tests.

Release

  • Bump the version in package.json (once the module hits 1.0 this will become automatic)
    npm run release
    

License

MIT

Generated using TypeDoc