I have a web service that uses the WCF REST Template 40. The way my data is set up, there are no [DataContract] or [DataMember] attributes on anything, it is just the class and its public properties. Example:
public class Permission : ServiceClass
{
public int PermissionID { get; set; }
public string PermissionName { get; set; }
public string PermissionCode { get; set; }
public string PermissionDescription { get; set; }
public bool IsActive { get; set; }
public DateTime? StartDate { get; set; }
public DateTime? EndDate { get; set; }
public int SystemID { get; set; }
}
This works fine except that if a property is null, e.g the two DateTime objects, the json still contains those values. I would like for them to be omitted. I have tried to add the [DataMember(EmitDefaultValue=false)] and [DataMember(IsRequired=true)] (i’m not using the default serializer when reading in, so I don’t think I need that anyway) and it doesn’t seem to work. Has anyone had any experience with this and know some kind of workaround?
[DataMember]attributes are only enforced if the class is also decorated with[DataContract]. You can do that, but once you go to the data contract route, then the serialization becomes an “opt-in” model: you’ll need to declare the other members with [DataMember] as well:Also, since this contract is now part of the data contract model, your base type (ServiceClass) will likely have to be changed to use the data contract as well.