Skip to content

Releases: dpopescu/ts-serializer

2.2.5

08 Mar 08:51
Compare
Choose a tag to compare
v2.2.5

For Webpack 4 globalObject is defined to resolve server-side issues a…

2.2.4

02 Oct 22:43
Compare
Choose a tag to compare

Fixes #16

2.2.3

12 Aug 09:19
Compare
Choose a tag to compare
v2.2.3

Update version to 2.2.3

1.0.1

31 Oct 15:10
Compare
Choose a tag to compare

TS Serializer - Build Status NPM Downloads LICENSE

TS Serializer provides TypeScript decorators to help developers with serializing/deserializing TypeScript classes into and from JSON objects.

Installation

Using npm:

npm install --save ts-serializer

Usage

TypeScript

Deserialization:

import {Serialize, SerializeProperty, Serializable} from 'ts-serializer';

@Serialize({})
class MyClass extends Serializable {
    @SerializeProperty({
        map: 'username'
    })
    public name:string;
}

let instance:MyClass = new MyClass();

console.log(instance.name); // Prints `undefined`

instance.deserialize({
    username: 'Some Value'
});

console.log(instance.name); // Prints 'Some Value'

Serialization:

import {Serialize, SerializeProperty, Serializable} from 'ts-serializer';

@Serialize({})
class MyClass extends Serializable {
    @SerializeProperty({
        map: 'username'
    })
    public name:string;
}

let instance:MyClass = new MyClass();
instance.name = 'Some Value';

console.log(instance.serialize()); // Prints {username:'Some Value'}

JavaScript

Note: Although the library was designed to be used as a decorator in TypeScript, it doesn't matter that it can't be used in plain old JavaScript. The syntax can be a little messy but the result is the same.

Deserialization:

var Serialize = TSerializer.Serialize;
var SerializeProperty = TSerializer.SerializeProperty;

function MyClass(){
    this.name;
}

Serialize({})(MyClass);
SerializeProperty({
   map: 'username'
})(MyClass.prototype, 'name');

var instance = new MyClass();

console.log(instance.name); // Prints `undefined`

instance.deserialize({
    username: 'Some Value'
});

console.log(instance.name); // Prints 'Some Value'

Serialization:

var Serialize = TSerializer.Serialize;
var SerializeProperty = TSerializer.SerializeProperty;

function MyClass(){
    this.name = 'Some Value';
}

Serialize({})(MyClass);
SerializeProperty({
   map: 'username'
})(MyClass.prototype, 'name');

var instance = new MyClass();

console.log(instance.serialize()); // Prints {username:'Some Value'}

Library Options

The library allows you to pass different serialization/deserialization options both on class level and on property level.

Class Options

root

When you want to deserialize just a child object from the JSON you can use the root option.

@Serialize({
    root: 'childObject'
})
class MyClass extends Serializable {
    @SerializeProperty({})
    public name:string;
}

let instance:MyClass = new MyClass();
instance.deserialize({
    childObject: {
        name: 'Some Value'
    }
});

console.log(instance.name); // Prints 'Some Value'

Property Options

root

The root option can also be used on a property.

Note: If root is already specified at class level the value is inherited to all class properties. If you want to override this, you can use hte . value. In this case, the property will be mapped up one level.

@Serialize({})
class MyClass extends Serializable {
    @SerializeProperty({
        root: 'childObject'
    })
    public name:string;
}

let instance:MyClass = new MyClass();
instance.deserialize({
    childObject: {
        name: 'Some Value'
    }
});

console.log(instance.name); // Prints 'Some Value'

map

When the property name in the JSON doesn't match with your class properties, the map option can be used. This option maps a property from the JSON with a different property from your class.

@Serialize({})
class MyClass extends Serializable {
    @SerializeProperty({
        map: 'username'
    })
    public name:string;
}

let instance:MyClass = new MyClass();
instance.deserialize({
    username: 'Some Value'
});

console.log(instance.name); // Prints 'Some Value'

list

The list option can be used when the JSON property value is a list of items.

@Serialize({})
class MyClass extends Serializable {
    @SerializeProperty({
        list: true
    })
    public items:string[];
}

let instance:MyClass = new MyClass();
instance.deserialize({
    items: ['a', 'b', 'c']
});

console.log(instance.items); // Prints ['a', 'b', 'c']

type

When you want to use non-primitive types for deserialization use the type option.

Note: The type object should also be a Serializable object.

@Serialize({})
class User extends Serializable {
    @SerializeProperty({})
    public firstName:string;
    @SerializeProperty({})
    public lastName:string;
} 

@Serialize({})
class Profile extends Serializable {
    @SerializeProperty({
        type: User
    })
    public user:User;
}

let instance:Profile = new Profile();
instance.deserialize({
    user: {
        firstName: 'John',
        lastName: 'Doe'
    }
});

console.log(instance.user.firstName); // Prints 'John'
console.log(instance.user.lastName); // Prints 'Doe'

1.0.0

31 Oct 15:10
Compare
Choose a tag to compare
1.0.0 Pre-release
Pre-release
Release 1.0.0