none
DataContractJsonSerializer - datetime issue with REST/JSON calls RRS feed

  • Question

  • We have self-hosted WCF service methods using .NET 4.5, WebServiceHost. The interface is REST based with JSON as request & response. Few service methods accept datetime as input parameter as well. Consumer is NOT a .NET app.

            [OperationContract]
            [WebGet(BodyStyle = WebMessageBodyStyle.Bare,
            RequestFormat = WebMessageFormat.Json,
            ResponseFormat = WebMessageFormat.Json)]

    The default DataContractJsonSerializer is converting date fields to values like "/Date(-62135568000000-0800)/". I would like to have the date in output as readable JSON date. 

    - Moving to web-api that uses JSON.Net serialization could be an option - but requires re-design.
    - Having a separate string value for each datetime field (as described at http://social.msdn.microsoft.com/Forums/vstudio/en-US/f36ab49d-1902-4b56-aab8-38abc17b1a9c/datetime-issue-with-datacontractjsonserializer) is not elegant.

    Please suggest if any good work-around exists or a generic implementation.

    Thanks

    Rajani

    Thursday, September 25, 2014 3:33 AM

All replies

  • Hi,

    If you want to convert the special DateTime representation back to a DateTime value after deserialization on .NET, you can use an OnDeserialized callback, like shown below:

    public class Post_da00e5d6_bdd9_4c32_8f36_ca2c60366aff   {     public class PocoPropertyData     {       
    public string N { get; set; } // the property name       public object V { get; set; } // the property value       static readonly DataContractJsonSerializer DateTimeDeserializer = new DataContractJsonSerializer(typeof(DateTime));       [OnDeserialized]       
    public void OnDeserialized(StreamingContext ctx)       {         
    string strV = V as string;         
    if (strV != null)         
    {           
      if (strV.StartsWith("/Date(") && strV.EndsWith(")/"))           {             
    string jsonDateTime = "\"\\" + strV.Substring(0, strV.Length - 1) + "\\/\""; // recreate the JSON object             V = (DateTime)DateTimeDeserializer.ReadObject(new MemoryStream(Encoding.UTF8.GetBytes(jsonDateTime)));           }         
    }       
    }       
    
    public override string ToString()       
    {         
    return string.Format("PocoPropertyData[N={0},V={1}]", N, V);       
    }     
    }     
    
    public static void Test()     
    {       
    MemoryStream ms = new MemoryStream();       PocoPropertyData poco = new PocoPropertyData { N = "DateTimeField", V = DateTime.Now };       DataContractJsonSerializer dcjs = new DataContractJsonSerializer(typeof(PocoPropertyData));       Console.WriteLine(poco);       
    dcjs.WriteObject(ms, poco);       Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));       
    ms.Position = 0;       
    PocoPropertyData poco2 = (PocoPropertyData)dcjs.ReadObject(ms);       Console.WriteLine(poco2);     
    }   
    }

    Friday, September 26, 2014 8:57 AM