If I have a class like this:
[DataContract(Name = \"\", Namespace = \"\")]
public class MyDataObject
{
[DataMember(Name = \"NeverNull\")]
public ILis
Initialization of IList<int>
with new int[0]
will help you out!
Its the solution that gives me the best results.
Perhaps add a post-serialization callback that checks this at the end of deserialization?
[DataContract(Name = "", Namespace = "")]
public class MyDataObject
{
[OnDeserialized]
public void OnDeserialized(StreamingContext context)
{
if (MyInts == null) MyInts = new List<int>();
}
[DataMember(Name = "NeverNull")]
public IList<int> MyInts { get; set; }
}
Note also that JsonConvert
(unlike DataContractSerializer
) executes the default constructor, so usually you could also have just added a default constructor:
public MyDataObject()
{
MyInts = new List<int>();
}
however, in this case the explict "NeverNull":null
changes it back to null
during deserialization, hence why I've used a callback above instead.