Skip to content
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

IContentSerializer implementation and/or example for .NET Core 3.0? #653

Closed
martincostello opened this issue Apr 20, 2019 · 7 comments
Closed
Labels

Comments

@martincostello
Copy link
Contributor

Is your feature request related to a problem? Please describe.

I'm currently maintaining branches of some applications using ASP.NET Core 2.2 + Refit against the ASP.NET Core 3.0 previews.

As the new built-in JSON (de)serialization for 3.0 is still in flux, I'm continuing to use Netwonsoft.Json as the (de)serializer using the new Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package and Refit's JsonContentSerializer class.

With updating to 3.0 preview 4, I'm now having to st AllowSynchronousIO to true on both Kestrel and TestServer in order to prevent transient failures from synchronous buffer flushes due to the synchronous nature of the Newtonsoft.Json APIs.

This highlights a need to me to consider transitioning away to the new JSON APIs in System.Text.Json to remove the need to opt-in to synchronous IO in this way.

If not built-in to Refit itself, it would be useful to the community to provide an example of an IContentSerializer (and if/when merged IContentSerializerWithCancellation) implementation that uses the new JSON APIs to enable people to easily get their applications to "keep working" with minimal opt-ins for "discouraged" code usages, such as synchronous IO.

Describe the solution you'd like

Either a NuGet package or documentation on how to use System.Text.Json with Refit.

Describe alternatives you've considered

  • Custom `` implementation in application source that uses System.Text.Json.
  • Continuing to use Newtonsoft.Json with synchronous IO enabled.

Describe suggestions on how to achieve the feature

  1. An extension NuGet package for Refit shipping in the ASP.NET Core 3.0 timeline that contains an appropriate IContentSerializer implementation for System.Text.Json.
  2. Documentation/sample code for a IContentSerializer implementation using System.Text.Json.
@martincostello
Copy link
Contributor Author

martincostello commented Apr 20, 2019

I had a quick play around by reverse-engineering the MVC input/output formatters and came up with the below.

public class SystemTextJsonContentSerializer : IContentSerializer
{
    public SystemTextJsonContentSerializer(IOptions<JsonOptions> options)
    {
        SerializerOptions = options.Value.JsonSerializerOptions;
    }

    private JsonSerializerOptions SerializerOptions { get; }

    public async Task<T> DeserializeAsync<T>(HttpContent content)
    {
        using var stream = await content.ReadAsStreamAsync().ConfigureAwait(false);
        return await JsonSerializer.DeserializeAsync<T>(stream, SerializerOptions).ConfigureAwait(false);
    }

    public Task<HttpContent> SerializeAsync<T>(T item)
    {
        string json = JsonSerializer.Serialize(item, SerializerOptions);
        var content = new StringContent(json, Encoding.UTF8, "application/json");

        return Task.FromResult<HttpContent>(content);
    }
}

@bugproof
Copy link

bugproof commented Jun 28, 2019

Refit should move away from Json.NET to the new System.Text.Json. Get rid of Json.NET completely.

@jamiehowarth0
Copy link
Member

jamiehowarth0 commented Jun 28, 2019 via email

@clairernovotny
Copy link
Member

.NET Standard 1.4 is no longer relevant as .NET Core 1.0 and 1.1 are both out of support now. .NET Standard 2 is our minimum.

@clairernovotny
Copy link
Member

Keep in mind that Json.NET is far more flexible than System.Text.Json. The latter is meant for raw perf but the former can adapt to more shapes.

If we go with Sys.Text.Json for the default, we may want to provide an optional package that provides a Json.Net serializer/deserializer as well.

@martincostello martincostello changed the title IContentSerializer implementation and/or example for ASP.NET Core 3.0? IContentSerializer implementation and/or example for .NET Core 3.0? Aug 4, 2019
@Gakk
Copy link

Gakk commented Oct 25, 2019

I already have project using System.Text.Json to (de)serialize REST API. Would love to implement Refit, but I would like to not depend on Newtonsoft.Json or change attributes to control serialization of property names.

@martincostello
Copy link
Contributor Author

Resolved by #836

Sergio0694 added a commit to Sergio0694/refit that referenced this issue Mar 24, 2020
@lock lock bot added the outdated label Jun 24, 2020
@lock lock bot locked and limited conversation to collaborators Jun 24, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

5 participants