3.3.0 • Published 1 month ago

ts-serializable v3.3.0

Weekly downloads
202
License
MIT
Repository
github
Last release
1 month ago

Serializable

Small library for deserialization and serialization for javascript and typescript

Description

  • For working this library needed Metadata Reflection API. If your platform (browser/nodejs) don't support it you must use polifyll. Example: reflect-metadata

  • By default library don't crash on wrong types in json and return default value on wrong property. If you need more secure behavior you must override method onWrongType on Serializable object and drop exception in this method, by your logic want.

Installation

You can use the following command to install this package:

npm install ts-serializable

Usage

This example writed on typescript, but if remove typing, then him will work and on javascript.

import { jsonProperty, Serializable } from "ts-serializable";

export class User extends Serializable {

    // @jsonProperty parrameters is accepted types for json
    // properties, if property in json will not by found or
    // will have invalid type, then will return default value
    @jsonProperty(Number, null)
    public id: number | null = null; // default value necessarily

    @jsonProperty(String)
    public firstName: string = ''; // default value necessarily

    @jsonProperty(String)
    public familyName: string = ''; // default value necessarily

    @jsonProperty(String, void 0)
    public lastName?: string = void 0; // default value necessarily

    @jsonProperty(Date)
    public birthdate: Date = new Date(); // default value necessarily

    @jsonProperty([String])
    public tags: string[] = []; // default value necessarily

    @jsonProperty(OtherClassConstructor, null)
    public other: OtherClassConstructor | null = null; // default value necessarily

    public getFullName(): string {
        return [
            this.firstName,
            this.familyName,
            this.lastName
        ].join(' ');
    }

    public getAge(): number {
        return new Date().getFullYear() - this.birthdate.getFullYear();
    }
}

/**
* Without Serializable
*/
const user: object = JSON.parse(json);
user.getFullName();
// runtime exception: Uncaught TypeError: user.getFullName is not a function
user.getAge();
// runtime exception: Uncaught TypeError: user.getAge is not a function

/**
* With Serializable
*/
const user: User = new User().fromJSON(json);
user.getFullName(); // work fine and return string
user.getAge(); // work fine and return number

// or
const user: User = User.fromJSON(json);
user.getFullName(); // work fine and return string
user.getAge(); // work fine and return number

Naming strategies

Supported conversion between different naming cases, such as SnakeCase, KebabCase, PascalCase and CamelCase. Also you can set custom name for property of json object.

const json = {
    first_name: "Jack",
    last_name: "Sparrow",
    date_of_birth: "1690-05-05T21:29:43.000Z",
    "very::strange::json:name": "I love jewelry"
};

@jsonObject({ namingStrategy: new SnakeCaseNamingStrategy() })
class User extends Serializable {

    @jsonProperty(String, null)
    public firstName: string | null = null;

    @jsonProperty(String, null)
    public lastName: string | null = null;

    @jsonProperty(Date, null)
    public dateOfBirth: Date | null = null;

    @jsonName("very::strange::json:name")
    @jsonProperty(String, null)
    public veryStrangePropertyName: string | null = null;

}

const user = new User().fromJSON(json);

user.firstName === json.first_name; // true
user.lastName === json.last_name; // true
user.dateOfBirth?.toISOString() === json.date_of_birth; // true
user.veryStrangePropertyName === json["very::strange::json:name"]; // true

Settings

How to specify settings:

// Global settings
Serializable.defaultSettings: SerializationSettings = { ...options };

// By object settings
@jsonObject(settings?: Partial<SerializationSettings>)
class User extends Serializable { ...code }

// By method settings
new User().fromJSON(json: object, settings?: Partial<SerializationSettings>);

Supported settings:

  • namingStrategy, INamingStrategy, default null - property name conversion strategies.
  • dateFormatHandling, enum, default IsoDateFormat - ...coming soon.
  • missingMemberHandling, enum, default Ignore - ...coming soon.
  • referenceLoopHandling, enum, default Serialize - ...coming soon.
  • nullValueHandling, enum, default Include - ...coming soon.
  • defaultValueHandling, enum, default Ignore - ...coming soon.
  • logLevel, enum, default Warning - ...coming soon.

View-Models from Backend Models

If you need to create view-model from dto or entities model you can use same model. Just add VM property to dto or entities model and mark this property by @jsonIgnore() decorator and this property will not be serialized to json.

import { jsonProperty, jsonIgnore, Serializable } from "ts-serializable";

export class User extends Serializable {

    @jsonProperty(String)
    public firstName: string = ''; // default value necessarily

    @jsonProperty(String)
    public familyName: string = ''; // default value necessarily

    @jsonIgnore()
    public isExpanded: boolean  = false;

}

const user = new User();
user.isExpanded = true;
JSON.stringify(user);
// Result: {"firstName":"","familyName":""}

Bonus

Deep copy

const newUser: User = new User().fromJSON(oldUser);
3.3.0

1 month ago

3.2.2

6 months ago

3.2.1

6 months ago

3.2.0

6 months ago

3.2.3

6 months ago

3.0.58

6 months ago

3.0.59

6 months ago

3.0.56

7 months ago

3.0.54

7 months ago

3.0.52

11 months ago

3.0.50

11 months ago

3.0.46

12 months ago

3.0.48

12 months ago

3.0.44

1 year ago

3.0.42

1 year ago

3.0.40

1 year ago

3.0.34

1 year ago

3.0.32

1 year ago

3.0.38

1 year ago

3.0.36

1 year ago

3.0.30

1 year ago

3.0.24

2 years ago

3.0.22

2 years ago

3.0.28

2 years ago

3.0.26

2 years ago

3.0.20

2 years ago

3.0.12

2 years ago

3.0.10

2 years ago

3.0.16

2 years ago

3.0.14

2 years ago

3.0.4

2 years ago

3.0.2

2 years ago

3.0.8

2 years ago

3.0.6

2 years ago

3.0.18

2 years ago

2.0.58

3 years ago

2.0.68

2 years ago

2.0.66

2 years ago

2.0.64

2 years ago

2.0.62

2 years ago

2.0.60

2 years ago

2.0.56

3 years ago

2.0.54

3 years ago

2.0.52

3 years ago

2.0.50

3 years ago

2.0.48

3 years ago

2.0.46

3 years ago

2.0.44

3 years ago

2.0.42

3 years ago

2.0.40

3 years ago

2.0.38

3 years ago

2.0.36

3 years ago

2.0.34

3 years ago

2.0.31

3 years ago

2.0.32

3 years ago

2.0.30

4 years ago

2.0.28

4 years ago

2.0.29

4 years ago

2.0.26

4 years ago

2.0.27

4 years ago

2.0.25

4 years ago

2.0.24

4 years ago

2.0.22

4 years ago

2.0.23

4 years ago

2.0.21

4 years ago

2.0.20

4 years ago

2.0.19

4 years ago

2.0.15

4 years ago

2.0.16

4 years ago

2.0.13

4 years ago

2.0.14

4 years ago

2.0.11

4 years ago

2.0.12

4 years ago

2.0.17

4 years ago

2.0.18

4 years ago

2.0.10

4 years ago

2.0.7

4 years ago

2.0.9

4 years ago

2.0.8

4 years ago

2.0.6

4 years ago

2.0.5

5 years ago

2.0.3

5 years ago

2.0.2

5 years ago

2.0.4

5 years ago

2.0.1

5 years ago

1.1.25

5 years ago

1.1.23

5 years ago

1.1.24

5 years ago

1.1.22

5 years ago

1.1.21

5 years ago

1.1.20

5 years ago

1.1.19

5 years ago

1.1.16

5 years ago

1.1.18

5 years ago

1.1.17

5 years ago

1.1.15

5 years ago

1.1.14

5 years ago

1.1.11

5 years ago

1.1.10

5 years ago

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

6 years ago

1.1.4

6 years ago

1.1.3

6 years ago

1.1.2

6 years ago

1.1.1

6 years ago

1.1.0

6 years ago

1.0.1

6 years ago

0.0.12

7 years ago

0.0.11

7 years ago

0.0.10

7 years ago

0.0.8

7 years ago

0.0.7

7 years ago

0.0.6

7 years ago

0.0.5

7 years ago

0.0.4

7 years ago

0.0.3

7 years ago

0.0.2

7 years ago

0.0.1

7 years ago