I'm trying to deserialize json to RequestWithDefault
object
JSON:
{
"fields":["f1","f2"]
}
My simple class diagram:
[DataContext]
public abstract class BaseRequest
{
[DataMember]
public virtual List<string> Fields { get; set; }
}
[DataContext]
public class RequestWithDefault : BaseRequest
{
[DataMember]
public override List<string> Fields {get; set; } = new List<string> {"test"}
}
After deserializing json to RequestWithDefault
object Fields
property contains ["test", "f1", "f1"]
. I want to be sure that this default values are applied only in case when Fields
were not specified in request, or was specified as null. How I can do this? I tried with [OnDeserializing]
attribute but without success. Result is the same
According to this:
https://learn.microsoft.com/en-us/dotnet/framework/wcf/feature-details/collection-types-in-data-contracts
Looks like during deserialization DataContractSerializer
calling Add
method from collection. That's why I have also default value and rest of items are added. When I will replace List<string>
to string[]
everything works fine.