8000 Rough serialization. · hubcarl/json-typescript-mapper@00ed602 · GitHub
[go: up one dir, main page]

Skip to content

Commit 00ed602

Browse files
Mönnich Daniel, ENT-BD-PDDMönnich Daniel, ENT-BD-PDD
Mönnich Daniel, ENT-BD-PDD
authored and
Mönnich Daniel, ENT-BD-PDD
committed
Rough serialization.
1 parent f554666 commit 00ed602

File tree

4 files changed

+154
-26
lines changed

4 files changed

+154
-26
lines changed

index.js

Lines changed: 35 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on G 10000 itHub.

index.ts

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ import {isTargetType, isPrimitiveOrPrimitiveClass, isArrayOrArrayClass} from './
88
*/
99
const JSON_META_DATA_KEY = 'JsonProperty';
1010

11+
export interface ICustomConverter {
12+
fromJson(data: any): any;
13+
toJson(data: any): any;
14+
}
15+
1116
/**
1217
* IDecoratorMetaData<T>
1318
* DecoratorConstraint
@@ -17,7 +22,9 @@ const JSON_META_DATA_KEY = 'JsonProperty';
1722
export interface IDecoratorMetaData<T> {
1823
name?: string,
1924
clazz?: {new(): T},
20-
fromJson?: (data: any) => any
25+
customConverter?: ICustomConverter,
26+
excludeToJson?: boolean,
27+
noConversion?: boolean
2128
}
2229

2330
/**
@@ -52,6 +59,7 @@ export function JsonProperty<T>(metadata?: IDecoratorMetaData<T>|string): (targe
5259
else {
5360
throw new Error('index.ts: meta data in Json property is undefined. meta data: ' + metadata)
5461
}
62+
5563
return Reflect.metadata(JSON_META_DATA_KEY, decoratorMetaData);
5664
}
5765

@@ -159,8 +167,8 @@ export function deserialize<T>(Clazz: {new(): T}, json: Object): T {
159167
/**
160168
* pass value to instance
161169
*/
162-
if (decoratorMetaData && decoratorMetaData.fromJson) {
163-
instance[key] = decoratorMetaData.fromJson(json[key]);
170+
if (decoratorMetaData && decoratorMetaData.customConverter) {
171+
instance[key] = decoratorMetaData.customConverter.fromJson(json[key]);
164172
} else {
165173
instance[key] = decoratorMetaData ? mapFromJson(decoratorMetaData, instance, json, key) : json[key];
166174
}
@@ -169,3 +177,46 @@ export function deserialize<T>(Clazz: {new(): T}, json: Object): T {
169177

170178
return instance;
171179
}
180+
181+
export function serialize(instance: any): any {
182+
183+
if (isPrimitiveOrPrimitiveClass(instance)) {
184+
return instance;
185+
}
186+
187+
if (isArrayOrArrayClass(instance)) {
188+
return instance.map(instanceArr => serialize(instanceArr));
189+
}
190+
191+
const obj = {};
192+
Object.keys(instance).forEach(key => {
193+
const metadata = getJsonProperty(instance, key);
194+
obj[metadata && metadata.name ? metadata.name : key] = serializeProperty(metadata, instance[key]);
195+
});
196+
return obj;
197+
}
198+
199+
function serializeProperty(metadata: IDecoratorMetaData<any>, prop: any): any {
200+
201+
if (!metadata || metadata.excludeToJson === true) {
202+
return;
203+
}
204+
205+
if (metadata.noConversion === true) {
206+
return prop;
207+
}
208+
209+
if (metadata.customConverter) {
210+
return metadata.customConverter.toJson(prop);
211+
}
212+
213+
if (isArrayOrArrayClass(prop)) {
214+
return prop.map(propItem => serialize(propItem));
215+
}
216+
217+
if (!isPrimitiveOrPrimitiveClass(prop)) {
218+
return serialize(prop);
219+
}
220+
221+
return prop;
222+
}

spec/custom-converter.js

Lines changed: 25 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

spec/custom-converter.ts

Lines changed: 40 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,62 @@
11
import {expect} from 'chai';
2-
import {deserialize, JsonProperty} from '../index';
2+
import {deserialize, serialize, JsonProperty, ICustomConverter} from '../index';
33

4-
const dateConverter = function (date: any): any {
5-
return new Date(date);
4+
5+
const dateConverter: ICustomConverter = {
6+
fromJson(data: any): any {
7+
return new Date(data);
8+
},
9+
10+
toJson(data: any): any {
11+
return data;
12+
}
613
};
714

815
class Student {
9-
@JsonProperty
16+
17+
@JsonProperty('Name')
1018
name: string = undefined;
1119

12-
@JsonProperty({fromJson: dateConverter})
20+
@JsonProperty({customConverter: dateConverter})
1321
dateOfBirth: Date = undefined;
1422

1523
constructor() {}
1624
}
1725

1826
describe('custom-converter', function () {
27+
1928
it('simple json object', function () {
20-
let json = {
21-
"name": "Mark",
22-
"xing": "Galea",
23-
"age": 30,
24-
"AddressArr": [],
25-
"Address": null,
29+
const json = {
30+
"Name": "Mark",
2631
dateOfBirth: "1995-11-10"
2732
};
2833
const student = deserialize(Student, json);
2934
expect(student.name).to.be.equals('Mark');
3035
expect(student.dateOfBirth).to.be.instanceof(Date);
3136
});
3237

33-
3438
});
39+
40+
41+
describe('serialize', function () {
42+
43+
it('simple json object', function () {
44+
const student = new Student();
45+
student.name = 'Jim';
46+
student.dateOfBirth = new Date('1995-11-12');
47+
const studentSerialize = serialize(student);
48+
expect(studentSerialize.Name).to.be.equals('Jim');
49+
expect(studentSerialize.dateOfBirth).to.be.instanceof(Date);
50+
});
51+
52+
//TODO test prop name conversion
53+
54+
//TODO test custom converter
55+
56+
//TODO test exclude
57+
58+
//TODO test noConversion
59+
60+
//TODO test deep serialization
61+
62+
});

0 commit comments

Comments
 (0)
0