I am using json.NET to deserialize my json string for my model. below is what I am trying to achieve, please advice what is best.
When there is no data, my answer looks below json string = "{\"message\":\"SUCCESS\",\"result\":null}"
the result is ultimately attached to the species. Therefore, when the answer is null, I would like to initialize my view using the default values ββof the model. And so I would like to call the default constructor when deserializing. The default constructor looks below.
public ProfileModel()
{
this.DefaultTab = DefaultTabOption.PROFILE;
this.DataLoadPosition = new DataLoadPositionOptionsModel();
this.DataLayout = new DataLayoutOptionsModel();
this.NAData = new NADataOptionsModel();
this.DataTable = new DataDisplayOptionsModel();
}
But when there is data, the answer is as follows.
{"message":"SUCCESS","result":{"dataLayout":{"vertical":false},"dataLoadPosition":{"cell":"B2","cursorLocation":false},"dataTable":{"decimalPts":1},"defaultTab":"BROWSE","naData":{"custom":"","naDataOption":"FORWARDFILL"}}}
In this case, I would like to call my parameterized constructor so that the models are correctly initialized.
Deserialization Code:
using (StreamReader reader = new StreamReader(responseStream))
{
var t = JsonConvert.DeserializeObject<T>(reader.ReadToEnd());
return t;
}
T - , . .
public ProfileModel(DefaultTabOption defaultTabModel,
DataLoadPositionOptionsModel dataLoadPositionOption ,
DataLayoutOptionsModel dataLayoutOptios ,
NADataOptionsModel naDataOptions ,
DataDisplayOptionsModel dataTableOptions)
{
this.DefaultTab = defaultTabModel;
this.DataLoadPosition = dataLoadPositionOption;
this.DataLayout = dataLayoutOptios;
this.NAData = naDataOptions;
this.DataTable = dataTableOptions;
}
, , null , . ConstructorHandling, NullValueHandling, .