Why is RestSharp deserializing two dates differently?

I have a rest call that returns this (using the Advance Rest Client in Chrome for testing):

MyObject: [22] 0: { ID: "123456" UTC1: "2013-04-19T03:12:32Z" UTC2: "2013-04-19T03:12:36.994Z" } 

The code that captures the response and serializes it for the object is as follows:

 IRestResponse<List<MyObject>> response = client.Execute<List<MyObject>>(request); 

When I look at the response object, one of the dates is incorrect. If I inspect it or use objects anyway, I get the following:

 UTC1: 4/19/2013 3:12 UTC2: 4/18/2013 9:12:36 PM <--CONVERTED!! 

I need both to be serialized as the time that is returned in the response, and not converted from UTC / GMT to local time. As you can see above, one value stores the UTC value and the other in my time zone. I realized that both are executed via the Convert.DateTime function, but if I do this using strings, both values ​​come out as converted to local time. I understand that one of the initial values ​​(the one that is converted) does not comply with the ISO 8601 standard (too much accuracy); unfortunately, this is the data that I have to work with now.

Can someone tell me how to get RestSharp to make sure both dates are in UTC?

+4
source share
2 answers

Use Json.NET to perform deserialization instead of the built-in RestSharp deserializer.

 response = client.Execute(request); var myObjects = JsonConvert.Deserialize<List<MyObject>>(response) 
+7
source

Wiring for convenience:

 private class CustomRestClient : RestClient { public CustomRestClient(string baseUrl) : base(baseUrl) { } private IRestResponse<T> Deserialize<T>(IRestRequest request, IRestResponse raw) { request.OnBeforeDeserialization(raw); var restResponse = (IRestResponse<T>)new RestResponse<T>(); try { restResponse = ResponseExtensions.toAsyncResponse<T>(raw); restResponse.Request = request; if (restResponse.ErrorException == null) { restResponse.Data = JsonConvert.DeserializeObject<T>(restResponse.Content); } } catch (Exception ex) { restResponse.ResponseStatus = ResponseStatus.Error; restResponse.ErrorMessage = ex.Message; restResponse.ErrorException = ex; } return restResponse; } public override IRestResponse<T> Execute<T>(IRestRequest request) { return Deserialize<T>(request, Execute(request)); } } 

This is the simple code that I put together, it just overrides Execute<T> and uses Json.net under the hood.

+1
source

All Articles