Skip to content

Commit d834b72

Browse files
committed
Merge pull request #439 from davidgrupp/master
Added support to JsonDeserializer for deserializing a Dictionary to a Dictionary<String,List<T>>
2 parents 9ca7557 + faa3e42 commit d834b72

File tree

5 files changed

+90
-2
lines changed

5 files changed

+90
-2
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -620,7 +620,7 @@ public void Can_Deserialize_Nullable_DateTimeOffset_With_Null()
620620
[Fact]
621621
public void Can_Deserialize_To_Dictionary_String_String()
622622
{
623-
var doc = CreateJsonStringDictionary();
623+
var doc = CreateJsonStringDictionary();
624624
var d = new JsonDeserializer();
625625
var response = new RestResponse { Content = doc };
626626
var bd = d.Deserialize<Dictionary<string,string>>(response);
@@ -664,6 +664,21 @@ public void Can_Deserialize_Object_Type_Property_With_Primitive_Vale()
664664
Assert.Equal(42L, payload.ObjectProperty);
665665
}
666666

667+
[Fact]
668+
public void Can_Deserialize_Dictionary_of_Lists()
669+
{
670+
var doc = File.ReadAllText(Path.Combine("SampleData", "jsondictionary.txt"));
671+
672+
var json = new JsonDeserializer();
673+
json.RootElement = "response";
674+
675+
var output = json.Deserialize<EmployeeTracker>(new RestResponse { Content = doc });
676+
677+
Assert.NotEmpty(output.EmployeesMail);
678+
Assert.NotEmpty(output.EmployeesTime);
679+
Assert.NotEmpty(output.EmployeesPay);
680+
}
681+
667682
private string CreateJsonWithUnderscores()
668683
{
669684
var doc = new JsonObject();

RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
</Reference>
8181
</ItemGroup>
8282
<ItemGroup>
83+
<Compile Include="SampleClasses\EmployeeTracker.cs" />
8384
<Compile Include="SampleClasses\GoogleWeatherWithAttributes.cs" />
8485
<Compile Include="XmlAttributeDeserializerTests.cs" />
8586
<Compile Include="CultureChange.cs" />
@@ -120,6 +121,9 @@
120121
<Content Include="SampleData\iso8601datetimes.txt">
121122
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
122123
</Content>
124+
<Content Include="SampleData\jsondictionary.txt">
125+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
126+
</Content>
123127
<Content Include="SampleData\objectproperty.txt">
124128
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
125129
</Content>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace RestSharp.Tests.SampleClasses
7+
{
8+
public class EmployeeTracker
9+
{
10+
/// <summary>
11+
/// Key: Employee name.
12+
/// Value: Messages sent to employee.
13+
/// </summary>
14+
public Dictionary<String, List<String>> EmployeesMail { get; set; }
15+
/// <summary>
16+
/// Key: Employee name.
17+
/// Value: Hours worked this each week.
18+
/// </summary>
19+
public Dictionary<String, List<List<Int32>>> EmployeesTime { get; set; }
20+
21+
/// <summary>
22+
/// Key: Employee name.
23+
/// Value: Payments made to employee
24+
/// </summary>
25+
public Dictionary<String, List<Payment>> EmployeesPay { get; set; }
26+
}
27+
28+
public class Payment
29+
{
30+
public PaymentType Type { get; set; }
31+
public Int32 Amount { get; set; }
32+
}
33+
34+
public enum PaymentType
35+
{
36+
Bonus,
37+
Monthly,
38+
BiWeekly
39+
}
40+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"EmployeesPay" :
3+
{
4+
"John": [{"Type":"BiWeekly","Amount":3000},{"Type":"Bonus","Amount":5000}],
5+
"David": [{"Type":"Monthly","Amount":5000},{"Type":"Bonus","Amount":2500}],
6+
"Mary": [{"Type":"BiWeekly","Amount":2000}]
7+
},
8+
"EmployeesMail" :
9+
{
10+
"John": ["Welcome to Restsharp", "Meetings at 4pm", "Meeting Cancled"],
11+
"David": ["Project deadline is Monday", "Good work"],
12+
"Mary": ["Is there any documentation on Product A", "I'm leaving early today"]
13+
},
14+
15+
"EmployeesTime" :
16+
{
17+
"John": [[8, 7, 8, 8, 8], [1, 2, 3]],
18+
"David": [[4, 12, 6, 4],[4, 12, 6, 4]],
19+
"Mary": [[]]
20+
}
21+
}

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,15 @@ private IDictionary BuildDictionary(Type type, object parent)
100100
foreach (var child in (IDictionary<string, object>)parent)
101101
{
102102
var key = child.Key;
103-
var item = ConvertValue(valueType, child.Value);
103+
object item = null;
104+
if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(List<>))
105+
{
106+
item = BuildList(valueType, child.Value);
107+
}
108+
else
109+
{
110+
item = ConvertValue(valueType, child.Value);
111+
}
104112
dict.Add(key, item);
105113
}
106114

0 commit comments

Comments
 (0)