Deserializing List<T>

62 views
Skip to first unread message

David Ortinau

unread,
Oct 16, 2010, 2:42:35 PM10/16/10
to RestSharp
Hey all, I'm hoping this'll be a stupid question and you can nudge me
in the right direction.

The json being returned by my Rails site wraps each object, so all my
values are 1 level deeper than the deserializer expects. A simplified
example:

[{"paper": {"permalink": null,"title": "Sample Title"} },{"paper":
{"permalink": null,"title": "Sample Title"} }]

I have a Paper class:

public class Paper
{
public string permalink { get; set; }
public string title { get; set; }
}

And then in my service class I'm calling:

client.ExecuteAsync<List<Paper>>(request, (response) =>
{
if (response.ResponseStatus == ResponseStatus.Error)
{
failure(response.ErrorMessage);
}
else
{
success(response.Data);
}
});

When the json is being deserialized, at Line 84 of JsonDeserializer.cs
it tries:

var value = json[name];

but my values are actually at json["paper"][name]

Is there a property akin to RootElement I should be setting to get
this to deserialize correctly? Do I need to write a custom
deserializer, and if so how do I tell the JToken to look one level
deeper? In this case it's "paper" but I have other resources that'll
have the same situation, so I can't just throw "paper" in there.

I knew my C# was rusty, but only now am I realizing just how much!

Thanks for giving this a look. Any and all thoughts welcome!

Dave

prabir

unread,
Oct 16, 2010, 2:54:26 PM10/16/10
to RestSharp
can u try this if it works.

[DataContract]
public class PaperCollection
{
[DataMember(Name="paper")]
public List<Paper> Paper { get; set;}
}

[DataContract]
public clas Paper
{
[DataMember(Name="permalink")]
public string PermaLink {get;set;}

[DataMember(Name="title")]
public string Title {get;set;}
}

client.ExecuteAsync<PaperCollection>(request,response=> ... );

prabir

unread,
Oct 16, 2010, 3:04:19 PM10/16/10
to RestSharp
oh a simple error (tried without the compiler), should be this
instead. i tested it also.

[DataContract]
public class PaperNode
{
[DataMember(Name="paper")]
public Paper Paper { get; set;}
}

[DataContract]
public class Paper
{
[DataMember(Name="permalink")]
public string PermaLink {get;set;}

[DataMember(Name="title")]
public string Title {get;set;}
}

var s = "[{\"paper\": {\"permalink\": \"p1\",\"title\": \"Sample
Title\"} },{\"paper\":{\"permalink\": null,\"title\": \"Sample Title
\"} }]";

var x =
Newtonsoft.Json.JsonConvert.DeserializeObject<List<PaperNode>>(s);

that means bascially it should be
client.ExecuteAsync<List<PaperNode>>(>(request,response=> ... );
> > Dave- Hide quoted text -
>
> - Show quoted text -

John Sheehan

unread,
Oct 16, 2010, 3:57:04 PM10/16/10
to rest...@googlegroups.com
This JSON is actually a list of objects that all have a 'paper'
property. So you'd have to do something like this to add another
layer:

class Result {
public Paper { get; set; }
}

then do client.Execute<List<Result>>(request);

Try that and let me know if it works.

johnsheehan

unread,
Oct 16, 2010, 4:06:12 PM10/16/10
to RestSharp
make sure you do 'public class Result', I was being lazy with my
pseudo code.

David Ortinau

unread,
Oct 16, 2010, 4:18:46 PM10/16/10
to RestSharp
Ok, I'll verify that implementation works when I get back in front if
the computer.

I'd tried similar to that, but without the metadata decoration.

Dave

John Sheehan

unread,
Oct 16, 2010, 4:48:58 PM10/16/10
to rest...@googlegroups.com
You don't need any meta data decoration unless you're going to write
your own deserializer or take the raw response and send it to
JSON.NET. The default deserializer doesn't use attributes.

David Ortinau

unread,
Oct 16, 2010, 8:29:36 PM10/16/10
to RestSharp
Thanks, wrapping another class around Paper does the trick.
Reply all
Reply to author
Forward
0 new messages