From 06d92a8963ac73dbbf346f9a9ee0dcbbc3d370cb Mon Sep 17 00:00:00 2001 From: JamesNK Date: Thu, 9 Feb 2012 11:11:23 +0000 Subject: -Fixed not replacing ignored properties in JsonPropertyCollection --- .../Newtonsoft.Json.Tests.Net20.csproj | 6 +++ .../Newtonsoft.Json.Tests.Silverlight.csproj | 6 +++ .../Newtonsoft.Json.Tests.WindowsPhone.csproj | 6 +++ .../Newtonsoft.Json.Tests.csproj | 6 +++ .../Properties/AssemblyInfo.cs | 2 +- .../Serialization/JsonPropertyCollectionTests.cs | 57 ++++++++++++++++++++++ .../TestObjects/IPrivateImplementationA.cs | 32 ++++++++++++ .../TestObjects/IPrivateImplementationB.cs | 32 ++++++++++++ .../IPrivateOverriddenImplementation.cs | 32 ++++++++++++ .../TestObjects/PrivateImplementationAClass.cs | 40 +++++++++++++++ .../TestObjects/PrivateImplementationBClass.cs | 57 ++++++++++++++++++++++ Src/Newtonsoft.Json/Properties/AssemblyInfo.cs | 2 +- .../Serialization/JsonPropertyCollection.cs | 12 +++-- 13 files changed, 283 insertions(+), 7 deletions(-) create mode 100644 Src/Newtonsoft.Json.Tests/Serialization/JsonPropertyCollectionTests.cs create mode 100644 Src/Newtonsoft.Json.Tests/TestObjects/IPrivateImplementationA.cs create mode 100644 Src/Newtonsoft.Json.Tests/TestObjects/IPrivateImplementationB.cs create mode 100644 Src/Newtonsoft.Json.Tests/TestObjects/IPrivateOverriddenImplementation.cs create mode 100644 Src/Newtonsoft.Json.Tests/TestObjects/PrivateImplementationAClass.cs create mode 100644 Src/Newtonsoft.Json.Tests/TestObjects/PrivateImplementationBClass.cs diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj index 190081a..135fa42 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj @@ -92,6 +92,7 @@ + @@ -107,6 +108,9 @@ + + + @@ -121,6 +125,8 @@ + + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj index 4d24a80..25d36f2 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Silverlight.csproj @@ -129,6 +129,7 @@ + @@ -170,6 +171,9 @@ + + + @@ -193,6 +197,8 @@ + + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.WindowsPhone.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.WindowsPhone.csproj index 5357dbc..78fe5a6 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.WindowsPhone.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.WindowsPhone.csproj @@ -92,6 +92,7 @@ + @@ -133,6 +134,9 @@ + + + @@ -156,6 +160,8 @@ + + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index bbd3840..2a87afd 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -143,6 +143,7 @@ + @@ -153,6 +154,9 @@ + + + @@ -183,6 +187,8 @@ + + diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index ac320ef..032490a 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -66,5 +66,5 @@ using System.Runtime.InteropServices; // by using the '*' as shown below: [assembly: AssemblyVersion("4.0.7.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.0.7.14603")] +[assembly: AssemblyFileVersion("4.0.7.14610")] #endif diff --git a/Src/Newtonsoft.Json.Tests/Serialization/JsonPropertyCollectionTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/JsonPropertyCollectionTests.cs new file mode 100644 index 0000000..b2a6782 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Serialization/JsonPropertyCollectionTests.cs @@ -0,0 +1,57 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using NUnit.Framework; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Tests.TestObjects; + +namespace Newtonsoft.Json.Tests.Serialization +{ + public class JsonPropertyCollectionTests : TestFixtureBase + { + [Test] + public void AddPropertyIncludesPrivateImplementations() + { + var value = new PrivateImplementationBClass + { + OverriddenProperty = "OverriddenProperty", + PropertyA = "PropertyA", + PropertyB = "PropertyB" + }; + + var resolver = new DefaultContractResolver(); + var contract = (JsonObjectContract) resolver.ResolveContract(value.GetType()); + + Assert.AreEqual(3, contract.Properties.Count); + Assert.IsTrue(contract.Properties.Contains("OverriddenProperty"), "Contract is missing property 'OverriddenProperty'"); + Assert.IsTrue(contract.Properties.Contains("PropertyA"), "Contract is missing property 'PropertyA'"); + Assert.IsTrue(contract.Properties.Contains("PropertyB"), "Contract is missing property 'PropertyB'"); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/IPrivateImplementationA.cs b/Src/Newtonsoft.Json.Tests/TestObjects/IPrivateImplementationA.cs new file mode 100644 index 0000000..ed68dfe --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/IPrivateImplementationA.cs @@ -0,0 +1,32 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +namespace Newtonsoft.Json.Tests.TestObjects +{ + public interface IPrivateImplementationA + { + string PropertyA { get; set; } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/IPrivateImplementationB.cs b/Src/Newtonsoft.Json.Tests/TestObjects/IPrivateImplementationB.cs new file mode 100644 index 0000000..8455549 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/IPrivateImplementationB.cs @@ -0,0 +1,32 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +namespace Newtonsoft.Json.Tests.TestObjects +{ + public interface IPrivateImplementationB + { + string PropertyB { get; set; } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/IPrivateOverriddenImplementation.cs b/Src/Newtonsoft.Json.Tests/TestObjects/IPrivateOverriddenImplementation.cs new file mode 100644 index 0000000..e4c361d --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/IPrivateOverriddenImplementation.cs @@ -0,0 +1,32 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +namespace Newtonsoft.Json.Tests.TestObjects +{ + public interface IPrivateOverriddenImplementation + { + object OverriddenProperty { get; set; } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/PrivateImplementationAClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/PrivateImplementationAClass.cs new file mode 100644 index 0000000..4baa43b --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/PrivateImplementationAClass.cs @@ -0,0 +1,40 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +namespace Newtonsoft.Json.Tests.TestObjects +{ + public class PrivateImplementationAClass : IPrivateImplementationA + { + [JsonIgnore] + public string PropertyA { get; set; } + + [JsonProperty("PropertyA")] + string IPrivateImplementationA.PropertyA + { + get { return this.PropertyA; } + set { this.PropertyA = value; } + } + } +} diff --git a/Src/Newtonsoft.Json.Tests/TestObjects/PrivateImplementationBClass.cs b/Src/Newtonsoft.Json.Tests/TestObjects/PrivateImplementationBClass.cs new file mode 100644 index 0000000..4b8ef53 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/TestObjects/PrivateImplementationBClass.cs @@ -0,0 +1,57 @@ +#region License +// Copyright (c) 2007 James Newton-King +// +// Permission is hereby granted, free of charge, to any person +// obtaining a copy of this software and associated documentation +// files (the "Software"), to deal in the Software without +// restriction, including without limitation the rights to use, +// copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following +// conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR +// OTHER DEALINGS IN THE SOFTWARE. +#endregion + +namespace Newtonsoft.Json.Tests.TestObjects +{ + public class PrivateImplementationBClass : PrivateImplementationAClass, IPrivateImplementationB, IPrivateOverriddenImplementation + { + [JsonIgnore] + public string PropertyB { get; set; } + + [JsonProperty("PropertyB")] + string IPrivateImplementationB.PropertyB + { + get { return this.PropertyB; } + set { this.PropertyB = value; } + } + + [JsonProperty("OverriddenProperty")] + private string OverriddenPropertyString + { + get { return this.OverriddenProperty.ToString(); } + set { this.OverriddenProperty = value; } + } + + [JsonIgnore] + public object OverriddenProperty { get; set; } + + [JsonIgnore] + object IPrivateOverriddenImplementation.OverriddenProperty + { + get { return this.OverriddenProperty; } + set { this.OverriddenProperty = value; } + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index b8363ff..877c5cc 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -85,7 +85,7 @@ using System.Security; // by using the '*' as shown below: [assembly: AssemblyVersion("4.0.7.0")] #if !PocketPC -[assembly: AssemblyFileVersion("4.0.7.14603")] +[assembly: AssemblyFileVersion("4.0.7.14610")] #endif [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs index 6274659..167e55f 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonPropertyCollection.cs @@ -43,7 +43,8 @@ namespace Newtonsoft.Json.Serialization /// Initializes a new instance of the class. /// /// The type. - public JsonPropertyCollection(Type type) : base(StringComparer.Ordinal) + public JsonPropertyCollection(Type type) + : base(StringComparer.Ordinal) { ValidationUtils.ArgumentNotNull(type, "type"); _type = type; @@ -72,12 +73,13 @@ namespace Newtonsoft.Json.Serialization return; JsonProperty existingProperty = this[property.PropertyName]; + bool duplicateProperty = true; if (existingProperty.Ignored) { // remove ignored property so it can be replaced in collection Remove(existingProperty); - return; + duplicateProperty = false; } if (property.DeclaringType != null && existingProperty.DeclaringType != null) @@ -86,7 +88,7 @@ namespace Newtonsoft.Json.Serialization { // current property is on a derived class and hides the existing Remove(existingProperty); - return; + duplicateProperty = false; } if (existingProperty.DeclaringType.IsSubclassOf(property.DeclaringType)) { @@ -95,8 +97,8 @@ namespace Newtonsoft.Json.Serialization } } - throw new JsonSerializationException( - "A member with the name '{0}' already exists on '{1}'. Use the JsonPropertyAttribute to specify another name.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName, _type)); + if (duplicateProperty) + throw new JsonSerializationException("A member with the name '{0}' already exists on '{1}'. Use the JsonPropertyAttribute to specify another name.".FormatWith(CultureInfo.InvariantCulture, property.PropertyName, _type)); } Add(property); -- cgit v1.2.3