Skip to content

Commit

Permalink
feat: add serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
LuciferHuang committed Mar 17, 2024
1 parent ca7b237 commit 912ec7b
Show file tree
Hide file tree
Showing 8 changed files with 496 additions and 224 deletions.
264 changes: 199 additions & 65 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Instead of directly using api data, we definitely require an adapter layer to tr

## Doc

[中文文档](https://mp.weixin.qq.com/s/YVOYZHTDbAfwcGaUUMld0Q)
[中文文档](https://juejin.cn/post/7221497108008419388)

## Get Started

Expand All @@ -24,99 +24,232 @@ Here is a complex example, hopefully could give you an idea of how to use it:
import { mapperProperty, deepMapperProperty, filterMapperProperty } from "type-json-mapper";

class Lesson {
@mapperProperty("ClassName", "string")
@mapperProperty('ClassName', 'string')
public name: string;
@mapperProperty("Teacher", "string")

@mapperProperty('Teacher', 'string')
public teacher: string;
@mapperProperty("DateTime", "datetime")

@mapperProperty('DateTime', 'datetime')
public datetime: string;

@mapperProperty('Date', 'date')
public date: string;

@mapperProperty('Time', 'time')
public time: string;

@mapperProperty('Compulsory', 'boolean')
public compulsory: boolean;

constructor() {
this.name = "";
this.teacher = "";
this.datetime = "";
this.name = '';
this.teacher = '';
this.datetime = '';
this.date = '';
this.time = '';
this.compulsory = false;
}
}

class Address {
@mapperProperty('province', 'string')
public province: string;
@mapperProperty('city', 'string')
public city: string;
@mapperProperty("full_address", "string")
@mapperProperty('full_address', 'string')
public fullAddress: string;

constructor() {
this.province = "";
this.city = "";
this.fullAddress = "";
this.province = '';
this.city = '';
this.fullAddress = '';
}
}

class Student {
@mapperProperty("StudentID", "string")
@mapperProperty('StudentID', 'string')
public id: string;
@mapperProperty("StudentName", "string")

@mapperProperty('StudentName', 'string')
public name: string;
@mapperProperty("StudentAge", "int")

@mapperProperty('StudentAge', 'int')
public age: number;
@filterMapperProperty("StudentSex", (val = 0) => {
const map = { 0: '保密' 1: '男生', 2: '女生'};
return map[val];
})
public sex: string;
@deepMapperProperty("Address", Address)

@mapperProperty('NotFloat', 'float')
public notFloat: number;

@mapperProperty('NotANum', 'int')
public notNum: number;

@mapperProperty('UnknownType', 'String')
public unknownType: string;

@mapperProperty('Grade', 'float')
public grade: number;

@mapperProperty('AddressIds', 'string')
public addressIds?: string[];

@deepMapperProperty('Address', Address)
public address?: Address;
@deepMapperProperty("Lessons", Lesson)

@deepMapperProperty('Lessons', Lesson)
public lessons?: Lesson[];

@filterMapperProperty('State', (val = 0) => {
const map = { '0': '未知', '1': '读书中', '2': '辍学', '3': '毕业' };
return map[`${val}`];
})
public status: string;

public extra: string;

constructor() {
this.id = "";
this.name = "";
this.id = '';
this.name = '';
this.age = 0;
this.sex = 0;
this.address = undefined;
this.lessons = undefined;
this.notNum = 0;
this.notFloat = 0;
this.unknownType = '';
this.grade = 0;
this.addressIds = [];
this.address = null;
this.lessons = [];
this.status = '';
this.extra = '';
}
}
```

Now here is what API server return, assume it is already parsed to JSON object.

```typescript
let json = {
StudentID: "123456",
StudentName: "李子明",
StudentAge: "9",
StudentSex: "1",
const json = {
StudentID: '123456',
StudentName: '李子明',
StudentAge: '10',
NotANum: 'lol',
NotFloat: 'def',
UnknownType: 'funny',
Grade: '98.6',
AddressIds: [1, 2],
Address: {
province: "广东",
city: "深圳",
full_address: "xxx小学三年二班",
province: '广东',
city: '深圳',
full_address: 'xxx小学三年二班'
},
Lessons: [
{
ClassName: "中国上下五千年",
Teacher: "建国老师",
ClassName: '中国上下五千年',
Teacher: '建国老师',
DateTime: 1609430399000,
Date: 1609430399000,
Time: 1609430399000,
Compulsory: 1
},
{
ClassName: '古筝的魅力',
Teacher: '美丽老师',
DateTime: ''
}
],
};
State: 1,
extra: '额外信息'
}
```

### Deserialize

Simply, just map it use following code.

```typescript
import { deserialize } from 'type-json-mapper';
try {
const student = deserialize(Student, json);
console.dir(student);
console.log(student);
} catch(err) {
console.error(err);
}
```

the result

```ts
{
id: '123456',
name: '李子明',
age: 10,
notNum: 'lol',
notFloat: 'def',
unknownType: 'funny',
grade: 98.6,
addressIds: ['1', '2'],
address: { province: '广东', city: '深圳', fullAddress: 'xxx小学三年二班' },
lessons: [
{
name: '中国上下五千年',
teacher: '建国老师',
datetime: '2020-12-31 23:59:59',
date: '2020-12-31',
time: '23:59:59',
compulsory: true
},
{
name: '古筝的魅力',
teacher: '美丽老师',
datetime: '',
date: undefined,
time: undefined,
compulsory: undefined
}
],
status: '读书中',
extra: '额外信息'
}
```

### Serialize

```typescript
import { serialize } from 'type-json-mapper';
try {
const params = serialize(Student, student);
console.log(params);
} catch(err) {
console.error(err);
}
```

![result.png](https://i.loli.net/2021/04/09/kPJW6Nn5gduBZXq.png)
```typescript
{
StudentID: '123456',
StudentName: '李子明',
StudentAge: 10,
NotANum: 'lol',
NotFloat: 'def',
UnknownType: 'funny',
Grade: 98.6,
AddressIds: [ '1', '2' ],
Address: { province: '广东', city: '深圳', full_address: 'xxx小学三年二班' },
Lessons: [
{
ClassName: '中国上下五千年',
Teacher: '建国老师',
DateTime: '2020-12-31 23:59:59',
Date: '2020-12-31',
Time: '23:59:59',
Compulsory: true
},
{ ClassName: '古筝的魅力', Teacher: '美丽老师', DateTime: '' }
],
State: '读书中',
extra: '额外信息'
}
```

Mock data
### Mock data

```typescript
import { mock } from 'type-json-mapper';
Expand All @@ -125,42 +258,43 @@ console.dir(res);
```

```js
// console.dir(res);
{
id: 'QGBLBA',
name: 'KTFH6d',
age: 4,
sex: '保密',
grade: 76.15,
address: { province: 'qvbCte', city: 'DbHfFZ', fullAddress: 'BQ4uIL' },
id: 'uBP2ug',
name: 'EmcuTs',
age: 7,
notNum: 4,
notFloat: 624.775,
unknownType: '',
grade: 537.3,
addressIds: 'cjcpoC',
address: { province: '1VSl9Z', city: 'tCtF9K', fullAddress: 'Ves3rZ' },
lessons: [
{
name: 'JDtNMx',
teacher: 'AeI6hB',
datetime: '2023-2-18 15:00:07',
date: '2023-2-18',
time: '15:00:07',
compulsory: true
name: 'm2WqeY',
teacher: 'UdIuLT',
datetime: '2024-03-17 03:54:59',
date: '2024-03-17',
time: '03:54:59',
compulsory: false
},
{
name: 'BIggA8',
teacher: '8byaId',
datetime: '2023-2-18 15:00:07',
date: '2023-2-18',
time: '15:00:07',
name: 'dCEoGG',
teacher: 'gJrmkV',
datetime: '2024-03-17 03:54:59',
date: '2024-03-17',
time: '03:54:59',
compulsory: false
},
{
name: 'pVda1n',
teacher: 'BPCmwa',
datetime: '2023-2-18 15:00:07',
date: '2023-2-18',
time: '15:00:07',
name: 'JDVZqP',
teacher: 'dV8ITf',
datetime: '2024-03-17 03:54:59',
date: '2024-03-17',
time: '03:54:59',
compulsory: false
}
],
status: '',
position: '',
status: '未知',
extra: ''
}
```
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "type-json-mapper",
"version": "1.2.7",
"version": "1.2.8",
"description": "Instead of directly using api data, we definitely require an adapter layer to transform data as needed. Furthermore, the adapter inverse the the data dependency from API server(API Server is considered uncontrollable and highly unreliable as data structure may be edit by backend coder for some specific purposes)to our adapter which becomes reliable. Thus, this library is created as the adapter make use of es7 reflect decorator.",
"license": "MIT",
"author": "LuciferHuang",
Expand Down
Loading

0 comments on commit 912ec7b

Please sign in to comment.