1.1.9 • Published 5 years ago

auto-serialize v1.1.9

Weekly downloads
2
License
ISC
Repository
github
Last release
5 years ago

@AutoSerialize

A decent way to serialize your Typescript models.

Installation

npm install @auto-serialize

How to use

Its is very simple to use. Just add @AutoSerialize decorotor to your model class and extend it from AbstractSerializer class.

@AutoSerialize
export class Address extends AbstractSerializer {
    ...
    ...
}

Now you can use setValues(json) & getValues() to serialize and deserialzie you objects like.

const address = new Address().setValues(json);
address.getValues();

Attribute of class attr:Class?

Yes you can also convert attribute of a class object. Just you need to add MapTo attribute decorator and define type like

...
 
  @MapTo({ type: Address }) address:Address = new Address();

Attribute of type Array of class object attr:Class[]?

For array you just put the class type in array like @MapTo({ type: [Address] })

...
 
  @MapTo({ type: [Address] }) addressList:Address = [];

Whole Code

import { AutoSerialize, AbstractSerializer, MapTo } from "./@auto-serialize";


@AutoSerialize
export class Address extends AbstractSerializer {

    house = '123';
    street = 'xyz';
    city = 'Peshawar';

    constructor() {
        super();
    }

    fullAddress() {
        return `${this.house} ${this.street} ${this.city}`;
    }
}
@AutoSerialize
export class Todo extends AbstractSerializer {

    id: number = null;
    text: string = 'First';
    desc: string = 'first todo description';
    ids: number[] = [];
    @MapTo({ type: Address }) address: Address = new Address();
    @MapTo({ type: [Address] }) list: Address[] = [];

    constructor() {
        super();
    }

    get displayString() { return 'some string'; }

    display() {
        console.log('function');
    }
}

const json = {
    text: 'again',
    address: {
        house: 'new house',
        street: 'new street',
    },
    list: [
        {
            house: 'new house',
            street: 'new street',
        },
        {
            house: 'new house',
            street: 'new street',
        },
    ],
    ids: [1, 2, 3, 4, 4],
}

const todo = new Todo().setValues(json) as Todo;
console.log(todo.getValues());
1.1.9

5 years ago

1.1.8

5 years ago

1.1.7

5 years ago

1.1.6

5 years ago

1.1.5

5 years ago

1.1.4

5 years ago

1.1.3

5 years ago

1.1.2

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago