Skip to main content

Posts

Showing posts with the label hide property from serialization

Custom serialization Using Newtonsoft.Json

When using Newtonsoft. Json to serialize objects, we can avoid few properties from being included in the output for some use case and the same property be included in the serialized output in other cases. There's a feature in the Newtonsoft.Json.NET library, that lets us  determine at runtime whether or not to serialize a particular property of a class / object. During the process of creating a class, we have to include a public method named ShouldSerialize{MemberName} returning a boolean value. Json.NET will call that method during serialization to determine whether or not to serialize the corresponding property of the class. If this method returns true, the property will be serialized; otherwise, it will be ignored. The following illustration shows how this can be achieved. The following is a data definition / class that may be serialized using Newtonsoft.Json.Net public class AuthTypeClaimMapViewModel {     [JsonIgnore]     public bool? _canSerializeName { get;