Over a million developers have joined DZone.

Deserializing POC Objects From Strings Using RestSharp

DZone's Guide to

Deserializing POC Objects From Strings Using RestSharp

· ·
Free Resource
RestSharp is a wonderful little library for communicating with REST services. There is one thing I like in particular – their forgiving deserialization classes. They are awesome, but they cannot deserialize plain strings. If you take a closer look at the Deserialize methods, you will notice that they accept an object of the class that implements IRestResponse. If you inspect the source code (it is open source over at github), you will see that only Content property is used.

So, let’s implement our FakeResponse class that will wrap our string:

public class FakeResponse : IRestResponse
    public string Content { get; set; }

    // default implementation is OK

You can now deserialize it with the following snippet (xml holds the XML text read from some source other than web response):

var xmlDeserializer = new RestSharp.Deserializers.XmlDeserializer();
var rss = xmlDeserializer.Deserialize<Data.Rss>(new FakeResponse() { Content = xml });

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}