From b215a4eefcce5e34fd0730ead0bb290248dd46dd Mon Sep 17 00:00:00 2001 From: DixonD-git Date: Sat, 22 Aug 2015 15:45:56 +0300 Subject: [PATCH 1/2] Fixed #730 --- RestSharp.Tests/JsonTests.cs | 26 ++++++++---- RestSharp.Tests/RestSharp.Tests.Signed.csproj | 3 ++ RestSharp.Tests/RestSharp.Tests.csproj | 3 ++ .../SampleData/jsondictionary_null.txt | 7 ++++ RestSharp/Deserializers/JsonDeserializer.cs | 42 ++++++++++--------- 5 files changed, 55 insertions(+), 26 deletions(-) create mode 100644 RestSharp.Tests/SampleData/jsondictionary_null.txt diff --git a/RestSharp.Tests/JsonTests.cs b/RestSharp.Tests/JsonTests.cs index bda88777c..4f9c952e3 100644 --- a/RestSharp.Tests/JsonTests.cs +++ b/RestSharp.Tests/JsonTests.cs @@ -12,19 +12,19 @@ // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and -// limitations under the License. +// limitations under the License. -#endregion +#endregion License +using NUnit.Framework; +using RestSharp.Deserializers; +using RestSharp.Tests.SampleClasses; using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; -using NUnit.Framework; -using RestSharp.Deserializers; -using RestSharp.Tests.SampleClasses; namespace RestSharp.Tests { @@ -195,7 +195,7 @@ public void Can_Deserialize_List_of_Guid() data["Ids"] = new JsonArray { id1, id2 }; JsonDeserializer d = new JsonDeserializer(); - RestResponse response = new RestResponse { Content = data.ToString() }; + RestResponse response = new RestResponse { Content = data.ToString() }; GuidList p = d.Deserialize(response); Assert.AreEqual(2, p.Ids.Count); @@ -780,6 +780,18 @@ public void Can_Deserialize_Dictionary_of_Lists() Assert.IsNotEmpty(output.EmployeesPay); } + [Test] + public void Can_Deserialize_Dictionary_with_Null() + { + string doc = File.ReadAllText(Path.Combine("SampleData", "jsondictionary_null.txt")); + JsonDeserializer json = new JsonDeserializer { RootElement = "response" }; + IDictionary output = json.Deserialize>(new RestResponse { Content = doc }); + + IDictionary dictionary = (IDictionary)output["SomeDictionary"]; + Assert.AreEqual("abra", dictionary["NonNull"]); + Assert.IsNull(dictionary["Null"]); + } + private static string CreateJsonWithUnderscores() { JsonObject doc = new JsonObject(); @@ -1013,4 +1025,4 @@ private static T GetPayLoad(string fileName) return d.Deserialize(response); } } -} +} \ No newline at end of file diff --git a/RestSharp.Tests/RestSharp.Tests.Signed.csproj b/RestSharp.Tests/RestSharp.Tests.Signed.csproj index 8aaafc768..ae944f44e 100644 --- a/RestSharp.Tests/RestSharp.Tests.Signed.csproj +++ b/RestSharp.Tests/RestSharp.Tests.Signed.csproj @@ -142,6 +142,9 @@ Always + + Always + Always diff --git a/RestSharp.Tests/RestSharp.Tests.csproj b/RestSharp.Tests/RestSharp.Tests.csproj index 2e08d17a6..0a55a43a9 100644 --- a/RestSharp.Tests/RestSharp.Tests.csproj +++ b/RestSharp.Tests/RestSharp.Tests.csproj @@ -129,6 +129,9 @@ Always + + Always + Always diff --git a/RestSharp.Tests/SampleData/jsondictionary_null.txt b/RestSharp.Tests/SampleData/jsondictionary_null.txt new file mode 100644 index 000000000..a9b1e36c0 --- /dev/null +++ b/RestSharp.Tests/SampleData/jsondictionary_null.txt @@ -0,0 +1,7 @@ +{ + "SomeDictionary" : + { + "NonNull": "abra", + "Null": null + } +} diff --git a/RestSharp/Deserializers/JsonDeserializer.cs b/RestSharp/Deserializers/JsonDeserializer.cs index ef773c9d9..9303636d2 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -1,10 +1,10 @@ -using System; +using RestSharp.Extensions; +using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; -using RestSharp.Extensions; namespace RestSharp.Deserializers { @@ -37,26 +37,26 @@ public T Deserialize(IRestResponse response) { object root = this.FindRoot(response.Content); - target = (T) this.BuildList(objType, root); + target = (T)this.BuildList(objType, root); } else { object data = SimpleJson.DeserializeObject(response.Content); - target = (T) this.BuildList(objType, data); + target = (T)this.BuildList(objType, data); } } else if (target is IDictionary) { object root = this.FindRoot(response.Content); - target = (T) this.BuildDictionary(target.GetType(), root); + target = (T)this.BuildDictionary(target.GetType(), root); } else { object root = this.FindRoot(response.Content); - target = (T) this.Map(target, (IDictionary) root); + target = (T)this.Map(target, (IDictionary)root); } return target; @@ -64,7 +64,7 @@ public T Deserialize(IRestResponse response) private object FindRoot(string content) { - IDictionary data = (IDictionary) SimpleJson.DeserializeObject(content); + IDictionary data = (IDictionary)SimpleJson.DeserializeObject(content); if (this.RootElement.HasValue() && data.ContainsKey(this.RootElement)) { @@ -89,7 +89,7 @@ private object Map(object target, IDictionary data) if (attributes.Length > 0) { - DeserializeAsAttribute attribute = (DeserializeAsAttribute) attributes[0]; + DeserializeAsAttribute attribute = (DeserializeAsAttribute)attributes[0]; name = attribute.Name; } else @@ -117,7 +117,7 @@ private object Map(object target, IDictionary data) } else { - currentData = (IDictionary) currentData[actualName]; + currentData = (IDictionary)currentData[actualName]; } } @@ -132,11 +132,11 @@ private object Map(object target, IDictionary data) private IDictionary BuildDictionary(Type type, object parent) { - IDictionary dict = (IDictionary) Activator.CreateInstance(type); + IDictionary dict = (IDictionary)Activator.CreateInstance(type); Type keyType = type.GetGenericArguments()[0]; Type valueType = type.GetGenericArguments()[1]; - foreach (KeyValuePair child in (IDictionary) parent) + foreach (KeyValuePair child in (IDictionary)parent) { object key = keyType != typeof(string) ? Convert.ChangeType(child.Key, keyType, CultureInfo.InvariantCulture) @@ -161,7 +161,7 @@ private IDictionary BuildDictionary(Type type, object parent) private IList BuildList(Type type, object parent) { - IList list = (IList) Activator.CreateInstance(type); + IList list = (IList)Activator.CreateInstance(type); Type listType = type.GetInterfaces() .First (x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList<>)); @@ -169,7 +169,7 @@ private IList BuildList(Type type, object parent) if (parent is IList) { - foreach (object element in (IList) parent) + foreach (object element in (IList)parent) { if (itemType.IsPrimitive) { @@ -225,8 +225,12 @@ private object ConvertValue(Type type, object value) type = type.GetGenericArguments()[0]; } - if (type == typeof(object) && value != null) + if (type == typeof(object)) { + if (value == null) + { + return null; + } type = value.GetType(); } @@ -272,14 +276,14 @@ private object ConvertValue(Type type, object value) if (type == typeof(DateTimeOffset)) { - return (DateTimeOffset) dt; + return (DateTimeOffset)dt; } } else if (type == typeof(decimal)) { if (value is double) { - return (decimal) ((double) value); + return (decimal)((double)value); } if (stringValue.Contains("e")) @@ -339,7 +343,7 @@ private object ConvertValue(Type type, object value) } else if (type == typeof(JsonObject)) { - // simplify JsonObject into a Dictionary + // simplify JsonObject into a Dictionary return this.BuildDictionary(typeof(Dictionary), value); } else @@ -355,9 +359,9 @@ private object CreateAndMap(Type type, object element) { object instance = Activator.CreateInstance(type); - this.Map(instance, (IDictionary) element); + this.Map(instance, (IDictionary)element); return instance; } } -} +} \ No newline at end of file From 98863df06fd6f62fa3cb308d8093c27284a7108f Mon Sep 17 00:00:00 2001 From: DixonD-git Date: Mon, 24 Aug 2015 21:13:06 +0300 Subject: [PATCH 2/2] Undo cosmetic changes --- RestSharp.Tests/JsonTests.cs | 15 +++++---- RestSharp/Deserializers/JsonDeserializer.cs | 36 ++++++++++----------- 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/RestSharp.Tests/JsonTests.cs b/RestSharp.Tests/JsonTests.cs index 4f9c952e3..c656b51fc 100644 --- a/RestSharp.Tests/JsonTests.cs +++ b/RestSharp.Tests/JsonTests.cs @@ -12,19 +12,19 @@ // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and -// limitations under the License. +// limitations under the License. -#endregion License +#endregion -using NUnit.Framework; -using RestSharp.Deserializers; -using RestSharp.Tests.SampleClasses; using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; +using NUnit.Framework; +using RestSharp.Deserializers; +using RestSharp.Tests.SampleClasses; namespace RestSharp.Tests { @@ -195,7 +195,7 @@ public void Can_Deserialize_List_of_Guid() data["Ids"] = new JsonArray { id1, id2 }; JsonDeserializer d = new JsonDeserializer(); - RestResponse response = new RestResponse { Content = data.ToString() }; + RestResponse response = new RestResponse { Content = data.ToString() }; GuidList p = d.Deserialize(response); Assert.AreEqual(2, p.Ids.Count); @@ -792,6 +792,7 @@ public void Can_Deserialize_Dictionary_with_Null() Assert.IsNull(dictionary["Null"]); } + private static string CreateJsonWithUnderscores() { JsonObject doc = new JsonObject(); @@ -1025,4 +1026,4 @@ private static T GetPayLoad(string fileName) return d.Deserialize(response); } } -} \ No newline at end of file +} diff --git a/RestSharp/Deserializers/JsonDeserializer.cs b/RestSharp/Deserializers/JsonDeserializer.cs index 9303636d2..ee85e1d76 100644 --- a/RestSharp/Deserializers/JsonDeserializer.cs +++ b/RestSharp/Deserializers/JsonDeserializer.cs @@ -1,10 +1,10 @@ -using RestSharp.Extensions; -using System; +using System; using System.Collections; using System.Collections.Generic; using System.Globalization; using System.Linq; using System.Reflection; +using RestSharp.Extensions; namespace RestSharp.Deserializers { @@ -37,26 +37,26 @@ public T Deserialize(IRestResponse response) { object root = this.FindRoot(response.Content); - target = (T)this.BuildList(objType, root); + target = (T) this.BuildList(objType, root); } else { object data = SimpleJson.DeserializeObject(response.Content); - target = (T)this.BuildList(objType, data); + target = (T) this.BuildList(objType, data); } } else if (target is IDictionary) { object root = this.FindRoot(response.Content); - target = (T)this.BuildDictionary(target.GetType(), root); + target = (T) this.BuildDictionary(target.GetType(), root); } else { object root = this.FindRoot(response.Content); - target = (T)this.Map(target, (IDictionary)root); + target = (T) this.Map(target, (IDictionary) root); } return target; @@ -64,7 +64,7 @@ public T Deserialize(IRestResponse response) private object FindRoot(string content) { - IDictionary data = (IDictionary)SimpleJson.DeserializeObject(content); + IDictionary data = (IDictionary) SimpleJson.DeserializeObject(content); if (this.RootElement.HasValue() && data.ContainsKey(this.RootElement)) { @@ -89,7 +89,7 @@ private object Map(object target, IDictionary data) if (attributes.Length > 0) { - DeserializeAsAttribute attribute = (DeserializeAsAttribute)attributes[0]; + DeserializeAsAttribute attribute = (DeserializeAsAttribute) attributes[0]; name = attribute.Name; } else @@ -117,7 +117,7 @@ private object Map(object target, IDictionary data) } else { - currentData = (IDictionary)currentData[actualName]; + currentData = (IDictionary) currentData[actualName]; } } @@ -132,11 +132,11 @@ private object Map(object target, IDictionary data) private IDictionary BuildDictionary(Type type, object parent) { - IDictionary dict = (IDictionary)Activator.CreateInstance(type); + IDictionary dict = (IDictionary) Activator.CreateInstance(type); Type keyType = type.GetGenericArguments()[0]; Type valueType = type.GetGenericArguments()[1]; - foreach (KeyValuePair child in (IDictionary)parent) + foreach (KeyValuePair child in (IDictionary) parent) { object key = keyType != typeof(string) ? Convert.ChangeType(child.Key, keyType, CultureInfo.InvariantCulture) @@ -161,7 +161,7 @@ private IDictionary BuildDictionary(Type type, object parent) private IList BuildList(Type type, object parent) { - IList list = (IList)Activator.CreateInstance(type); + IList list = (IList) Activator.CreateInstance(type); Type listType = type.GetInterfaces() .First (x => x.IsGenericType && x.GetGenericTypeDefinition() == typeof(IList<>)); @@ -169,7 +169,7 @@ private IList BuildList(Type type, object parent) if (parent is IList) { - foreach (object element in (IList)parent) + foreach (object element in (IList) parent) { if (itemType.IsPrimitive) { @@ -276,14 +276,14 @@ private object ConvertValue(Type type, object value) if (type == typeof(DateTimeOffset)) { - return (DateTimeOffset)dt; + return (DateTimeOffset) dt; } } else if (type == typeof(decimal)) { if (value is double) { - return (decimal)((double)value); + return (decimal) ((double) value); } if (stringValue.Contains("e")) @@ -343,7 +343,7 @@ private object ConvertValue(Type type, object value) } else if (type == typeof(JsonObject)) { - // simplify JsonObject into a Dictionary + // simplify JsonObject into a Dictionary return this.BuildDictionary(typeof(Dictionary), value); } else @@ -359,9 +359,9 @@ private object CreateAndMap(Type type, object element) { object instance = Activator.CreateInstance(type); - this.Map(instance, (IDictionary)element); + this.Map(instance, (IDictionary) element); return instance; } } -} \ No newline at end of file +}