Skip to content

Commit e8e2ce7

Browse files
committed
JsonDeserializer support for IEnumerable
Bug fix for JsonDeserialize to support type of IEnumerable. If the given property is IEnumerable, create a new list using the same generic argument.
1 parent 0ef81fd commit e8e2ce7

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
@@ -294,6 +294,13 @@ private object ConvertValue(Type type, object value)
294294
{
295295
Type genericTypeDef = type.GetGenericTypeDefinition();
296296

297+
if (genericTypeDef == typeof(IEnumerable<>))
298+
{
299+
Type itemType = type.GetGenericArguments()[0];
300+
Type listType = typeof(List<>).MakeGenericType(itemType);
301+
return this.BuildList(listType, value);
302+
}
303+
297304
if (genericTypeDef == typeof(List<>))
298305
{
299306
return this.BuildList(type, value);

0 commit comments

Comments
 (0)