diff --git a/spec/deserialize.spec.ts b/spec/deserialize.spec.ts index 3a7b64d..2d94d2d 100644 --- a/spec/deserialize.spec.ts +++ b/spec/deserialize.spec.ts @@ -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"; diff --git a/src/deserialize.ts b/src/deserialize.ts index b77ac95..53310cb 100644 --- a/src/deserialize.ts +++ b/src/deserialize.ts @@ -121,6 +121,8 @@ export function DeserializeJSON(data : JsonType, transformKe } function _Deserialize(data : JsonObject, type : SerializableType, target? : T, instantiationMethod? : InstantiationMethod) : T | null { + if (data === null) { return null; } + const metadataList = MetaData.getMetaDataForType(type); if (metadataList === null) {