Skip to content

Commit

Permalink
Merge pull request #89 from prudnikov/mw/v2
Browse files Browse the repository at this point in the history
Fixes #88: Deserializing optional nested fields
  • Loading branch information
weichx authored Sep 14, 2018
2 parents 2474bb2 + f05d214 commit 5d18ae6
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions spec/deserialize.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,26 @@ describe("Deserializing", function () {
expect(instance.test.value2).toBe(100);
});

it("deserializes nullable nested types", function () {
class Test {
@deserializeAs(String) value0 : string = "bad";
@deserializeAs(Boolean) value1 : boolean = false;
@deserializeAs(Number) value2 : number = 1;
}

class Test0 {
@deserializeAs(Test) test : Test;
}

const json = {
test: null
};
const target = createTarget(makeTarget, instantiationMethod, Test0);
if (target) target.test = createTarget(makeTarget, instantiationMethod, Test);
const instance = Deserialize(json, Test0, target, instantiationMethod);
expect(instance.test).toBeNull();
});

it("deserializes doubly nested types", function () {
class Test0 {
@deserializeAs(String) value0 : string = "bad";
Expand Down
2 changes: 2 additions & 0 deletions src/deserialize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export function DeserializeJSON<T extends JsonType>(data : JsonType, transformKe
}

function _Deserialize<T extends Indexable>(data : JsonObject, type : SerializableType<T>, target? : T, instantiationMethod? : InstantiationMethod) : T | null {
if (data === null) { return null; }

const metadataList = MetaData.getMetaDataForType(type);

if (metadataList === null) {
Expand Down

0 comments on commit 5d18ae6

Please sign in to comment.