Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for deserializing arrays #457

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions RestSharp.IntegrationTests/RestSharp.IntegrationTests.csproj
Expand Up @@ -93,6 +93,9 @@
<Name>RestSharp.Net4</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Expand Down
4 changes: 4 additions & 0 deletions RestSharp.Tests/RestSharp.Tests.csproj
Expand Up @@ -81,6 +81,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="SampleClasses\EmployeeTracker.cs" />
<Compile Include="SampleClasses\FooWithArrayProperty.cs" />
<Compile Include="SampleClasses\GoogleWeatherWithAttributes.cs" />
<Compile Include="XmlAttributeDeserializerTests.cs" />
<Compile Include="CultureChange.cs" />
Expand Down Expand Up @@ -220,5 +221,8 @@
<Name>RestSharp</Name>
</ProjectReference>
</ItemGroup>
<ItemGroup>
<Service Include="{82A7F48D-3B50-4B1E-B82E-3ADA8210C358}" />
</ItemGroup>
<Import Project="$(SolutionDir)\.nuget\nuget.targets" />
</Project>
14 changes: 14 additions & 0 deletions RestSharp.Tests/SampleClasses/FooWithArrayProperty.cs
@@ -0,0 +1,14 @@
namespace RestSharp.Tests.SampleClasses
{
internal class FooWithArrayProperty
{
public string Name { get; set; }

public Id[] Ids { get; set; }
}

internal class Id
{
public int Value { get; set; }
}
}
29 changes: 29 additions & 0 deletions RestSharp.Tests/XmlDeserializerTests.cs
Expand Up @@ -646,6 +646,21 @@ public void Can_Deserialize_DateTimeOffset()
Assert.Equal(NullableDateTimeOffsetWithValue, payload.NullableDateTimeOffsetWithValue);
}

[Fact]
public void Can_Deserialize_Array()
{
var xml = CreateXmlWithArray();
var deserializer = new XmlDeserializer();

var result = deserializer.Deserialize<FooWithArrayProperty>(new RestResponse { Content = xml });

Assert.NotNull(result);
Assert.Equal("Foobar", result.Name);
Assert.Equal(2, result.Ids.Length);
Assert.Equal(1, result.Ids[0].Value);
Assert.Equal(2, result.Ids[1].Value);
}

private static string CreateUnderscoresXml()
{
var doc = new XDocument();
Expand Down Expand Up @@ -939,5 +954,19 @@ private static string CreateXmlWithAttributesAndNullValuesAndPopulatedValues()
return doc.ToString();
}

private static string CreateXmlWithArray()
{
var document = new XDocument();

document.Add(new XElement("FooWithArrayProperty",
new XElement("Name", "Foobar"),
new XElement("Ids",
new XElement("Id",
new XElement("Value", 1)),
new XElement("Id",
new XElement("Value", 2)))));

return document.ToString();
}
}
}
4 changes: 3 additions & 1 deletion RestSharp.sln
@@ -1,6 +1,8 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 2012
# Visual Studio 2013
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes to this file doesn't belong in this PR.

VisualStudioVersion = 12.0.21005.1
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestSharp", "RestSharp\RestSharp.csproj", "{2ECECFBF-5F3E-40EE-A963-72336DC7ABE2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RestSharp.Tests", "RestSharp.Tests\RestSharp.Tests.csproj", "{1464E4AC-18BB-4F23-8A0B-68196F9E1871}"
Expand Down
16 changes: 16 additions & 0 deletions RestSharp/Deserializers/XmlDeserializer.cs
Expand Up @@ -237,6 +237,22 @@ protected virtual void Map(object x, XElement root)
var list = HandleListDerivative(x, root, prop.Name, type);
prop.SetValue(x, list, null);
}
else if (type.IsArray)
{
// TODO: this solution is rather nasty(?). There should be a HandleArray method.
var listType = typeof(List<>);
var closedListType = listType.MakeGenericType(type.GetElementType());

var list = (IList)HandleListDerivative(x, root, prop.Name, closedListType);
var array = (Array)Activator.CreateInstance(type, new object[] { list.Count });

for (int i = 0; i < list.Count; i++)
{
array.SetValue(list[i], i);
}

prop.SetValue(x, array, null);
}
else
{
//fallback to type converters if possible
Expand Down