Skip to content

Commit

Permalink
Update with the right name, to dpopescu
Browse files Browse the repository at this point in the history
  • Loading branch information
delete authored and dpopescu committed Aug 12, 2019
1 parent 0a43e19 commit 60e61a1
Showing 1 changed file with 51 additions and 28 deletions.
79 changes: 51 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
<h1 align="center"> TS Serializer</h1>

<p align="center">
<a href="https://www.npmjs.com/package/@delete21/ts-serializer" target="_blank">
<img src="https://img.shields.io/npm/dt/@delete21/ts-serializer.svg" alt="npm" />
<a href="https://www.npmjs.com/package/dpopescu/ts-serializer" target="_blank">
<img src="https://img.shields.io/npm/dt/ts-serializer.svg" alt="npm" />
</a>
<a href="https://github.com/delete/ts-serializer/blob/master/LICENSE" target="_blank">
<img src="https://img.shields.io/npm/l/@delete21/ts-serializer.svg" alt="license" />
<a href="https://travis-ci.org/dpopescu/ts-serializer.svg?branch=master" target="_blank">
<img src="https://travis-ci.org/dpopescu/ts-serializer.svg" alt="license" />
</a>
<a href="https://github.com/delete/ts-serializer/stargazers" target="_blank">
<img alt="GitHub stars" src="https://img.shields.io/github/stars/delete/ts-serializer.svg?style=social" alt="stars">
<a href="https://github.com/dpopescu/ts-serializer/stargazers" target="_blank">
<img alt="GitHub stars" src="https://img.shields.io/github/stars/dpopescu/ts-serializer.svg?style=social" alt="stars">
</a>
</p>

<br />

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

## This project is a fork from [dpopescu's project](https://github.com/dpopescu/ts-serializer)

### Why?
The project is not maintained anymore and there are some feature and fixes that me and my team needed:

- `optional` property option, disscursed here [issue 10](https://github.com/dpopescu/ts-serializer/issues/10) - [commit here](https://github.com/delete/ts-serializer/commit/d5eb8121af08a00635d7445f50507c20a513127d)
- fix to properties with falsy values such as `0` (zero) and `false` that should be valid - [commit here](https://github.com/delete/ts-serializer/commit/926f3c6d945b2ab32fa551c07912d43a469a5702)



## Installation

### Using npm:
`npm install --save @delete21/ts-serializer`

`npm install --save ts-serializer`

## Usage

You can find a [codesandbox playground here](https://codesandbox.io/s/ts-serializer-playground-gdbid)!

### TypeScript

#### Deserialization:
```TypeScript
import {Serialize, SerializeProperty, Serializable} from '@delete21/ts-serializer';

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

@Serialize({})
class MyClass extends Serializable {
Expand All @@ -58,10 +51,13 @@ instance.deserialize({

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

---

#### Serialization:

```TypeScript
import {Serialize, SerializeProperty, Serializable} from '@delete21/ts-serializer';
import {Serialize, SerializeProperty, Serializable} from 'ts-serializer';

@Serialize({})
class MyClass extends Serializable {
Expand All @@ -76,10 +72,13 @@ 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:

```JavaScript
var Serialize = TSerializer.Serialize;
var SerializeProperty = TSerializer.SerializeProperty;
Expand All @@ -103,8 +102,11 @@ instance.deserialize({

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

---

#### Serialization:

```JavaScript
var Serialize = TSerializer.Serialize;
var SerializeProperty = TSerializer.SerializeProperty;
Expand All @@ -122,11 +124,17 @@ 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.

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.

```TypeScript
@Serialize({
root: 'childObject'
Expand All @@ -145,8 +153,11 @@ instance.deserialize({

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.
Expand All @@ -169,9 +180,13 @@ instance.deserialize({

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.

```TypeScript
@Serialize({})
class MyClass extends Serializable {
Expand All @@ -188,9 +203,13 @@ instance.deserialize({

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

---

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

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

```TypeScript
@Serialize({})
class MyClass extends Serializable {
Expand All @@ -207,8 +226,11 @@ instance.deserialize({

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.
Expand All @@ -220,7 +242,7 @@ class User extends Serializable {
public firstName:string;
@SerializeProperty({})
public lastName:string;
}
}

@Serialize({})
class Profile extends Serializable {
Expand All @@ -243,7 +265,9 @@ console.log(instance.user.lastName); // Prints 'Doe'
```

#### optional
The `optional` option can be used when the property or the value may not exist.

The `optional` option can be used when the property or the value may not exist.

```TypeScript
@Serialize({})
class User extends Serializable {
Expand All @@ -253,7 +277,7 @@ class User extends Serializable {
optional: true
})
public age:number;
}
}

@Serialize({})
class Profile extends Serializable {
Expand All @@ -276,5 +300,4 @@ console.log(instance.user.age); // Prints 'null'

# Contribute

You can help improving this project sending PRs and helping with issues.
Also you can ping me at [Twitter](http://twitter.com/pinheirofellipe)
You can help improving this project sending PRs and helping with issues.

0 comments on commit 60e61a1

Please sign in to comment.