Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion RestSharp.Tests/JsonTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -620,7 +620,7 @@ public void Can_Deserialize_Nullable_DateTimeOffset_With_Null()
[Fact]
public void Can_Deserialize_To_Dictionary_String_String()
{
var doc = CreateJsonStringDictionary();
var doc = CreateJsonStringDictionary();
var d = new JsonDeserializer();
var response = new RestResponse { Content = doc };
var bd = d.Deserialize<Dictionary<string,string>>(response);
Expand Down Expand Up @@ -664,6 +664,21 @@ public void Can_Deserialize_Object_Type_Property_With_Primitive_Vale()
Assert.Equal(42L, payload.ObjectProperty);
}

[Fact]
public void Can_Deserialize_Dictionary_of_Lists()
{
var doc = File.ReadAllText(Path.Combine("SampleData", "jsondictionary.txt"));

var json = new JsonDeserializer();
json.RootElement = "response";

var output = json.Deserialize<EmployeeTracker>(new RestResponse { Content = doc });

Assert.NotEmpty(output.EmployeesMail);
Assert.NotEmpty(output.EmployeesTime);
Assert.NotEmpty(output.EmployeesPay);
}

private string CreateJsonWithUnderscores()
{
var doc = new JsonObject();
Expand Down
4 changes: 4 additions & 0 deletions RestSharp.Tests/RestSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="SampleClasses\EmployeeTracker.cs" />
<Compile Include="SampleClasses\GoogleWeatherWithAttributes.cs" />
<Compile Include="XmlAttributeDeserializerTests.cs" />
<Compile Include="CultureChange.cs" />
Expand Down Expand Up @@ -120,6 +121,9 @@
<Content Include="SampleData\iso8601datetimes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="SampleData\jsondictionary.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="SampleData\objectproperty.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
40 changes: 40 additions & 0 deletions RestSharp.Tests/SampleClasses/EmployeeTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RestSharp.Tests.SampleClasses
{
public class EmployeeTracker
{
/// <summary>
/// Key: Employee name.
/// Value: Messages sent to employee.
/// </summary>
public Dictionary<String, List<String>> EmployeesMail { get; set; }
/// <summary>
/// Key: Employee name.
/// Value: Hours worked this each week.
/// </summary>
public Dictionary<String, List<List<Int32>>> EmployeesTime { get; set; }

/// <summary>
/// Key: Employee name.
/// Value: Payments made to employee
/// </summary>
public Dictionary<String, List<Payment>> EmployeesPay { get; set; }
}

public class Payment
{
public PaymentType Type { get; set; }
public Int32 Amount { get; set; }
}

public enum PaymentType
{
Bonus,
Monthly,
BiWeekly
}
}
21 changes: 21 additions & 0 deletions RestSharp.Tests/SampleData/jsondictionary.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"EmployeesPay" :
{
"John": [{"Type":"BiWeekly","Amount":3000},{"Type":"Bonus","Amount":5000}],
"David": [{"Type":"Monthly","Amount":5000},{"Type":"Bonus","Amount":2500}],
"Mary": [{"Type":"BiWeekly","Amount":2000}]
},
"EmployeesMail" :
{
"John": ["Welcome to Restsharp", "Meetings at 4pm", "Meeting Cancled"],
"David": ["Project deadline is Monday", "Good work"],
"Mary": ["Is there any documentation on Product A", "I'm leaving early today"]
},

"EmployeesTime" :
{
"John": [[8, 7, 8, 8, 8], [1, 2, 3]],
"David": [[4, 12, 6, 4],[4, 12, 6, 4]],
"Mary": [[]]
}
}
10 changes: 9 additions & 1 deletion RestSharp/Deserializers/JsonDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,15 @@ private IDictionary BuildDictionary(Type type, object parent)
foreach (var child in (IDictionary<string, object>)parent)
{
var key = child.Key;
var item = ConvertValue(valueType, child.Value);
object item = null;
if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(List<>))
{
item = BuildList(valueType, child.Value);
}
else
{
item = ConvertValue(valueType, child.Value);
}
dict.Add(key, item);
}

Expand Down