1

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))]
Dor Cohen
  • 16,769
  • 23
  • 93
  • 161
  • Possible duplicate of [How to handle json DateTime returned from WCF Data Services (OData)](http://stackoverflow.com/questions/3818719/how-to-handle-json-datetime-returned-from-wcf-data-services-odata) – Oluwafemi Jul 25 '16 at 14:00
  • 1
    [`JsonPropertyAttribute.ItemConverterType`](http://www.newtonsoft.com/json/help/html/P_Newtonsoft_Json_JsonPropertyAttribute_ItemConverterType.htm) is the converter to use for *collection items*. You want [`JsonConverter`](http://www.newtonsoft.com/json/help/html/T_Newtonsoft_Json_JsonConverterAttribute.htm). But why did you tag this [tag:wcf]? WCF doesn't use Json.NET, it [uses `DataContractSerializer`](https://stackoverflow.com/questions/11153628). – dbc Jul 25 '16 at 14:01
  • Can you create a [mcve] for your problem? Json.NET does not output dates in that format by default, so I think you unknowingly may be using `DataContractSerializer`. – dbc Jul 25 '16 at 14:07
  • 1
    Json.NET does *not* return that format. By default it returns the ISO format. You don't need to use any attribute for that.On the other hand, WCF *doesn't* use Json.NET, it uses the very, very old DataContractSerializer. – Panagiotis Kanavos Jul 25 '16 at 14:58

2 Answers2

1

Try [JsonProperty(ItemConverterType = typeof(JavaScriptDateTimeConverter)) or use CustomDateConverter as explained here in Parsing JSON DateTime from Newtonsoft's JSON Serializer

Community
  • 1
  • 1
redsam
  • 145
  • 1
  • 18
1

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

Niro
  • 394
  • 1
  • 2
  • 12