【问题标题】:Can I set a custom JsonSerializer to RestSharp RestClient我可以将自定义 JsonSerializer 设置为 RestSharp RestClient
【发布时间】:2017-10-06 14:07:37
【问题描述】:

我正在使用 RestSharp 包的自述文件中提到的自定义 JsonSerializer。

到目前为止,我为每个请求添加了自定义序列化程序:

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

但是read me文件提示我可以直接将它添加到restclient:

有一个重大变化:默认的 JsonSerializer 是 no 与 Json.NET 更兼容。要使用 Json.NET 进行序列化, 复制代码 https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279154f41a17b36f7/RestSharp/Serializers/JsonSerializer.cs 并向您的客户注册:

var client = new RestClient();

client.JsonSerializer = new YourCustomSerializer();

我在 RestClient 类中找不到这个属性...

是自述文件中的错误吗?还是包没有更新? 是否有另一种方法可以添加自定义序列化程序而不是将其添加到每个请求中?

【问题讨论】:

    标签: c# json rest json.net


    【解决方案1】:

    自述文件中似乎有错误。

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

    应该这样使用:

    有一个重大变化:默认的 JsonSerializer 是 no 与 Json.NET 更兼容。要使用 Json.NET 进行序列化, 复制代码 https://github.com/restsharp/RestSharp/blob/86b31f9adf049d7fb821de8279154f41a17b36f7/RestSharp/Serializers/JsonSerializer.cs 并按照您的要求注册:

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

    然后你可以在客户端使用它:

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

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

    【讨论】:

      猜你喜欢
      • 2014-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 2021-05-18
      • 2020-03-05
      相关资源
      最近更新 更多