Why is RestSharp deserializing two dates differently?

﹥>﹥吖頭↗ 提交于 2019-12-05 12:26:32
cgotberg

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)

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.

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