Can I set a custom JsonSerializer to RestSharp Res

2019-06-01 04:31发布

问题:

I'm using the custom JsonSerializer as mentioned in the Readme file in the RestSharp package.

Up until now I added the custom serializer to each request:

RestRequest request = new RestRequest("scans", Method.POST);
request.JsonSerializer = new MyCustomJsonSerializer();

But the read me file mentained I can add it straight to the restclient:

There is one breaking change: the default JsonSerializer is no longer compatible with Json.NET. To use Json.NET for serialization, copy the code from https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279154f41a17b36f7/RestSharp/Serializers/JsonSerializer.cs and register it with your client:

var client = new RestClient();

client.JsonSerializer = new YourCustomSerializer();

I can't find this property in the RestClient class...

Is it a mistake in the ReadMe file? or the package didn't get updated? Is there another way to add the custom serializer instead of adding it to each request?

回答1:

There was a mistake in the Readme, it seems.

https://github.com/restsharp/RestSharp/issues/886 https://github.com/restsharp/RestSharp/issues/947

This is how it should be used:

There is one breaking change: the default JsonSerializer is no longer compatible with Json.NET. To use Json.NET for serialization, copy the code from https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279154f41a17b36f7/RestSharp/Serializers/JsonSerializer.cs and register it with your request:

 var request = new RestRequest();
 request.JsonSerializer = new Shared.JsonSerializer();

then you can use it in a client:

 var client = new RestClient();
 client.Post(request);

Source: https://github.com/restsharp/RestSharp/blob/4e239eaa90abcc699f875bbd7a64efe76a995771/readme.txt