8000 Started with tests for serialize. · hubcarl/json-typescript-mapper@d363ed3 · GitHub
[go: up one dir, main page]

Skip to content

Commit d363ed3

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
Started with tests for serialize.
1 parent 00ed602 commit d363ed3

File tree

4 files changed

+195
-50
lines changed

4 files changed

+195
-50
lines changed

index.js

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

index.ts

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export interface IDecoratorMetaData<T> {
2323
name?: string,
2424
clazz?: {new(): T},
2525
customConverter?: ICustomConverter,
26-
excludeToJson?: boolean,
27-
noConversion?: boolean
26+
excludeToJson?: boolean
27+
// noConversion?: boolean
2828
}
2929

3030
/**
@@ -178,15 +178,22 @@ export function deserialize<T>(Clazz: {new(): T}, json: Object): T {
178178
return instance;
179179
}
180180

181+
//TODO only recursive if clazz is given in metadata
182+
//TODO also for arrays
183+
//TODO and probably remove unnecessary noConversion option
181184
export function serialize(instance: any): any {
182185

183-
if (isPrimitiveOrPrimitiveClass(instance)) {
186+
if (!isTargetType(instance, 'object') || isArrayOrArrayClass(instance)) {
184187
return instance;
185188
}
186189

187-
if (isArrayOrArrayClass(instance)) {
188-
return instance.map(instanceArr => serialize(instanceArr));
189-
}
190+
// if (isPrimitiveOrPrimitiveClass(instance)) {
191+
// return instance;
192+
// }
193+
194+
// if (isArrayOrArrayClass(instance)) {
195+
// return instance.map(instanceArr => serialize(instanceArr));
196+
// }
190197

191198
const obj = {};
192199
Object.keys(instance).forEach(key => {
@@ -202,21 +209,21 @@ function serializeProperty(metadata: IDecoratorMetaData<any>, prop: any): any {
202209
return;
203210
}
204211

205-
if (metadata.noConversion === true) {
206-
return prop;
207-
}
208-
209212
if (metadata.customConverter) {
210213
return metadata.customConverter.toJson(prop);
211214
}
212215

216+
if (!metadata.clazz) {
217+
return prop;
218+
}
219+
213220
if (isArrayOrArrayClass(prop)) {
214221
return prop.map(propItem => serialize(propItem));
215222
}
216223

217-
if (!isPrimitiveOrPrimitiveClass(prop)) {
218-
return serialize(prop);
219-
}
224+
// if (!isPrimitiveOrPrimitiveClass(prop)) {
225+
return serialize(prop);
226+
// }
220227

221-
return prop;
228+
// return prop;
222229
}

spec/custom-converter.js

Lines changed: 91 additions & 12 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: 66 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const dateConverter: ICustomConverter = {
88
},
99

1010
toJson(data: any): any {
11-
return data;
11+
return 'some-date';
1212
}
1313
};
1414

@@ -25,7 +25,7 @@ class Student {
2525

2626
describe('custom-converter', function () {
2727

28-
it('simple json object', function () {
28+
it('should use the custom converter if available for deserialization', function () {
2929
const json = {
3030
"Name": "Mark",
3131
dateOfBirth: "1995-11-10"
@@ -35,28 +35,81 @@ describe('custom-converter', function () {
3535
expect(student.dateOfBirth).to.be.instanceof(Date);
3636
});
3737 A3E2

38+
//TODO serialize test
39+
3840
});
3941

4042

4143
describe('serialize', function () {
4244

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);
45+
it('should use the property name given in the meta data', function () {
46+
class ClassWithPrimitiveProp {
47+
@JsonProperty('theName')
48+
name: string = undefined;
49+
}
50+
const instance = new ClassWithPrimitiveProp();
51+
instance.name = 'Jim';
52+
const serializedInstance = serialize(instance);
53+
expect(serializedInstance.theName).to.equal('Jim');
5054
});
5155

52-
//TODO test prop name conversion
56+
describe('primitive types', function () {
5357

54-
//TODO test custom converter
58+
const primitiveTypes = ['some-string', true, 25, new Number(25), new Boolean(true)];
5559

56-
//TODO test exclude
60+
primitiveTypes.forEach((primitiveType) => {
61+
it(`should keep ${typeof primitiveType} as is`, function () {
62+
class PrimitiveProp {
63+
@JsonProperty('someProp')
64+
someProp = primitiveType;
65+
}
66+
const instance = new PrimitiveProp();
67+
// instance.someProp = primitiveType;
68+
const serializedInstance = serialize(instance);
69+
expect(serializedInstance.someProp).to.equal(primitiveType);
70+
});
71+
});
72+
73+
});
74+
75+
it('should keep unspecified objects as is', function () {
76+
class ClassWithUnspecObject {
77+
@JsonProperty('date')
78+
date: Date = new Date();
79+
}
80+
const instance = new ClassWithUnspecObject();
81+
const serializedInstance = serialize(instance);
82+
expect(serializedInstance.date).to.equal(instance.date);
83+
});
5784

58-
//TODO test noConversion
85+
it('should use custom converter if available', function () {
86+
class ClassWithCustomConv {
87+
@JsonProperty({name: 'date', customConverter: dateConverter})
88+
date: Date = new Date();
89+
}
90+
const instance = new ClassWithCustomConv();
91+
const serializedInstance = serialize(instance);
92+
expect(serializedInstance.date).to.equal('some-date');
93+
});
94+
95+
it('should exclude properties if specified', function () {
96+
class ClassWithExcludedProp {
97+
@JsonProperty('name')
98+
name: string = 'John';
99+
100+
@JsonProperty({ name: 'lastName', excludeToJson: true})
101+
lastName: string = 'Doe';
102+
}
103+
const instance = new ClassWithExcludedProp();
104+
const serializedInstance = serialize(instance);
105+
expect(serializedInstance.name).to.equal('John');
106+
expect(serializedInstance.lastName).to.be.undefined;
107+
});
108+
109+
//TODO test exclude
59110

60111
//TODO test deep serialization
61112

113+
//TODO test with Arrays
114+
62115
});

0 commit comments

Comments
 (0)
0