Last active
October 15, 2018 17:09
-
-
Save refactorsaurusrex/24367f198365ce8d1384 to your computer and use it in GitHub Desktop.
Debugging RestSharp Deserialization Errors http://wp.me/p4mdOw-1f9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assume 'baseUri' and 'request' are valid. | |
public async Task<T> ProcessRequest<T>(string baseUri, RestSharp.RestRequest request) | |
{ | |
var restClient = new RestSharp.RestClient(baseUri); | |
var result = await restClient.ExecuteTaskAsync<T>(request); | |
return result.Data; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Serialization; | |
using RestSharp; | |
using RestSharp.Deserializers; | |
namespace MyAwesomeProject | |
{ | |
public class JsonDeserializer : IDeserializer | |
{ | |
static readonly Lazy<JsonDeserializer> lazyInstance = | |
new Lazy<JsonDeserializer>(() => new JsonDeserializer()); | |
readonly JsonSerializerSettings settings; | |
public static JsonDeserializer Default | |
{ | |
get { return lazyInstance.Value; } | |
} | |
public JsonDeserializer() | |
{ | |
settings = new JsonSerializerSettings | |
{ | |
Error = ErrorHandler | |
}; | |
} | |
public T Deserialize<T>(IRestResponse response) | |
{ | |
return JsonConvert.DeserializeObject<T>(response.Content, settings); | |
} | |
void ErrorHandler(object sender, ErrorEventArgs args) | |
{ | |
// Only handle initial error | |
if (args.CurrentObject != args.ErrorContext.OriginalObject) | |
return; | |
// This was for *my* specific problem. It is not a generic solution! | |
if (args.ErrorContext.Member.ToString() == "Qty" && | |
args.ErrorContext.Error.Message.Contains("Could not convert string to integer")) | |
args.ErrorContext.Handled = true; // Leave null | |
} | |
/// <summary> | |
/// Not used for Json serialization | |
/// </summary> | |
string IDeserializer.RootElement { get; set; } | |
/// <summary> | |
/// Not used for Json serialization | |
/// </summary> | |
string IDeserializer.Namespace { get; set; } | |
/// <summary> | |
/// Not used for Json serialization | |
/// </summary> | |
string IDeserializer.DateFormat { get; set; } | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Assume 'baseUri' and 'request' are valid. | |
public async Task<T> ProcessRequest<T>(string baseUri, RestSharp.RestRequest request) | |
{ | |
var restClient = new RestSharp.RestClient(baseUri); | |
restClient.AddHandler("application/json", JsonDeserializer.Default); | |
restClient.AddHandler("text/json", JsonDeserializer.Default); | |
restClient.AddHandler("image/gif", JsonDeserializer.Default); | |
var result = await restClient.ExecuteTaskAsync<T>(request); | |
return result.Data; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment