4

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?

Mario
  • 3,405
  • 6
  • 38
  • 48
  • Both a valid ISO8601 representation of UTC date time (there is no restriction on number of digits in fractions part). Try comma as separator - I believe original separator was dot, but according to [Wikipedia](http://en.wikipedia.org/wiki/ISO_8601) newer version advice to use comma... – Alexei Levenkov Apr 23 '13 at 16:21
  • In MyObject are UTC1 and UTC2 defined as the same type? – cgotberg Apr 23 '13 at 16:22
  • @cgotberg, yes, they are the same type (DateTime) – Mario Apr 23 '13 at 16:28
  • 1
    I tried your example in Newtonsoft.Json and it doesn't do the weird datetimeoffset switch. You could use RestSharp to get the raw response and then use var myObject = JsonConvert.Deserialize(jsonString) to do the Deserialization. – cgotberg Apr 23 '13 at 17:45
  • @cgotberg, thanks. If you want to put that in as the answer I will accept it - I can't figure out another way to get around this other than to change the current computer's time zone – Mario Apr 23 '13 at 17:52

2 Answers2

8

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)
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
cgotberg
  • 2,045
  • 1
  • 18
  • 18
1

Posting this 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 a simple code I put together, it just overrides Execute<T> and uses Json.net under the hood.

Erti-Chris Eelmaa
  • 25,338
  • 6
  • 61
  • 78