Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RestSharp specifying the default JsonSerializer in version 107

Tags:

restsharp

In the current code which is using older version of RestSharp there is option to specify the default JsonSerializer, I am not able to figure out how to do specify request.JsonSerializer in version 107

            var request = new RestRequest("abc");
            request.AddHeader(Constants.HttpHeaderNames.ContentType, "application/json; charset=utf-8");            
            request.JsonSerializer = NewtonsoftJsonSerializer.Default;
like image 591
aquitted-mind Avatar asked Nov 02 '25 20:11

aquitted-mind


1 Answers

Request-specific serializers are no longer supported. The proper way of using RestSharp (similar to HttpClient) is to have one instance per API client instance. Normally, an API you talk to has the same serialization for all its endpoints.

If you must use NewtonsoftJson, use it as described in the docs.

client.UseNewtonsoftJson();

And please, don't add the content type header.

As the API might change, you can always reference to the latest version docs.

UPDATE 26 April 2023

The current latest v110 has the serialiser configuration moved to RestSharpOptions. It was done to avoid changing the serialiser during concurrent calls, which might create weird side effects.

The new API can be used like this:

var client = new RestClient(
    options, 
    configureSerialization: s => s.UseSerializer(() => new CustomSerializer());
);

There is a discussion to add an option to have a serialiser set per request.

like image 145
Alexey Zimarev Avatar answered Nov 04 '25 20:11

Alexey Zimarev