Reputation: 10476
I have the following model class:
[DataContract(Namespace = "http://api.mycompany.com/v1")]
public class Job{
[DataMember(IsRequired = true), Required]
public long Id { get; set; }
[DataMember(EmitDefaultValue = false)]
public DateTime? StartDate { get; set; }
[DataMember(EmitDefaultValue = false)]
public DateTime? EndDate { get; set; }
[DataMember(EmitDefaultValue = false)]
public bool isCurrentJob { get; set; }
}
For some reason when I do an HTTP GET request, the boolean isCurrentJob field does not get included in the serialized response back to the client. Is there any reason this might be happening? The value is not null, it's set to true.
If I change this field to a string, it appears no problem in the response. All of the other fields appear in the response.
Upvotes: 0
Views: 2413
Reputation: 27187
It is not desirable to use EmitDefaultValue
with value types (like bool
). Default value for bool
is false
and that's why it gets omitted for you.
Perhaps you were intending to use nullable bool bool?
instead?
More info about this here:
In the .NET Framework, types have a concept of default values. For example, for any reference type the default value is null, and for an integer type it is 0. It is occasionally desirable to omit a data member from the serialized data when it is set to its default value. To do this, set the EmitDefaultValue property to false (it is true by default).
Setting the EmitDefaultValue property to false is not a recommended practice. It should only be done if there is a specific need to do so (such as for interoperability or to reduce data size).
Upvotes: 4