Can I set a custom JsonSerializer to RestSharp RestClient

跟風遠走 提交于 2019-12-10 10:55:57

问题


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



来源:https://stackoverflow.com/questions/43849892/can-i-set-a-custom-jsonserializer-to-restsharp-restclient

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!