我正在使用 RestSharp 包中的自述文件中提到的自定义 JsonSerializer。
到目前为止,我向每个请求添加了自定义序列化器:
RestRequest request = new RestRequest("scans", Method.POST);
request.JsonSerializer = new MyCustomJsonSerializer();
但是自述文件表明我可以将其直接添加到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();
我在 RestClient 类中找不到此属性...
自述文件中是否有错误?或者包没有更新? 是否有另一种方法来添加自定义序列化器,而不是将其添加到每个请求中?
最佳答案
自述文件中似乎有错误。
https://github.com/restsharp/RestSharp/issues/886 https://github.com/restsharp/RestSharp/issues/947
这就是它的使用方式:
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);
来源:https://github.com/restsharp/RestSharp/blob/4e239eaa90abcc699f875bbd7a64efe76a995771/readme.txt
关于c# - 我可以将自定义 JsonSerializer 设置为 RestSharp RestClient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43849892/