From b28e7f80bfa17791f04806de83dda6e7feeed2e1 Mon Sep 17 00:00:00 2001 From: david grupp Date: Wed, 16 Oct 2013 17:23:20 -0500 Subject: [PATCH 1/5] added support for deserializing a dictionary's values to a list --- RestSharp/Deserializers/JsonDeserializer.cs | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/RestSharp/Deserializers/JsonDeserializer.cs b/RestSharp/Deserializers/JsonDeserializer.cs index 3c15305c6..391544e67 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -100,7 +100,18 @@ 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; + if(valueType.GetGenericTypeDefinition() != typeof(List<>)) + { + var item = ConvertValue(valueType, child.Value); + } + else + { + foreach (var element in (JsonArray)child.Value) + { + var item = ConvertValue(valueType, element); + } + } dict.Add(key, item); } From fa812cf0cca28623bb7a799ea343e255c19a3c80 Mon Sep 17 00:00:00 2001 From: david grupp Date: Wed, 16 Oct 2013 17:26:23 -0500 Subject: [PATCH 2/5] fixed build error in JsonDeserializer when building dictionaries --- RestSharp/Deserializers/JsonDeserializer.cs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/RestSharp/Deserializers/JsonDeserializer.cs b/RestSharp/Deserializers/JsonDeserializer.cs index 391544e67..d436f12f8 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -100,16 +100,16 @@ private IDictionary BuildDictionary(Type type, object parent) foreach (var child in (IDictionary)parent) { var key = child.Key; - object item; + object item = null; if(valueType.GetGenericTypeDefinition() != typeof(List<>)) { - var item = ConvertValue(valueType, child.Value); + item = ConvertValue(valueType, child.Value); } else { foreach (var element in (JsonArray)child.Value) { - var item = ConvertValue(valueType, element); + item = ConvertValue(valueType, element); } } dict.Add(key, item); From a13238ccf6f515ea6581184faa0b51eac480e060 Mon Sep 17 00:00:00 2001 From: david grupp Date: Thu, 17 Oct 2013 11:43:29 -0500 Subject: [PATCH 3/5] added unit test for deserializing a Dictionary> --- RestSharp.Tests/JsonTests.cs | 15 +++++++ RestSharp.Tests/RestSharp.Tests.csproj | 4 ++ .../SampleClasses/EmployeeTracker.cs | 40 +++++++++++++++++++ RestSharp.Tests/SampleData/jsondictionary.txt | 21 ++++++++++ 4 files changed, 80 insertions(+) create mode 100644 RestSharp.Tests/SampleClasses/EmployeeTracker.cs create mode 100644 RestSharp.Tests/SampleData/jsondictionary.txt diff --git a/RestSharp.Tests/JsonTests.cs b/RestSharp.Tests/JsonTests.cs index 17ee699e1..a87402799 100644 --- a/RestSharp.Tests/JsonTests.cs +++ b/RestSharp.Tests/JsonTests.cs @@ -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..437203e80 --- /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 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..79ad67458 --- /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], + "Mary": [[]] + } +} \ No newline at end of file From 07229beeaee512ea30c559eca2f3da34276fe3cc Mon Sep 17 00:00:00 2001 From: david grupp Date: Thu, 17 Oct 2013 11:44:12 -0500 Subject: [PATCH 4/5] fixed BuildDictionary in JsonDeserializer to add all elements from the JsonArrays --- RestSharp/Deserializers/JsonDeserializer.cs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/RestSharp/Deserializers/JsonDeserializer.cs b/RestSharp/Deserializers/JsonDeserializer.cs index d436f12f8..dd5b6358e 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -107,9 +107,13 @@ private IDictionary BuildDictionary(Type type, object parent) } else { - foreach (var element in (JsonArray)child.Value) + item = (IList)Activator.CreateInstance(valueType); + foreach (var jArray in (JsonArray)child.Value) { - item = ConvertValue(valueType, element); + foreach (var element in (IList)ConvertValue(valueType, jArray)) + { + ((IList)item).Add(element); + } } } dict.Add(key, item); From faa3e4224cb37b656757221882a7987b8126c5dc Mon Sep 17 00:00:00 2001 From: david grupp Date: Thu, 17 Oct 2013 18:47:30 -0500 Subject: [PATCH 5/5] updated JsonDeserializer to handle Dictionarys> 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 --- RestSharp.Tests/JsonTests.cs | 2 +- RestSharp.Tests/SampleClasses/EmployeeTracker.cs | 4 ++-- RestSharp.Tests/SampleData/jsondictionary.txt | 16 ++++++++-------- RestSharp/Deserializers/JsonDeserializer.cs | 13 +++---------- 4 files changed, 14 insertions(+), 21 deletions(-) diff --git a/RestSharp.Tests/JsonTests.cs b/RestSharp.Tests/JsonTests.cs index a87402799..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); diff --git a/RestSharp.Tests/SampleClasses/EmployeeTracker.cs b/RestSharp.Tests/SampleClasses/EmployeeTracker.cs index 437203e80..42c81b614 100644 --- a/RestSharp.Tests/SampleClasses/EmployeeTracker.cs +++ b/RestSharp.Tests/SampleClasses/EmployeeTracker.cs @@ -14,9 +14,9 @@ public class EmployeeTracker public Dictionary> EmployeesMail { get; set; } /// /// Key: Employee name. - /// Value: Hours worked this week. + /// Value: Hours worked this each week. /// - public Dictionary> EmployeesTime { get; set; } + public Dictionary>> EmployeesTime { get; set; } /// /// Key: Employee name. diff --git a/RestSharp.Tests/SampleData/jsondictionary.txt b/RestSharp.Tests/SampleData/jsondictionary.txt index 79ad67458..5c4ebb76c 100644 --- a/RestSharp.Tests/SampleData/jsondictionary.txt +++ b/RestSharp.Tests/SampleData/jsondictionary.txt @@ -1,21 +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}]] + "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"]] + "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], + "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 dd5b6358e..3dff63860 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -101,20 +101,13 @@ private IDictionary BuildDictionary(Type type, object parent) { var key = child.Key; object item = null; - if(valueType.GetGenericTypeDefinition() != typeof(List<>)) + if (valueType.IsGenericType && valueType.GetGenericTypeDefinition() == typeof(List<>)) { - item = ConvertValue(valueType, child.Value); + item = BuildList(valueType, child.Value); } else { - item = (IList)Activator.CreateInstance(valueType); - foreach (var jArray in (JsonArray)child.Value) - { - foreach (var element in (IList)ConvertValue(valueType, jArray)) - { - ((IList)item).Add(element); - } - } + item = ConvertValue(valueType, child.Value); } dict.Add(key, item); }