Skip to content

Commit 85d4693

Browse files
committed
Add support for deserializing JSON to subclasses of List<T>. Closes pull
request #236.
1 parent abc0d76 commit 85d4693

File tree

3 files changed

+16
-1
lines changed

3 files changed

+16
-1
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,16 @@ public void Can_Deserialize_Root_Json_Array_To_List()
193193
Assert.Equal(4, output.Count);
194194
}
195195

196+
[Fact]
197+
public void Can_Deserialize_Root_Json_Array_To_Inherited_List()
198+
{
199+
var data = File.ReadAllText(Path.Combine("SampleData", "jsonarray.txt"));
200+
var response = new RestResponse { Content = data };
201+
var json = new JsonDeserializer();
202+
var output = json.Deserialize<StatusList>(response);
203+
Assert.Equal(4, output.Count);
204+
}
205+
196206
[Fact]
197207
public void Can_Deserialize_Various_Enum_Values ()
198208
{

RestSharp.Tests/SampleClasses/twitter.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,8 @@ public class user
5353
public int utc_offset { get; set; }
5454

5555
}
56+
57+
public class StatusList : List<status>
58+
{
59+
}
5660
}

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,8 @@ private IDictionary BuildDictionary(Type type, object parent)
198198
private IList BuildList(Type type, object parent)
199199
{
200200
var list = (IList)Activator.CreateInstance(type);
201-
var itemType = type.GetGenericArguments()[0];
201+
var listType = type.GetInterfaces().First(x => x.GetGenericTypeDefinition() == typeof(IList<>));
202+
var itemType = listType.GetGenericArguments()[0];
202203

203204
if (parent is IList) {
204205
foreach (var element in (IList)parent) {

0 commit comments

Comments
 (0)