title: RestSharp to WCF as JSON date formatting / serializing
body: <p>I have a WCF Method that takes in a list of "Timestamps"</p>
  
  <pre><code>public bool SyncTimestamps(IList&lt;Timestamp&gt; timestamps)
  </code></pre>
  
  <p>For the life of me I cannot get the client to pass values to the host in a format that it is happy with using RestSharp.  The problem appears to be with dateformatting.</p>
  
  <p><strong>Attempt 1</strong></p>
  
  <pre><code>var request = new RestRequest("Timestamp/SyncTimestamps", Method.POST);
  request.RequestFormat = DataFormat.Json;
  request.AddBody(timestamps);
  </code></pre>
  
  <p><strong>Output 1</strong></p>
  
  <pre><code>"[{\"ID\":1,\"JobId\":654321,\"TimestampSelected\":\"2013-08-05T13:51:13.6201529Z\",\"TimestampActual\":\"2013-08-05T13:51:13.6201529Z\",\"Type\":1,\"Active\":false}]"
  </code></pre>
  
  <p><strong>Error 1</strong></p>
  
  <blockquote>
    <p>DateTime content '2013-08-05T13:51:13.6201529Z' does not start with
    '/Date(' and end with ')/' as required for JSON.'</p>
  </blockquote>
  
  <p>I read that this was a problem with the RestSharp serializer so I replaced with Json.Net which produces a slightly different date string.</p>
  
  <p><strong>Attempt 2</strong></p>
  
  <pre><code>var request = new RestRequest("Timestamp/SyncTimestamps", Method.POST);
  request.RequestFormat = DataFormat.Json;
  request.AddBody(JsonConvert.SerializeObject(timestamps));    
  </code></pre>
  
  <p><strong>Output 2</strong></p>
  
  <pre><code>{application/json="[{\"ID\":1,\"JobId\":654321,\"TimestampSelected\":\"2013-08-05T14:54:33.9261815+01:00\",\"TimestampActual\":\"2013-08-05T14:54:33.9251814+01:00\",\"Type\":1,\"Active\":false}]"}
  </code></pre>
  
  <p><strong>Error 2</strong></p>
  
  <pre><code>The exception message is 'Expecting state 'Element'.. 
  Encountered 'Text' with name '', namespace ''. '. See server logs for more details. 
  The exception stack trace is: 
  
  at ReadArrayOfTimestampFromJson(XmlReaderDelegator , XmlObjectSerializerReadContextComplexJson , XmlDictionaryString , XmlDictionaryString , CollectionDataContract ) 
  at System.Runtime.Serialization.Json.JsonCollectionDataContract.ReadJsonValueCore(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) 
  at System.Runtime.Serialization.Json.JsonDataContract.ReadJsonValue(XmlReaderDelegator jsonReader, XmlObjectSerializerReadContextComplexJson context) at 
  </code></pre>
  
  <p>Can anyone suggest a way to produce a dateformat that WCF service will be happy to accept and deserialize?  The docs on MSDN state that the following format is required.</p>
  
  <blockquote>
    <p>DateTime values appear as JSON strings in the form of
    "/Date(700000+0500)/", where the first number (700000 in the example
    provided) is the number of milliseconds in the GMT time zone, regular
    (non-daylight savings) time since midnight, January 1, 1970.</p>
  </blockquote>
  
  <p><strong>UPDATE :</strong> </p>
  
  <p>I found a settings as part of JSON.Net that allows me to transfer the date into the format that WCF appears to be askign for.</p>
  
  <pre><code>var settings = new JsonSerializerSettings() { DateFormatHandling = DateFormatHandling.MicrosoftDateFormat };
  JsonConvert.SerializeObject(obj, settings);
  </code></pre>
  
  <p>This appears to have done the job in creating the serialized string.</p>
  
  <pre><code>{application/json="[{\"ID\":1,\"JobId\":654321,\"TimestampSelected\":\"\\/Date(1375713542908+0100)\\/\",\"TimestampActual\":\"\\/Date(1375713542908+0100)\\/\",\"Type\":1,\"Active\":false}]"}
  </code></pre>
  
  <p>My service however still rejects this with "The exception message is 'Expecting state 'Element'.."</p>