c# - 我可以将自定义 JsonSerializer 设置为 RestSharp RestClient

标签 c# json rest json.net

我正在使用 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/

相关文章:

c# - 以编程方式更改系统日期

c# - 如何为 Azure Functions EventHub 监听器配置接收器批量大小?

c# - 如何使应用程序与 .NET 4.0 和 .NET 4.5 兼容

javascript - 是否有用于异步 JSON 解析器的 Node 模块,它不会将整个 JSON 字符串加载到内存中?

javascript - JSON 不会在 MVC 4 中发布包含 1 个项目的数组

javascript - 将 Feefo JSON 数组提取到 Javascript 中,显示为 HTML 列表

python - 我应该为用户创建一个 API,为管理员创建一个 API 吗?

asp.net-mvc - 没有模型的 WebApi GET 和多个 POST 参数始终为 null

python - 如何在两个不同的 docker 容器中的两个 django 应用程序之间建立连接?

c# - 在 C# 中识别手写数字的神经网络