Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is RestSharp deserializing two dates differently?

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

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

The code that grabs the response and serializes it to an object looks like this:

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

When I look at the response object one of the dates is wrong. If I inspect it or use the objects in any way I get this:

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, not converted from UTC/GMT to local time. As you can see above one value keeps its UTC value while the other gets converted to my timezone. I figured that both were being run through the Convert.DateTime function but if I do that with the strings both values come out as converted to local time. I realize that one fo the original values (the one that is getting converted) doesn't exactly obey ISO 8601 format (too much precision); unfortunately that is the data I have to work with for now.

Can anyone tell me how to force RestSharp to make sure both dates are in UTC?

like image 827
Mario Avatar asked Jan 14 '23 13:01

Mario


1 Answers

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

response = client.Execute(request);    
var myObjects = JsonConvert.Deserialize<List<MyObject>>(response)
like image 72
cgotberg Avatar answered Jan 24 '23 06:01

cgotberg