Why is RestSharp deserializing two dates differently?

大城市里の小女人 提交于 2019-12-07 10:06:40

问题


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?


回答1:


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)



回答2:


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.



来源:https://stackoverflow.com/questions/16174316/why-is-restsharp-deserializing-two-dates-differently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!