53

I have a string that comes out of a database which is in Json format.

I have tried to deserialize it with:

RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();
var x = deserial .Deserialize<Customer>(myStringFromDB)

But the .Deserialize function expects an IRestResponse

Is there a way to use RestSharp to just deserialize raw strings?

Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
Ian Vink
  • 66,960
  • 104
  • 341
  • 555

2 Answers2

73

There are sereval ways to do this. A very popular library to handle json is the Newtonsoft.Json. Probably you already have it on your asp.net project but if not, you could add it from nuget.

Considering you have a response object, include the following namespaces and call the static method DeserializeObject<T> from JsonConvert class:

using Newtonsoft.Json;
using RestSharp;
return JsonConvert.DeserializeObject<T>(response.Content);

On the response.Content, you will have the raw result, so just deserialize this string to a json object. The T in the case is the type you need to deserialize.

For example:

var customerDto = JsonConvert.DeserializeObject<CustomerDto>(response.Content);

Update

Recently, Microsoft has added a namespace System.Text.Json which handle json format on the .Net platform. You could use it calling the JsonSerializer.Deserialize<T> static method:

using System.Text.Json;
var customer = JsonSerializer.Deserialize<Customer>(jsonContent);
Felipe Oriani
  • 37,948
  • 19
  • 131
  • 194
  • 4
    The RestSharp serializer and deserializer was the problem. By using Newton soft the problem went away. – Ian Vink May 13 '13 at 22:29
  • 1
    @BahaiResearch.com It's not that RestSharp's serializer/deserializer has/is a problem per se; it just doesn't have the functionality you need, since RestSharp is an http client library not a general serialization tool. As you pointed out it requires an `IRestResponse`, as opposed to let's say supporting an IRestResponse.Content (string type) as well. StevieJ81 below points out a potential way workaround if for some reason you want to or must use RestSharp for json deserialization: he directly plugs IRestResponse.Content. – Matthew Dec 26 '15 at 20:00
  • Using the native `System.Text.Json` in .NET 6 LTS and it works wonderful. – k_rollo Feb 18 '23 at 08:58
58

If you want to avoid using extra libraries, try this:

RestSharp.RestResponse response = new RestSharp.RestResponse();

response.Content = myStringFromDB; 

RestSharp.Deserializers.JsonDeserializer deserial = new JsonDeserializer();

Customer x = deserial.Deserialize<Customer>(response);

Caveats apply - not extensively tested - but seems to work well enough.

StevieJ81
  • 745
  • 6
  • 12
  • Works great. One less library I have to include in my application. – Brad Bruce Feb 05 '17 at 15:56
  • 9
    In the version of RestSharp I'm using it looks like the JsonDeserializer class has been moved to RestSharp.Serialization.Json.JsonDeserializer – Michael Aug 16 '19 at 17:23
  • I don't think this is valid anymore, at least for version "100.6.10" – Esteban Verbel Oct 01 '19 at 20:44
  • 2
    In my case the RestSharp deserializer handles funky object names in the json content much better than the Newtonsoft deserializer, so that's another argument not to include another library just for this. – Alexis Leclerc Oct 25 '19 at 15:38