Skip to content

Commit a13238c

Browse files
committed
added unit test for deserializing a Dictionary<String,List<T>>
1 parent fa812cf commit a13238c

File tree

4 files changed

+80
-0
lines changed

4 files changed

+80
-0
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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 week.
18+
/// </summary>
19+
public Dictionary<String, 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],
19+
"Mary": [[]]
20+
}
21+
}

0 commit comments

Comments
 (0)