I have a few classes that map to below structure/hierarchy.
public class CustomModel
{
public string Message { get; set; }
public int Code { get; set; }
public CustomData Info { get; set; }
}
public class CustomData
{
public CustomData (CustomObject customData)
{
CustomObjectProp = customData.customMessage
}
}
public class CustomObject
{
public string CustomObjectProp {get; set;}
}
When serializing CustomModel I get a Json string like below
{
"Message ": "A message is set.",
"Code": 825,
"Info": "Some Info is set"
}
However when deserializing I get an System.NullReferenceException
error as the constructor of CustomData
gets called with customData being null.
How can I avoid the 'getter' to execute before the setter?