项目作者: kaiu-lab

项目描述 :
Simple JSON deserializer for typescript applications
高级语言: TypeScript
项目地址: git://github.com/kaiu-lab/serializer.git
创建时间: 2017-06-29T09:53:26Z
项目社区:https://github.com/kaiu-lab/serializer

开源协议:MIT License

下载


serializer

Build Status
codecov
@kaiu/serializer">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:

  1. npm install --save @kaiu/serializer

Usage

Deserialize

  1. import { Serializer } from '@kaiu/serializer';
  2. const serializer = new Serializer();
  3. class Foo {
  4. bar: string;
  5. public getUpperCaseBar(): string {
  6. return this.bar.toUpperCase();
  7. }
  8. }
  9. const foo = serializer.deserialize<Foo>({ bar: 'baz' }, Foo);
  10. console.log(foo.getUpperCaseBar()); // Will print "BAZ"

More details: Class Serializer

Serialize

  1. import { Serializer, Transient } from '@kaiu/serializer';
  2. const serializer = new Serializer();
  3. class Foo {
  4. bar: string;
  5. @Transient()
  6. secret: string;
  7. public getUpperCaseBar(): string {
  8. return this.bar.toUpperCase();
  9. }
  10. }
  11. const foo = new Foo();
  12. foo.bar = 'baz';
  13. foo.secret = 's3cr3t';
  14. 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

  1. import { Serializer, DeserializeAs } from '@kaiu/serializer';
  2. class Bar {
  3. baz: string;
  4. public getUpperCaseBaz(): string {
  5. return this.baz.toUpperCase();
  6. }
  7. }
  8. class Foo {
  9. @DeserializeAs(Bar)
  10. bar: Bar;
  11. }
  12. const foo = serializer.deserialize<Foo>({ bar: { baz: 'baz' } }, Foo);
  13. console.log(foo.bar.getUpperCaseBar()); // Will print "BAZ"

More details: DeserializeAs

Arrays

  1. import { Serializer } from '@kaiu/serializer';
  2. const serializer = new Serializer();
  3. class Foo {
  4. bar: string;
  5. public getUpperCaseBar(): string {
  6. return this.bar.toUpperCase();
  7. }
  8. }
  9. const foo = serializer.deserialize<Foo>([{ bar: 'baz' }, { bar: 'buz' }], [Foo]);
  10. console.log(foos[1].getUpperCaseBar()); // Will print "BUZ"

Discriminant field

  1. import { Serializer, Registry, Parent } from '@kaiu/serializer';
  2. @Parent({
  3. discriminatorField: 'type',
  4. allowSelf: true // This one is optional.
  5. })
  6. export class Vehicle {
  7. type: string;
  8. color: string;
  9. public getDescription(): string {
  10. return 'I am just a vehicle';
  11. }
  12. }
  13. export class Car extends Vehicle {
  14. public getDescription(): string {
  15. return 'I am a car, I can move using wheels';
  16. }
  17. }
  18. const registry = new Registry();
  19. registry.add([
  20. {
  21. parent: Vehicle,
  22. children: {
  23. car: Car
  24. }
  25. }
  26. ]);
  27. const serializer = new Serializer(registry);
  28. const foo = serializer.deserialize<Vehicle>({type: 'car', color: 'red'}, Vehicle);
  29. console.log(foo.getDescription()); // Will print "I am a car, I can move using wheels"

More details: Class Registry

Property mapping

  1. export class Example{
  2. @DeserializeFieldName('bar')
  3. foo: string;
  4. }
  5. const result = serializer.deserialize<Example>({ bar: 'hey' }, Example);
  6. 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)
    1. npm run release

License

MIT