From ba01753fc56c6237f637c5841d650951875b516b Mon Sep 17 00:00:00 2001 From: pla Date: Mon, 14 Jan 2019 19:09:10 +0100 Subject: [PATCH] Initialize properties in deserialization example --- README.md | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 47ae20d..dc34cec 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,13 @@ Lets have a look at an example: ```typescript class SimpleRoster { @JsonProperty() - private name: String; + private name: String = undefined; @JsonProperty() - private worksOnWeekend: Boolean; + private worksOnWeekend: Boolean = undefined; @JsonProperty() - private numberOfHours: Number; + private numberOfHours: Number = undefined; @JsonProperty({type:Date}) - private systemDate: Date; + private systemDate: Date = undefined; public isAvailableToday(): Boolean { if (this.systemDate.getDay() % 6 == 0 && this.worksOnWeekend == false) { @@ -83,6 +83,8 @@ a `Number` object and the `systemDate` field will be assinged a `Date` object wi `Sat Dec 31, 2016`. The method will also make sure that all the nested object graphs has been created based on the JSON model. +Note that initializing the properties is important (eg by assigning `undefined` at their declaration or in the constructor). Properties which are not initialized properly are not part of the keys-collection after object creation - therefore deserialization would not work as expected. + The `serialize` method will serialize an object graph into JSON string (similar to what `JSON.stringrify()` method would do) while honoring the decorator metadata. Following is an example of serialization: