blgrnboy
blgrnboy

Reputation: 5167

RestSharp Deserialization not working

The below raw output (obtained from the RestResponse.Content Property) is not being deserialized. Is it because "ns1" is being added as a prefix? Is there something I am doing wrong?

Here is the raw JSON content returned when making a call:

{"ns1.model-response-list":{"@throttle":"2","@total-models":"3372","ns1.model-re sponses":{"ns1.model":[{"@mh":"0x20e800","ns1.attribute":{"@id":"0x1006e","$":"S servername.com"}},{"@mh":"0x21a400","ns1.attribute":{"@id":"0x100 6e","$":"servername.com"}}]},"ns1.link":{"@rel":"next","@href":" http://ipaddress/spectrum/restful/devices?id=93fc1a07-60be-4dd5-964c-7 e8660dd3028&start=2&throttlesize=2","@type":"application/xml"}}}

class Program
{
static void Main(string[] args)
{
var client = new RestClient(Spectrum.Endpoints.Development);
client.Authenticator = new HttpBasicAuthenticator("myid", "mypassword");

var request = new RestRequest(Method.GET);
request.RequestFormat = DataFormat.Xml;
request.Resource = "devices?{attr}&{throttlesize}";
request.AddParameter("attr", Spectrum.Attributes.ModelName);
request.AddParameter("throttlesize", "2");

IRestResponse<ModelResponseList> response = client.Execute<ModelResponseList>(request);

Console.Write(response.Data.Throttle); // This line keeps returning 0, but should return 2
}

Here are the classes which should hold the data:

[DeserializeAs(Name = "model-response-list")]
public class ModelResponseList
{
    [DeserializeAs(Name = "throttle")]
    public int Throttle { get; set; }

    [DeserializeAs(Name = "total-models")]
    public int TotalModels { get; set; }

    [DeserializeAs(Name = "model-responses")]
    public List<Model> ModelResponses { get; set; }

    [DeserializeAs(Name = "link")]
    public Link Link { get; set; }
}

public class Model
{
    public string Mh { get; set; }
    public ModelAttribute Attribute { get; set; }
}

public class ModelAttribute
{
    public string Id { get; set; }
    public string Value { get; set; }
}

public class Link
{
    public string Rel { get; set; }
    // Note! Href must be escaped, e.g. "&" => "&amp;" or comment this prop out
    public string Href { get; set; }
    public string Type { get; set; }
}

Upvotes: 3

Views: 3261

Answers (1)

Mikko Viitala
Mikko Viitala

Reputation: 8404

You should go and get Json.NET via NuGet and let it help you out. It's more sophisticated than RestSharp's built-in de/serialization.

Once you have Json.NET, then your JSON can be de/serialized using the classes below. This time I hope you are NOT removing the question after you receive an answer but accept and maybe upvote it instead?

So, using

var response = client.Execute(request);
var deserialized = JsonConvert.DeserializeObject<Wrapper>(response.Content);
Console.WriteLine(JsonConvert.SerializeObject(deserialized));

Outputs to console the following

{"ns1.model-response-list":{"@throttle":2,"@total-models":3372,"ns1.model-responses":{"ns1.model":[{"@mh":"0x20e800","ns1.attribute":{"@id":"0x1006e","$":"Sservername.com"}},{"@mh":"0x21a400","ns1.attribute":{"@id":"0x1006e","$":"servername.com"}}]},"ns1.link":{"@rel":"next","@href":" hxxp://ipaddress/spectrum/restful/devices?id=93fc1a07-60be-4dd5-964c-7 e8660dd3028&start=2&throttlesize=2","@type":"application/xml"}}}

If you use the classes below

[JsonObject]
public class Wrapper
{
    [JsonProperty(PropertyName = "ns1.model-response-list")]
    public ModelResponseList ModelResponseList { get; set; }
}

[JsonObject]
public class ModelResponseList
{
    [JsonProperty(PropertyName = "@throttle")]
    public int Throttle { get; set; }

    [JsonProperty(PropertyName = "@total-models")]
    public int TotalModels { get; set; }

    [JsonProperty(PropertyName = "ns1.model-responses")]
    public Responses ModelResponses { get; set; }

    [JsonProperty(PropertyName = "ns1.link")]
    public Link Link { get; set; }
}

[JsonObject]
public class Responses
{
    [JsonProperty(PropertyName = "ns1.model")]
    public List<Model> Model { get; set; }
}

[JsonObject]
public class Model
{
    [JsonProperty(PropertyName = "@mh")]
    public object Mh { get; set; }

    [JsonProperty(PropertyName = "ns1.attribute")]
    public ModelAttribute Attribute { get; set; }
}

[JsonObject]
public class ModelAttribute
{
    [JsonProperty(PropertyName = "@id")]
    public string Id { get; set; }

    [JsonProperty(PropertyName = "$")]
    public string Value { get; set; }
}

[JsonObject]
public class Link
{
    [JsonProperty(PropertyName = "@rel")]
    public string Rel { get; set; }

    [JsonProperty(PropertyName = "@href")]
    public string Href { get; set; }
    
    [JsonProperty(PropertyName = "@type")]
    public string Type { get; set; }
}

Upvotes: 3

Related Questions