Dor Cohen Dor Cohen - 3 months ago 44
C# Question

How to return json date from C# DateTime

I'm using Json.NET/newtonsoft and I have the following C# class:

public class EntityDefinition
{
[DataMember]
public string CreatedBy { get; set; }
[DataMember]
[JsonProperty(ItemConverterType = typeof(IsoDateTimeConverter))]
public DateTime CreatedOn { get; set; }
}


When I try to return this class in my wcf I'm getting the following JSON:

{
"GetDefinitionResult": {
"CreatedBy": "Dor",
"CreatedOn": "/Date(1466428742000+0300)/"
}
}


How can I get the date to be parsed without the "Date(", meaning only the milliseconds or in iso format "yyy-mm-dd"

I tried using the JsonProperty convertor but it still returns the "Date()"

[JsonProperty(ItemConverterType = typeof(IsoDateTimeConverter))]

Answer

WCF is using DataContractSerializer by default to serialize/deserialize messages and the mentioned date format is its default format.

If you'd like to change the way your WCF service serialize/deserialize messages, you should replace some things in the service's behavior (mainly IDispatchMessageFormatter). However, it's too long to describe here and there's a great tutorial about it here

Good Luck

Comments