From 16d0c090270cac72a74ed089c9186703952053a9 Mon Sep 17 00:00:00 2001 From: ravensorb Date: Wed, 1 Apr 2015 23:08:58 -0500 Subject: [PATCH 1/4] Fix for "inherited" interfaces --- .../Serialization/DependencyInjectionTests.cs | 18 +++++++++++++++--- .../Utilities/ReflectionUtils.cs | 16 ++++++++++++++-- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs index 7590a785..46dfca86 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs @@ -47,7 +47,12 @@ using NUnit.Framework; namespace Newtonsoft.Json.Tests.Serialization { - public interface ITaskRepository + public interface IBase + { + DateTime CreatedOn { get; set; } + } + + public interface ITaskRepository : IBase { string ConnectionString { get; set; } } @@ -58,7 +63,12 @@ namespace Newtonsoft.Json.Tests.Serialization string Level { get; set; } } - public class TaskRepository : ITaskRepository + public class Base : IBase + { + public DateTime CreatedOn { get; set; } + } + + public class TaskRepository : Base, ITaskRepository { public string ConnectionString { get; set; } } @@ -202,7 +212,8 @@ namespace Newtonsoft.Json.Tests.Serialization 'Level': 'Debug' }, 'Repository': { - 'ConnectionString': 'server=.' + 'ConnectionString': 'server=.', + 'CreatedOn': '2015-04-01 20:00' }, 'People': [ { @@ -223,6 +234,7 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.IsNotNull(o); Assert.IsNotNull(o.Logger); Assert.IsNotNull(o.Repository); + Assert.IsNotNull(o.Repository.CreatedOn); Assert.AreEqual(2, count); diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 6fd669fa..2ba6a6d8 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -883,8 +883,20 @@ namespace Newtonsoft.Json.Utilities { ValidationUtils.ArgumentNotNull(targetType, "targetType"); - List propertyInfos = new List(targetType.GetProperties(bindingAttr)); - GetChildPrivateProperties(propertyInfos, targetType, bindingAttr); + List propertyInfos; + + if (!targetType.IsInterface()) + { + propertyInfos = new List(targetType.GetProperties(bindingAttr)); + } + else + { + propertyInfos = (new Type[] { targetType }) + .Concat(targetType.GetInterfaces()) + .SelectMany(i => i.GetProperties()).ToList(); + } + + GetChildPrivateProperties(propertyInfos, targetType, bindingAttr); // a base class private getter/setter will be inaccessable unless the property was gotten from the base class for (int i = 0; i < propertyInfos.Count; i++) From b7315c926b3e6cebc0a1db69108aafdc2a9ce84b Mon Sep 17 00:00:00 2001 From: ravensorb Date: Wed, 1 Apr 2015 23:13:30 -0500 Subject: [PATCH 2/4] Added missing parameter to GetProperties call for nested interfaces --- Src/Newtonsoft.Json.sln.DotSettings | 2 ++ Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs | 2 +- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json.sln.DotSettings b/Src/Newtonsoft.Json.sln.DotSettings index c97ec47f..2c4af6be 100644 --- a/Src/Newtonsoft.Json.sln.DotSettings +++ b/Src/Newtonsoft.Json.sln.DotSettings @@ -2,5 +2,7 @@ False True False + True True + True \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 2ba6a6d8..c378a8a4 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -893,7 +893,7 @@ namespace Newtonsoft.Json.Utilities { propertyInfos = (new Type[] { targetType }) .Concat(targetType.GetInterfaces()) - .SelectMany(i => i.GetProperties()).ToList(); + .SelectMany(i => i.GetProperties(bindingAttr)).ToList(); } GetChildPrivateProperties(propertyInfos, targetType, bindingAttr); From 5b392b54b8904aff87408819f68a229c42e71afe Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 2 Apr 2015 20:28:01 +1300 Subject: [PATCH 3/4] -Clean up PR --- .../Serialization/DependencyInjectionTests.cs | 26 +++++++++---------- Src/Newtonsoft.Json.sln.DotSettings | 3 --- .../Utilities/ReflectionUtils.cs | 24 ++++++++--------- 3 files changed, 25 insertions(+), 28 deletions(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs index 46dfca86..7c31f9c7 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs @@ -47,10 +47,10 @@ using NUnit.Framework; namespace Newtonsoft.Json.Tests.Serialization { - public interface IBase - { - DateTime CreatedOn { get; set; } - } + public interface IBase + { + DateTime CreatedOn { get; set; } + } public interface ITaskRepository : IBase { @@ -63,10 +63,10 @@ namespace Newtonsoft.Json.Tests.Serialization string Level { get; set; } } - public class Base : IBase - { - public DateTime CreatedOn { get; set; } - } + public class Base : IBase + { + public DateTime CreatedOn { get; set; } + } public class TaskRepository : Base, ITaskRepository { @@ -213,7 +213,7 @@ namespace Newtonsoft.Json.Tests.Serialization }, 'Repository': { 'ConnectionString': 'server=.', - 'CreatedOn': '2015-04-01 20:00' + 'CreatedOn': '2015-04-01 20:00' }, 'People': [ { @@ -227,14 +227,14 @@ namespace Newtonsoft.Json.Tests.Serialization 'Name': 'Name3!' } }", new JsonSerializerSettings - { - ContractResolver = contractResolver - }); + { + ContractResolver = contractResolver + }); Assert.IsNotNull(o); Assert.IsNotNull(o.Logger); Assert.IsNotNull(o.Repository); - Assert.IsNotNull(o.Repository.CreatedOn); + Assert.IsNotNull(o.Repository.CreatedOn); Assert.AreEqual(2, count); diff --git a/Src/Newtonsoft.Json.sln.DotSettings b/Src/Newtonsoft.Json.sln.DotSettings index 2c4af6be..6a89a056 100644 --- a/Src/Newtonsoft.Json.sln.DotSettings +++ b/Src/Newtonsoft.Json.sln.DotSettings @@ -2,7 +2,4 @@ False True False - True - True - True \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index c378a8a4..ba73f174 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -883,20 +883,20 @@ namespace Newtonsoft.Json.Utilities { ValidationUtils.ArgumentNotNull(targetType, "targetType"); - List propertyInfos; + List propertyInfos; - if (!targetType.IsInterface()) - { - propertyInfos = new List(targetType.GetProperties(bindingAttr)); - } - else - { - propertyInfos = (new Type[] { targetType }) - .Concat(targetType.GetInterfaces()) - .SelectMany(i => i.GetProperties(bindingAttr)).ToList(); - } + if (!targetType.IsInterface()) + { + propertyInfos = new List(targetType.GetProperties(bindingAttr)); + } + else + { + propertyInfos = (new Type[] { targetType }) + .Concat(targetType.GetInterfaces()) + .SelectMany(i => i.GetProperties(bindingAttr)).ToList(); + } - GetChildPrivateProperties(propertyInfos, targetType, bindingAttr); + GetChildPrivateProperties(propertyInfos, targetType, bindingAttr); // a base class private getter/setter will be inaccessable unless the property was gotten from the base class for (int i = 0; i < propertyInfos.Count; i++) From 780cd8bd184ed113539343ed0a4cb0e4dc03046f Mon Sep 17 00:00:00 2001 From: ravensorb Date: Thu, 2 Apr 2015 05:52:38 -0500 Subject: [PATCH 4/4] Updated unit test to check for specific date value --- .../Serialization/DependencyInjectionTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs index 46dfca86..757451d4 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs @@ -234,7 +234,7 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.IsNotNull(o); Assert.IsNotNull(o.Logger); Assert.IsNotNull(o.Repository); - Assert.IsNotNull(o.Repository.CreatedOn); + Assert.AreEqual(o.Repository.CreatedOn, DateTime.Parse("2015-04-01 20:00")); Assert.AreEqual(2, count);