Skip to content

Commit d1f5216

Browse files
committed
Merge pull request restsharp#766 from adback03/master
JsonDeserializer support for IEnumerable
2 parents a9a2b8a + e8e2ce7 commit d1f5216

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,17 @@ public void Can_Deserialize_4sq_Json_With_Root_Element_Specified()
8181
Assert.IsNotEmpty(output.Groups);
8282
}
8383

84+
[Test]
85+
public void Can_Deserialize_IEnumerable_of_Simple_Types()
86+
{
87+
const string content = "{\"numbers\":[1,2,3,4,5]}";
88+
JsonDeserializer json = new JsonDeserializer { RootElement = "numbers" };
89+
var output = json.Deserialize<IEnumerable<int>>(new RestResponse { Content = content });
90+
91+
Assert.IsNotEmpty(output);
92+
Assert.IsTrue(output.Count() == 5);
93+
}
94+
8495
[Test]
8596
public void Can_Deserialize_Lists_of_Simple_Types()
8697
{

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,13 @@ private object ConvertValue(Type type, object value)
333333
{
334334
Type genericTypeDef = type.GetGenericTypeDefinition();
335335

336+
if (genericTypeDef == typeof(IEnumerable<>))
337+
{
338+
Type itemType = type.GetGenericArguments()[0];
339+
Type listType = typeof(List<>).MakeGenericType(itemType);
340+
return this.BuildList(listType, value);
341+
}
342+
336343
if (genericTypeDef == typeof(List<>))
337344
{
338345
return this.BuildList(type, value);

0 commit comments

Comments
 (0)