cerialize
cerialize copied to clipboard
Deserializing optional nested fields
Cerialize version 2.0.1
Here is example code:
class TestNested {
@deserializeAs(Number) id: number;
}
class TestModel {
@deserializeAs(Number) id: number;
@deserializeAs(TestNested) n: TestNested;
}
const data1 = {id: 1, n: {id: 2}};
const data2 = {id: 1, n: null};
console.log('Data1', Deserialize(data1, TestModel));
console.log('Data2', Deserialize(data2, TestModel));
Second deserialization is failing with error while first is ok.
MyComponent_Host.ngfactory.js? [sm]:1 ERROR TypeError: Cannot read property 'id' of null
at _Deserialize (deserialize.js:124)
at _Deserialize (deserialize.js:139)
at Deserialize (deserialize.js:159)
// ... remaining stack trace
Is there any way to deserialize optional nested field? Or should I write a custom SerializeFn
?