I have the following Model Class:
public class UserData
{
public IList<bool> Checked { get; set; }
public IList<int> Matches { get; set; }
public int TestQuestionId { get; set; }
public string Text { get; set; }
}
The data coming from my client looks like this:
{"Checked":[true,true,false,false,false,false],"Matches":null,"TestQuestionId":480,"Text":null}
Do I need to modify my model class if it is possible that some of the data may not be present and if so then how could I modify the IList ?
If the field you are trying to deserialize is a Value Type, and your JSON says its
null
, then you need to change it to a Nullable field.If the value being transferred as null is a Reference Type, there isn't a need to change anything, as a Reference Type can be null. When deserializing the JSON, the value will remain null.
For example, lets say
TestQuestionId
was null in your JSON:If you wanted to deserialize that JSON properly, you would have to declare
TestQuestionId
as aNullable<int>
, like this:Edit
To make it simple and clear: Value Types (int, uint, double, sbyte, etc) cannot be assigned a null value, that is why
Nullable<T>
(A.K.A Nullable Types) were invented. Reference Types (string, custom classes) may be assigned a null value.