jgiacomini / Tiny.RestClient

Simpliest Fluent REST client for .NET
MIT License
210 stars 30 forks source link

Support for System.Text.Json #107

Open thomaslevesque opened 4 years ago

thomaslevesque commented 4 years ago

The built-in JSON serializer uses Newtonsoft.Json. But in ASP.NET Core 3.0, the default serializer is System.Text.Json. It would be nice if Tiny.RestClient supported it, because:

It's easy enough to implement a IFormatter that uses System.Text.Json, but there's a bit of an impedance mismatch between the IFormatter and System.Text.Json APIs... IFormatter only has synchronous methods that take a Stream. But System.Text.Json doesn't have synchronous methods that accept a stream, only async ones. And they return a ValueTask, so we can't just use .Result. Maybe the solution would be to add a IAsyncFormatter interface. If the formatter implements it, use the async methods, if not, use the sync methods.

jgiacomini commented 4 years ago

Hello a version 2.0 will support Async Formatter ! And support .netstandard2.1. :)

jgiacomini commented 4 years ago

After a small discussion we choose to change IFormatter like below

public interface IFormatter
{
        string DefaultMediaType { get; }

        bool IsMediaTypeSupported (string mediaType);
        Task SerializeAsync<T>(T data, Stream stream, Encoding encoding);
        Task<T> DeserializeAsync<T>(Stream stream, Encoding encoding);
}
auriou commented 4 years ago

hello, do you intend to delete the reference to newtonsoft in your next version? this could be compatible with netstandard 2.0 so that it is also compatible with Xamarin

And maybe be inspired by the instantiation of Restclient.net https://github.com/MelbourneDeveloper/RestClient.Net

var client = new Client(new ProtobufSerializationAdapter(), new Uri("http://localhost:42908/person"));
var client = new Client(new NewtonsoftSerializationAdapter(), new Uri("https://jsonplaceholder.typicode.com"));
// for System.Text.Json
var client = new Client(new Uri("https://jsonplaceholder.typicode.com"));

In any case I find TinyRest the best library for webrequest, simple to use and read, great work.

jgiacomini commented 4 years ago

Hello, @auriou Yes, the next major version of TinyRestClient will remove the need of newtonsoft. :) The main idea is to be dependency less!

I am working on it!

For the instanciation in the 2.0 version it will be also more configurable.

auriou commented 4 years ago

being able to change serializer would be great. also be able to use protobuf since the GRPC Protocol is increasingly used

thomaslevesque commented 4 years ago

being able to change serializer would be great

It's already possible today, by providing your own implementation of IFormatter. Anyway, maybe Newtonsoft.Json support could be provided as a separate package.

also be able to use protobuf since the GRPC Protocol is increasingly used

Yes, but GRPC is not ReST...

auriou commented 4 years ago

yes it's true, then just the possibility of using a protobuf serializer for a private API.

jgiacomini commented 1 year ago

Hello guys,

I have a PR ready which remove NewtownSoft.Json and make library compatible with System.Text.Json.

I will release this week the version 2.0 of the library

I am also writing benchmarks to try to remove unecessary allocations.