-
-
Notifications
You must be signed in to change notification settings - Fork 760
Isolated Content Serialization #571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Thanks for this!
Yes, I would say so, but with a warning, not an error, and the message should point to the new type and docs. I'm not keen on using an extension method to configure a new instance of the settings object -- @paulcbetts any thoughts? |
- Added IContentSerializer, JsonContentSerializer, XmlContentSerializer - RefitSettings allows the IContentSerializer to be specified - Refit defaults to JsonContentSerializer for back-compat - Tests modified to exercise Json and Xml Serialization - .NET Standard 1.4 has package dep on System.Xml.XmlSerializer
- Added Obsolete attributes - Added Serialized enum as a generic replacement of JSON enum - Updated tests to use ContentSerializer property - Updated README with XML example
Glad to help! I've rebased my branch on master and made some further changes.
I hope this is OK? Thanks. |
IContentSeriaizer - all methods made async - removed 2nd deserialize method - type safe operation ApiException - Added GetContentAsAsync<T>() method - Marked GetContentAs<T>() as obsolete
Good spot with the the missing ConfigureAwait's, I'll add them in. The ValidationApiException.Content property is a bit trickier. If I change this factory method to refit/Refit/ValidationApiException.cs Lines 22 to 25 in c14b2c2
I could then set the Content property from within the We already have an async context where the |
…Exception.Content
Looks good! One last thing and then I think it can be merged. |
This still doesn't make it easy to use one's own ContentSerializer, since the IContentSerializer interface is buried in RefitSettings. I'm using Blazor, Json.NET is not supported under WASM, I can easily do my own deserialization based on returning HttpResponseMessage but posting is proving more than a little evil. If I could simply throw in something that implements IContentSerializer, it'd be easy. But I can't. Why not? |
Hi @richbryant if you want to implement your own IContentSerializer then you can. Here's one which sends everything as Base64 strings and uses a non-existent (I think!) media type of text/base64 public class Base64ContentSerializer : IContentSerializer
{
BinaryFormatter formatter = new BinaryFormatter();
public async Task<T> DeserializeAsync<T>(HttpContent content)
{
var bytes = Convert.FromBase64String(await content.ReadAsStringAsync());
using (var ms = new MemoryStream(bytes))
{
return (T)formatter.Deserialize(ms);
}
}
public Task<HttpContent> SerializeAsync<T>(T item)
{
using (var stream = new MemoryStream())
{
formatter.Serialize(stream, item);
var content = (HttpContent)new StringContent(Convert.ToBase64String(stream.ToArray()), System.Text.Encoding.UTF8, "text/base64");
return Task.FromResult(content);
}
}
}
// use it like this
var client = RestService.For<IApi>("http://localhost:5000",
new RefitSettings
{
ContentSerializer = new Base64ContentSerializer()
}); The @onovotny do you have an ETA for when 4.6.68 be published to NuGet? |
My first effort at adding support for custom serializers #566 . I'm interested in hearing what you think, getting feedback etc. Thanks.
Overview of Changes
Tests
How to use
Possible further changes
RefitSettings.JsonSerializerSettings
property as marked as[Obsolete]
? It should only be used with the JsonContentSerializer.RefitSettings
extension class - something like this:The extension class would be used like this: