RestSharp XML Serializer in version 106.6

325 views
Skip to first unread message

Anders Hvidgaard Poder

unread,
Jan 16, 2019, 11:50:48 AM1/16/19
to RestSharp
I have discovered that in version 106.6 a new method has been added: public IRestClient UseSerializer(IRestSerializer serializer);

This method is undocumented, and return an IRestClient. According to the documentation on how to use custom serializers it is not mentioned, only:

var request = new RestRequest();
request.RequestFormat = DataFormat.Xml;
request.XmlSerializer = new SuperXmlSerializer(); // implements ISerializer
request.AddBody(person); // object serialized to XML using your custom serializer;

But this is no longer true, a call to UseSerializer must be added. What is the proper way to use custom serializers in the new version of RestSharp?

Alexey Zimarev

unread,
Jan 16, 2019, 4:30:15 PM1/16/19
to RestSharp
The old code should work as before with the latest version.

Concerning the "not documented" note, it is right there at the top of the readme

Anders Hvidgaard Poder

unread,
Jan 17, 2019, 2:50:22 AM1/17/19
to RestSharp
Thank you for the note and the link, that was exactly what I needed. 

However, I can say that it does not work as before. I am using the client against a WCF REST API, and for that I have previously found out that I need an XmlDataContractSerializer, which I have written and use. This class first implemented ISerializer and IDeserializer, but after updating to 106.5 (not 106.6, the one before that), it needed to implement IXmlSerializer, but as the interface was already implemented it was merely a matter of adding it and recompiling. This was due to a change to the RestRequest.XmlSerializer type. Once this was updated and compiled it worked as before.

When updating to version 106.x there was no need to change anything, just recompile, but the code:

client = new RestClient(host);
var request = new RestRequest(location.PathAndQuery, method);
request.XmlSerializer = new XmlDataContractSerializer();
request.RequestFormat = DataFormat.Xml;
client.AddHandler("text/xml", new XmlDataContractSerializer());
client.AddHandler("application/xml", new XmlDataContractSerializer());

stopped working when sending objects to the API. Receiving objects worked as before. 

I experimented with different solutions, and found that changing the XmlDataContractSerializer to implement IRestSerializer and adding UseSerializer, as below

client = new RestClient(host);
var request = new RestRequest(location.PathAndQuery, method);
request.XmlSerializer = new XmlDataContractSerializer();
request.RequestFormat = DataFormat.Xml;
client.AddHandler("text/xml", new XmlDataContractSerializer());
client.AddHandler("application/xml", new XmlDataContractSerializer());
client.UseSerializer(new XmlDataContractSerializer());

made the code work again. However, I have not been able to find any examples using UseSerializer, so whether the above, which works, is also the recommended way is the question. Also, the return value of UseSerializer is IRestClient? Can the client returned be different from the client supplied, or can the return value be ignored? And perhaps the UseDotNetXmlSerializer is what I should use instead of using my own - I will experiment with that. Then I just need to find out what to pass to AddHandler.

The example for using custom serialization I found here: https://github.com/restsharp/RestSharp/wiki/Deserialization 

Is there a better place for finding RestSharp documentation and examples? As for documentation, I meant in the code, the others have method description. 

From IRestClient:

.
.
.
        //
        // Summary:
        //     Removes custom deserialzier for the specified content type
        //
        // Parameters:
        //   contentType:
        //     Content type for which deserializer needs to be removed
        void RemoveHandler(string contentType);
        IRestClient UseSerializer(IRestSerializer serializer);


Thank you again for your note, it was very helpful.
Reply all
Reply to author
Forward
0 new messages