diff --git a/RestSharp.Tests/JsonTests.cs b/RestSharp.Tests/JsonTests.cs index 17ee699e1..456af1992 100644 --- a/RestSharp.Tests/JsonTests.cs +++ b/RestSharp.Tests/JsonTests.cs @@ -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>(response); @@ -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(new RestResponse { Content = doc }); + + Assert.NotEmpty(output.EmployeesMail); + Assert.NotEmpty(output.EmployeesTime); + Assert.NotEmpty(output.EmployeesPay); + } + private string CreateJsonWithUnderscores() { var doc = new JsonObject(); diff --git a/RestSharp.Tests/RestSharp.Tests.csproj b/RestSharp.Tests/RestSharp.Tests.csproj index 2bfe13443..dc506f201 100644 --- a/RestSharp.Tests/RestSharp.Tests.csproj +++ b/RestSharp.Tests/RestSharp.Tests.csproj @@ -80,6 +80,7 @@ + @@ -120,6 +121,9 @@ Always + + Always + Always diff --git a/RestSharp.Tests/SampleClasses/EmployeeTracker.cs b/RestSharp.Tests/SampleClasses/EmployeeTracker.cs new file mode 100644 index 000000000..42c81b614 --- /dev/null +++ b/RestSharp.Tests/SampleClasses/EmployeeTracker.cs @@ -0,0 +1,40 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +namespace RestSharp.Tests.SampleClasses +{ + public class EmployeeTracker + { + /// + /// Key: Employee name. + /// Value: Messages sent to employee. + /// + public Dictionary> EmployeesMail { get; set; } + /// + /// Key: Employee name. + /// Value: Hours worked this each week. + /// + public Dictionary>> EmployeesTime { get; set; } + + /// + /// Key: Employee name. + /// Value: Payments made to employee + /// + public Dictionary> EmployeesPay { get; set; } + } + + public class Payment + { + public PaymentType Type { get; set; } + public Int32 Amount { get; set; } + } + + public enum PaymentType + { + Bonus, + Monthly, + BiWeekly + } +} diff --git a/RestSharp.Tests/SampleData/jsondictionary.txt b/RestSharp.Tests/SampleData/jsondictionary.txt new file mode 100644 index 000000000..5c4ebb76c --- /dev/null +++ b/RestSharp.Tests/SampleData/jsondictionary.txt @@ -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": [[]] + } +} \ No newline at end of file diff --git a/RestSharp/Deserializers/JsonDeserializer.cs b/RestSharp/Deserializers/JsonDeserializer.cs index 3c15305c6..3dff63860 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -100,7 +100,15 @@ private IDictionary BuildDictionary(Type type, object parent) foreach (var child in (IDictionary)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); }