8

I have a working WCF service which used JSON as its RequestFormat and ResponseFormat.

[ServiceContract]     
public interface IServiceJSON 
{ 

    [OperationContract]   
    [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)] 
    MyClassA echo(MyClassA oMyObject); 

} 

[DataContract] 
public class MyClassA 
{ 
    [DataMember] 
    public string message; 

    [DataMember] 
    public List<MyClassB> myList; 

    public MyClassA() 
    { 
        myList = new List<MyClassB>(); 
    } 
} 

[DataContract] 
public class MyClassB 
{ 
    [DataMember] 
    public int myInt; 

    [DataMember] 
    public double myDouble; 

    [DataMember] 
    public bool myBool; 

    [DataMember] 
    public DateTime myDateTime; 

}

The myDateTime property of class MyClassB is of type DateTime. This is being serialized to the following format: "myDateTime":"/Date(1329919837509+0100)/"

The client I need to communicate with can not deal with this format. It requires it to be a more conventional format like for example: yyyy-MM-dd hh:mm:ss

Is it somehow possible to add this to the DataMember attribute? Like so:

[DataMember format = “yyyy-MM-dd hh:mm:ss”] 
public DateTime myDateTime;

Thanks in advance!

Brabbeldas
  • 1,939
  • 4
  • 20
  • 32
  • Did you find a solution? The only way I did it was a kludge workaround, http://stackoverflow.com/questions/25894068/change-the-json-datetime-serialization-in-wcf-4-0-rest-service – bpeikes Sep 17 '14 at 15:01
  • No real solution except for the workaround described by tad donaghe below, which comes down to the same as what you are referring to: add an additional datamember of type string. Maybe you should edit tad's answer and add your example for completeness. – Brabbeldas Sep 17 '14 at 19:03
  • Yeah, I've looked at MS reference code for WCF and serialization and it's unreadable. It's no wonder it appears that they've dumped REST over WCF. – bpeikes Sep 17 '14 at 19:06

3 Answers3

6

Here's an example of the already checked answer...

[DataContract]
public class ProductExport
{
    [DataMember]
    public Guid ExportID { get; set; }

    [DataMember( EmitDefaultValue = false, Name = "updateStartDate" )]
    public string UpdateStartDateStr
    {
        get
        {
            if( this.UpdateStartDate.HasValue )
                return this.UpdateStartDate.Value.ToUniversalTime().ToString( "s", CultureInfo.InvariantCulture );
            else
                return null;
        }
        set
        {
            // should implement this...
        }
    }

    // this property is not transformed to JSon. Basically hidden
    public DateTime? UpdateStartDate { get; set; }

    [DataMember]
    public ExportStatus Status { get; set; }
}

The class above defines two methods to handle the UpdateStartDate. One that contains the nullable DateTime property, and the other convert the DateTime? to a string for the JSon response from my service.

MonkeyWrench
  • 1,809
  • 2
  • 24
  • 48
4

Why not just pass it as an already formatted string?

That is, don't pass the date in your DataContract as a date. Make that member a string instead, and format the string the way your client it wants it.

Tad Donaghe
  • 6,625
  • 1
  • 29
  • 64
0

Although the question is quite old, I came across a similar issue recently. To meet your requirements, you can use the following methods:

[DataMember(Name = "MyDateTime")]
public string FormattedMyDateTime
{
    get => $"{MyDateTime:yyyy-MM-dd hh:mm:ss}";
    set => MyDateTime = DateTime.ParseExact(value, "yyyy-MM-dd hh:mm:ss", CultureInfo.CurrentCulture);
}
public DateTime MyDateTime{ get; set; }
clfaster
  • 1,450
  • 19
  • 26