@128keaton/ts-serializer 中文文档教程
TS Serializer
TS Serializer 提供
TypeScript
装饰器,以帮助开发人员将 TypeScript 类序列化/反序列化为 JSON 对象。
This project is a fork from dpopescu's project
Why?
该项目不再维护,我和我的团队需要一些功能和修复:
optional
property option, disscursed here issue 10 - commit here- fix to properties with falsy values such as
0
(zero) andfalse
that should be valid - commit here
Installation
Using npm:
npm install --save @delete21/ts-serializer
Usage
你可以找到一个 此处为 codesandbox 游乐场!
TypeScript
Deserialization:
```打字稿 从“@delete21/ts-serializer”导入{Serialize、SerializeProperty、Serializable};
@连载({}) 类 MyClass 扩展序列化 { @SerializeProperty({ 地图:'用户名' }) 公共名称:字符串; }
让实例:MyClass = new MyClass();
控制台日志(实例名称); // 打印 undefined
instance.deserialize({ 用户名:'一些价值' });
控制台日志(实例名称); // 打印 'Some Value'
---
#### Serialization:
TypeScript 从“@delete21/ts-serializer”导入{Serialize、SerializeProperty、Serializable};
@连载({}) 类 MyClass 扩展序列化 { @SerializeProperty({ 地图:'用户名' }) 公共名称:字符串; }
让实例:MyClass = new MyClass(); instance.name = '一些值';
console.log(instance.serialize()); // 打印 {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;
函数 MyClass(){ 这个名字; }
序列化({})(MyClass); 序列化属性({ 地图:'用户名' })(MyClass.prototype, '名称');
var 实例 = new MyClass();
控制台日志(实例名称); // 打印 undefined
instance.deserialize({ 用户名:'一些价值' });
控制台日志(实例名称); // 打印“一些值”
---
#### Serialization:
JavaScript var Serialize = TSerializer.Serialize; var SerializeProperty = TSerializer.SerializeProperty;
函数 MyClass(){ this.name = '一些值'; }
序列化({})(MyClass); 序列化属性({ 地图:'用户名' })(MyClass.prototype, '名称');
var 实例 = new MyClass();
console.log(instance.serialize()); // 打印 {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.
TypeScript @连载({ 根:'childObject' }) 类 MyClass 扩展序列化 { @SerializeProperty({}) 公共名称:字符串; }
让实例:MyClass = new MyClass(); instance.反序列化({ 子对象:{ 名称:'一些价值' } });
控制台日志(实例名称); // 打印 '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.
TypeScript @连载({}) 类 MyClass 扩展序列化 { @SerializeProperty({ 根:'childObject' }) 公共名称:字符串; }
让实例:MyClass = new MyClass(); instance.反序列化({ 子对象:{ 名称:'一些价值' } });
控制台日志(实例名称); // 打印 '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 @连载({}) 类 MyClass 扩展序列化 { @SerializeProperty({ 地图:'用户名' }) 公共名称:字符串; }
让实例:MyClass = new MyClass(); instance.反序列化({ 用户名:'一些价值' });
控制台日志(实例名称); // 打印 'Some Value'
---
#### list
The `list` option can be used when the JSON property value is a list of items.
TypeScript @连载({}) 类 MyClass 扩展序列化 { @SerializeProperty({ 列表:正确 }) 公共项目:字符串[]; }
让实例:MyClass = new MyClass(); instance.反序列化({ 项目:['a','b','c'] });
console.log(实例.items); // 打印 ['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.
] @连载({}) 类用户扩展可序列化{ @SerializeProperty({}) 公共名字:字符串; @SerializeProperty({}) 公共姓氏:字符串; }
@序列化({}) 类配置文件扩展可序列化{ @SerializeProperty({ 类型:用户 }) 公共用户:用户; }
让实例:Profile = new Profile(); instance.反序列化({ 用户:{ 名字:'约翰', 姓氏:'母鹿' } });
console.log(instance.user.firstName); // 打印'约翰' console.log(instance.user.lastName); // 打印 'Doe'
#### optional
The `optional` option can be used when the property or the value may not exist.
TypeScript @连载({}) 类用户扩展可序列化{ @SerializeProperty({}) 公共名称:字符串; @SerializeProperty({ 可选:真 }) 公开年龄:人数; }
@序列化({}) 类配置文件扩展可序列化{ @SerializeProperty({ 类型:用户 }) 公共用户:用户; }
让实例:Profile = new Profile(); instance.反序列化({ 用户:{ 名字:'约翰', } });
console.log(instance.user.firstName); // 打印'约翰' console.log(instance.user.age); // 打印'null' ```
Contribute
你可以帮助改进这个项目,发送 PR 并帮助解决问题。
你也可以在 Twitter 上联系我
TS Serializer
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
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 - commit here- fix to properties with falsy values such as
0
(zero) andfalse
that should be valid - commit here
Installation
Using npm:
npm install --save @delete21/ts-serializer
Usage
You can find a codesandbox playground here!
TypeScript
Deserialization:
```TypeScript import {Serialize, SerializeProperty, Serializable} from '@delete21/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:
TypeScript import {Serialize, SerializeProperty, Serializable} from '@delete21/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:
JavaScript 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:
JavaScript 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.
TypeScript @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.
TypeScript @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.
TypeScript @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.
TypeScript @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.
TypeScript @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'
#### optional
The `optional` option can be used when the property or the value may not exist.
TypeScript @Serialize({}) class User extends Serializable { @SerializeProperty({}) public name:string; @SerializeProperty({ optional: true }) public age:number; }
@Serialize({}) class Profile extends Serializable { @SerializeProperty({ type: User }) public user:User; }
let instance:Profile = new Profile(); instance.deserialize({ user: { firstName: 'John', } });
console.log(instance.user.firstName); // Prints 'John' 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