Skip to content

Commit faa3e42

Browse files
committed
updated JsonDeserializer to handle Dictionarys<String,List<T>> by just calling BuildList method which fixed confusion around list of lists.
Also updated the sample data to more clearly show lists vs list of lists
1 parent 07229be commit faa3e42

File tree

4 files changed

+14
-21
lines changed

4 files changed

+14
-21
lines changed

RestSharp.Tests/JsonTests.cs

Lines changed: 1 addition & 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);

RestSharp.Tests/SampleClasses/EmployeeTracker.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ public class EmployeeTracker
1414
public Dictionary<String, List<String>> EmployeesMail { get; set; }
1515
/// <summary>
1616
/// Key: Employee name.
17-
/// Value: Hours worked this week.
17+
/// Value: Hours worked this each week.
1818
/// </summary>
19-
public Dictionary<String, List<Int32>> EmployeesTime { get; set; }
19+
public Dictionary<String, List<List<Int32>>> EmployeesTime { get; set; }
2020

2121
/// <summary>
2222
/// Key: Employee name.
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
{
22
"EmployeesPay" :
33
{
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}]]
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}]
77
},
88
"EmployeesMail" :
99
{
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"]]
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"]
1313
},
1414

1515
"EmployeesTime" :
1616
{
17-
"John": [[8, 7, 8, 8, 8],[1,2,3]],
18-
"David": [4, 12, 6, 4],
17+
"John": [[8, 7, 8, 8, 8], [1, 2, 3]],
18+
"David": [[4, 12, 6, 4],[4, 12, 6, 4]],
1919
"Mary": [[]]
2020
}
2121
}

RestSharp/Deserializers/JsonDeserializer.cs

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -101,20 +101,13 @@ private IDictionary BuildDictionary(Type type, object parent)
101101
{
102102
var key = child.Key;
103103
object item = null;
104-
if(valueType.GetGenericTypeDefinition() != typeof(List<>))
104+
if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(List<>))
105105
{
106-
item = ConvertValue(valueType, child.Value);
106+
item = BuildList(valueType, child.Value);
107107
}
108108
else
109109
{
110-
item = (IList)Activator.CreateInstance(valueType);
111-
foreach (var jArray in (JsonArray)child.Value)
112-
{
113-
foreach (var element in (IList)ConvertValue(valueType, jArray))
114-
{
115-
((IList)item).Add(element);
116-
}
117-
}
110+
item = ConvertValue(valueType, child.Value);
118111
}
119112
dict.Add(key, item);
120113
}

0 commit comments

Comments
 (0)