我可以将自定义JsonSerializer设置为RestSharp RestClient吗? [英] Can I set a custom JsonSerializer to RestSharp RestClient

查看:628
本文介绍了我可以将自定义JsonSerializer设置为RestSharp RestClient吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();

但是我读过的文件确实存在,我可以将其直接添加到restclient中:

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

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

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();

var client = new RestClient();

client.JsonSerializer = new YourCustomSerializer();

client.JsonSerializer = new YourCustomSerializer();

我在RestClient类中找不到此属性...

自述文件是否有错误?或软件包没有更新? 有没有其他方法可以添加自定义序列化程序,而不是将其添加到每个请求中?

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?

推荐答案

自述文件中似乎有一个错误.

There was a mistake in the Readme, it seems.

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

这是应该如何使用:

有一个重大变化:默认的Json Serializer 为no 与Json.NET不再兼容.要使用Json.NET进行序列化, 从复制代码 https://github.com/restsharp/RestSharp/blob 86b31f9adf049d7fb821de8279154f41a17b36f7/RestSharp/Serializers/JsonSerializer.cs 并根据您的请求进行注册:

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

这篇关于我可以将自定义JsonSerializer设置为RestSharp RestClient吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆