From a2e15adf801b59042d2f45495ddca06ce5ccfc0a Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Tue, 23 Dec 2014 10:57:17 +0000 Subject: [PATCH] -Added AttributeProvider -Increament version to 7.0.1 --- Build/Newtonsoft.Json.nuspec | 2 +- Build/build.ps1 | 7 +- Src/Newtonsoft.Json.TestConsole/Program.cs | 27 ++++- .../Properties/AssemblyInfo.cs | 30 ++++- .../Newtonsoft.Json.Tests.Net20.csproj | 3 + .../Newtonsoft.Json.Tests.Net35.csproj | 3 + .../Newtonsoft.Json.Tests.Net40.csproj | 3 + .../Newtonsoft.Json.Tests.Portable.csproj | 3 + .../Newtonsoft.Json.Tests.Portable40.csproj | 3 + .../Newtonsoft.Json.Tests.csproj | 1 + .../Properties/AssemblyInfo.cs | 4 +- .../Serialization/ContractResolverTests.cs | 12 ++ .../Serialization/DependencyInjectionTests.cs | 2 +- .../Serialization/DynamicConcreteTests.cs | 5 +- .../ImmutableCollectionsTests.cs | 1 + .../ReflectionAttributeProviderTests.cs | 105 ++++++++++++++++++ Src/Newtonsoft.Json/Linq/JToken.cs | 3 - .../Newtonsoft.Json.Net20.csproj | 2 + .../Newtonsoft.Json.Net35.csproj | 2 + .../Newtonsoft.Json.Net40.csproj | 2 + .../Newtonsoft.Json.Portable.csproj | 2 + .../Newtonsoft.Json.Portable40.csproj | 2 + Src/Newtonsoft.Json/Newtonsoft.Json.csproj | 2 + .../Properties/AssemblyInfo.cs | 4 +- .../Serialization/DefaultContractResolver.cs | 2 + .../Serialization/IAttributeProvider.cs | 51 +++++++++ .../Serialization/JsonProperty.cs | 7 ++ .../ReflectionAttributeProvider.cs | 70 ++++++++++++ .../Utilities/ReflectionUtils.cs | 58 ++++++++-- 29 files changed, 389 insertions(+), 29 deletions(-) create mode 100644 Src/Newtonsoft.Json.Tests/Serialization/ReflectionAttributeProviderTests.cs create mode 100644 Src/Newtonsoft.Json/Serialization/IAttributeProvider.cs create mode 100644 Src/Newtonsoft.Json/Serialization/ReflectionAttributeProvider.cs diff --git a/Build/Newtonsoft.Json.nuspec b/Build/Newtonsoft.Json.nuspec index 35e47ca5..094705d7 100644 --- a/Build/Newtonsoft.Json.nuspec +++ b/Build/Newtonsoft.Json.nuspec @@ -2,7 +2,7 @@ Newtonsoft.Json - 6.0.7 + 7.0.1-beta1 Json.NET Json.NET is a popular high-performance JSON framework for .NET James Newton-King diff --git a/Build/build.ps1 b/Build/build.ps1 index 04df4f4e..7d488120 100644 --- a/Build/build.ps1 +++ b/Build/build.ps1 @@ -1,7 +1,7 @@ properties { - $zipFileName = "Json60r7.zip" - $majorVersion = "6.0" - $majorWithReleaseVersion = "6.0.7" + $zipFileName = "Json70r1.zip" + $majorVersion = "7.0" + $majorWithReleaseVersion = "7.0.1" $version = GetVersion $majorWithReleaseVersion $signAssemblies = $false $signKeyPath = "C:\Development\Releases\newtonsoft.snk" @@ -22,7 +22,6 @@ @{Name = "Newtonsoft.Json.Portable"; TestsName = "Newtonsoft.Json.Tests.Portable"; TestsFunction = "NUnitTests"; Constants="PORTABLE"; FinalDir="Portable"; NuGetDir = "portable-net45+wp80+win8+wpa81+aspnetcore50"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Portable40"; TestsName = "Newtonsoft.Json.Tests.Portable40"; TestsFunction = "NUnitTests"; Constants="PORTABLE40"; FinalDir="Portable40"; NuGetDir = "portable-net40+sl5+wp80+win8+wpa81"; Framework="net-4.0"; Sign=$true}, #@{Name = $null; TestsName = "Newtonsoft.Json.Tests.AspNetCore50"; TestsFunction = "CoreClrTests"; Constants="ASPNETCORE50"; FinalDir="ASPNETCORE50"; NuGetDir = $null; Framework=$null; Sign=$null}, - @{Name = "Newtonsoft.Json.WinRT"; TestsName = "Newtonsoft.Json.Tests.WinRT"; TestsFunction = "WinRTTests"; Constants="NETFX_CORE"; FinalDir="WinRT"; NuGetDir = "netcore45"; Framework="net-4.5"; Sign=$true}, @{Name = "Newtonsoft.Json.Net40"; TestsName = "Newtonsoft.Json.Tests.Net40"; TestsFunction = "NUnitTests"; Constants="NET40"; FinalDir="Net40"; NuGetDir = "net40"; Framework="net-4.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Net35"; TestsName = "Newtonsoft.Json.Tests.Net35"; TestsFunction = "NUnitTests"; Constants="NET35"; FinalDir="Net35"; NuGetDir = "net35"; Framework="net-2.0"; Sign=$true}, @{Name = "Newtonsoft.Json.Net20"; TestsName = "Newtonsoft.Json.Tests.Net20"; TestsFunction = "NUnitTests"; Constants="NET20"; FinalDir="Net20"; NuGetDir = "net20"; Framework="net-2.0"; Sign=$true} diff --git a/Src/Newtonsoft.Json.TestConsole/Program.cs b/Src/Newtonsoft.Json.TestConsole/Program.cs index 42e3546f..2e43b374 100644 --- a/Src/Newtonsoft.Json.TestConsole/Program.cs +++ b/Src/Newtonsoft.Json.TestConsole/Program.cs @@ -1,4 +1,29 @@ -using System; +#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.Diagnostics; using System.IO; diff --git a/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs index 9133187c..cdf2801c 100644 --- a/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.TestConsole/Properties/AssemblyInfo.cs @@ -1,3 +1,28 @@ +#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.Reflection; using System.Runtime.CompilerServices; using System.Runtime.InteropServices; @@ -31,6 +56,5 @@ using System.Runtime.InteropServices; // // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: -// [assembly: AssemblyVersion("6.0.0.0")] -[assembly: AssemblyVersion("6.0.0.0")] -[assembly: AssemblyFileVersion("6.0.7.18023")] +[assembly: AssemblyVersion("7.0.0.0")] +[assembly: AssemblyFileVersion("7.0.1.18023")] diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj index abc23174..de98d464 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net20.csproj @@ -101,6 +101,8 @@ + + @@ -110,6 +112,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net35.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net35.csproj index beef4ced..91f56cfa 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net35.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net35.csproj @@ -143,6 +143,8 @@ + + @@ -152,6 +154,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.csproj index a6e955a4..bd655768 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Net40.csproj @@ -265,6 +265,8 @@ + + @@ -272,6 +274,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj index c60697b4..e5247c29 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable.csproj @@ -144,6 +144,8 @@ + + @@ -151,6 +153,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj index d583459c..2bbaca60 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.Portable40.csproj @@ -145,6 +145,8 @@ + + @@ -152,6 +154,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj index 3b0234b0..fe518cc8 100644 --- a/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj +++ b/Src/Newtonsoft.Json.Tests/Newtonsoft.Json.Tests.csproj @@ -261,6 +261,7 @@ + diff --git a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs index 7d62e73e..ad393cfe 100644 --- a/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json.Tests/Properties/AssemblyInfo.cs @@ -75,5 +75,5 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("6.0.0.0")] -[assembly: AssemblyFileVersion("6.0.7.18023")] +[assembly: AssemblyVersion("7.0.0.0")] +[assembly: AssemblyFileVersion("7.0.1.18023")] diff --git a/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs index 0084d018..c0308997 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/ContractResolverTests.cs @@ -25,6 +25,7 @@ using System; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Runtime.Serialization; #if NETFX_CORE @@ -155,6 +156,17 @@ namespace Newtonsoft.Json.Tests.Serialization Assert.IsNotNull(contract.DefaultCreator); } + [Test] + public void PropertyAttributeProvider() + { + var resolver = new DefaultContractResolver(); + var contract = (JsonObjectContract)resolver.ResolveContract(typeof(Invoice)); + + JsonProperty property = contract.Properties["FollowUpDays"]; + Assert.AreEqual(1, property.AttributeProvider.GetAttributes(false).Count); + Assert.AreEqual(typeof(DefaultValueAttribute), property.AttributeProvider.GetAttributes(false)[0].GetType()); + } + [Test] public void AbstractTestClass() { diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs index 2eab44b2..9b8bd106 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/DependencyInjectionTests.cs @@ -23,7 +23,7 @@ // OTHER DEALINGS IN THE SOFTWARE. #endregion -#if !(NET35 || NET20 || ASPNETCORE50) +#if !(NET40 || NET35 || NET20 || ASPNETCORE50 || PORTABLE || PORTABLE40) using Autofac; using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Tests.TestObjects; diff --git a/Src/Newtonsoft.Json.Tests/Serialization/DynamicConcreteTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/DynamicConcreteTests.cs index de688f32..f9505689 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/DynamicConcreteTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/DynamicConcreteTests.cs @@ -29,8 +29,11 @@ using System.Reflection.Emit; using Newtonsoft.Json.Serialization; using Newtonsoft.Json.Tests.TestObjects; using System; +#if NET20 +using Newtonsoft.Json.Utilities.LinqBridge; +#else using System.Linq; - +#endif #if NETFX_CORE using Microsoft.VisualStudio.TestPlatform.UnitTestFramework; using TestFixture = Microsoft.VisualStudio.TestPlatform.UnitTestFramework.TestClassAttribute; diff --git a/Src/Newtonsoft.Json.Tests/Serialization/ImmutableCollectionsTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/ImmutableCollectionsTests.cs index 58a5ddaa..26525659 100644 --- a/Src/Newtonsoft.Json.Tests/Serialization/ImmutableCollectionsTests.cs +++ b/Src/Newtonsoft.Json.Tests/Serialization/ImmutableCollectionsTests.cs @@ -46,6 +46,7 @@ using Newtonsoft.Json.Linq; namespace Newtonsoft.Json.Tests.Serialization { [TestFixture] + [Ignore] public class ImmutableCollectionsTests : TestFixtureBase { #region List diff --git a/Src/Newtonsoft.Json.Tests/Serialization/ReflectionAttributeProviderTests.cs b/Src/Newtonsoft.Json.Tests/Serialization/ReflectionAttributeProviderTests.cs new file mode 100644 index 00000000..26411e53 --- /dev/null +++ b/Src/Newtonsoft.Json.Tests/Serialization/ReflectionAttributeProviderTests.cs @@ -0,0 +1,105 @@ +#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.ComponentModel; +using System.Reflection; +using System.Runtime.Serialization; +using System.Text; +using Newtonsoft.Json.Serialization; +using Newtonsoft.Json.Tests.TestObjects; +using NUnit.Framework; + +namespace Newtonsoft.Json.Tests.Serialization +{ + [TestFixture] + public class ReflectionAttributeProviderTests : TestFixtureBase + { + public class ReflectionTestObject + { + [DefaultValue("1")] + [JsonProperty] + public int TestProperty { get; set; } + + [DefaultValue("1")] + [JsonProperty] + public int TestField; + + public ReflectionTestObject( + [DefaultValue("1")] + [JsonProperty] + int testParameter) + { + TestProperty = testParameter; + TestField = testParameter; + } + } + + [Test] + public void GetAttributes_Property() + { + PropertyInfo property = typeof(ReflectionTestObject).GetProperty("TestProperty"); + + ReflectionAttributeProvider provider = new ReflectionAttributeProvider(property); + + IList attributes = provider.GetAttributes(typeof(DefaultValueAttribute), false); + Assert.AreEqual(1, attributes.Count); + + attributes = provider.GetAttributes(false); + Assert.AreEqual(2, attributes.Count); + } + + [Test] + public void GetAttributes_Field() + { + FieldInfo field = typeof(ReflectionTestObject).GetField("TestField"); + + ReflectionAttributeProvider provider = new ReflectionAttributeProvider(field); + + IList attributes = provider.GetAttributes(typeof(DefaultValueAttribute), false); + Assert.AreEqual(1, attributes.Count); + + attributes = provider.GetAttributes(false); + Assert.AreEqual(2, attributes.Count); + } + + [Test] + public void GetAttributes_Parameter() + { + ParameterInfo[] parameters = typeof(ReflectionTestObject).GetConstructor(new[] { typeof(int) }).GetParameters(); + + ParameterInfo parameter = parameters[0]; + + ReflectionAttributeProvider provider = new ReflectionAttributeProvider(parameter); + + IList attributes = provider.GetAttributes(typeof(DefaultValueAttribute), false); + Assert.AreEqual(1, attributes.Count); + + attributes = provider.GetAttributes(false); + Assert.AreEqual(2, attributes.Count); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Linq/JToken.cs b/Src/Newtonsoft.Json/Linq/JToken.cs index 93442011..b3f4df01 100644 --- a/Src/Newtonsoft.Json/Linq/JToken.cs +++ b/Src/Newtonsoft.Json/Linq/JToken.cs @@ -66,9 +66,6 @@ namespace Newtonsoft.Json.Linq private JToken _next; private object _annotations; - //private int? _lineNumber; - //private int? _linePosition; - private static readonly JTokenType[] BooleanTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean }; private static readonly JTokenType[] NumberTypes = new[] { JTokenType.Integer, JTokenType.Float, JTokenType.String, JTokenType.Comment, JTokenType.Raw, JTokenType.Boolean }; #if !(NET20 || NET35 || PORTABLE40 || PORTABLE) diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj index 4131e63f..0590b6fe 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net20.csproj @@ -123,10 +123,12 @@ + + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj index 05d5c555..b5942172 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net35.csproj @@ -135,9 +135,11 @@ + + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj index da1dd081..70c7dfd2 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Net40.csproj @@ -135,9 +135,11 @@ + + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj index 2136d184..4aefa945 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable.csproj @@ -166,6 +166,7 @@ + @@ -191,6 +192,7 @@ + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj index c1194aa9..7b849766 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.Portable40.csproj @@ -171,6 +171,7 @@ + @@ -196,6 +197,7 @@ + diff --git a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj index 2bfacf80..c34a33ee 100644 --- a/Src/Newtonsoft.Json/Newtonsoft.Json.csproj +++ b/Src/Newtonsoft.Json/Newtonsoft.Json.csproj @@ -170,6 +170,7 @@ + @@ -195,6 +196,7 @@ + diff --git a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs index 3fdc4b0b..f4964748 100644 --- a/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs +++ b/Src/Newtonsoft.Json/Properties/AssemblyInfo.cs @@ -91,6 +91,6 @@ using System.Security; // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("6.0.0.0")] -[assembly: AssemblyFileVersion("6.0.7.18023")] +[assembly: AssemblyVersion("7.0.0.0")] +[assembly: AssemblyFileVersion("7.0.1.18023")] [assembly: CLSCompliant(true)] diff --git a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs index f9a4433c..36aadf43 100644 --- a/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs +++ b/Src/Newtonsoft.Json/Serialization/DefaultContractResolver.cs @@ -640,6 +640,7 @@ namespace Newtonsoft.Json.Serialization { JsonProperty property = new JsonProperty(); property.PropertyType = parameterInfo.ParameterType; + property.AttributeProvider = new ReflectionAttributeProvider(parameterInfo); bool allowNonPublicAccess; SetPropertySettingsFromAttributes(property, parameterInfo, parameterInfo.Name, parameterInfo.Member.DeclaringType, MemberSerialization.OptOut, out allowNonPublicAccess); @@ -1167,6 +1168,7 @@ namespace Newtonsoft.Json.Serialization property.PropertyType = ReflectionUtils.GetMemberUnderlyingType(member); property.DeclaringType = member.DeclaringType; property.ValueProvider = CreateMemberValueProvider(member); + property.AttributeProvider = new ReflectionAttributeProvider(member); bool allowNonPublicAccess; SetPropertySettingsFromAttributes(property, member, member.Name, member.DeclaringType, memberSerialization, out allowNonPublicAccess); diff --git a/Src/Newtonsoft.Json/Serialization/IAttributeProvider.cs b/Src/Newtonsoft.Json/Serialization/IAttributeProvider.cs new file mode 100644 index 00000000..97a64693 --- /dev/null +++ b/Src/Newtonsoft.Json/Serialization/IAttributeProvider.cs @@ -0,0 +1,51 @@ +#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; + +namespace Newtonsoft.Json.Serialization +{ + /// + /// Provides methods to get attributes. + /// + public interface IAttributeProvider + { + /// + /// Returns a collection of all of the attributes, or an empty collection if there are no attributes. + /// + /// When true, look up the hierarchy chain for the inherited custom attribute. + /// A collection of s, or an empty collection. + IList GetAttributes(bool inherit); + + /// + /// Returns a collection of attributes, identified by type, or an empty collection if there are no attributes. + /// + /// The type of the attributes. + /// When true, look up the hierarchy chain for the inherited custom attribute. + /// A collection of s, or an empty collection. + IList GetAttributes(Type attributeType, bool inherit); + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs index 604dc347..45c5e38d 100644 --- a/Src/Newtonsoft.Json/Serialization/JsonProperty.cs +++ b/Src/Newtonsoft.Json/Serialization/JsonProperty.cs @@ -24,6 +24,7 @@ #endregion using System; +using System.Reflection; using Newtonsoft.Json.Utilities; #if NET20 @@ -87,6 +88,12 @@ namespace Newtonsoft.Json.Serialization /// The that will get and set the during serialization. public IValueProvider ValueProvider { get; set; } + /// + /// Gets or sets the for this property. + /// + /// The for this property. + public IAttributeProvider AttributeProvider { get; set; } + /// /// Gets or sets the type of the property. /// diff --git a/Src/Newtonsoft.Json/Serialization/ReflectionAttributeProvider.cs b/Src/Newtonsoft.Json/Serialization/ReflectionAttributeProvider.cs new file mode 100644 index 00000000..c70c32fd --- /dev/null +++ b/Src/Newtonsoft.Json/Serialization/ReflectionAttributeProvider.cs @@ -0,0 +1,70 @@ +#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.Reflection; +using Newtonsoft.Json.Utilities; + +namespace Newtonsoft.Json.Serialization +{ + /// + /// Provides methods to get attributes from a , , or . + /// + public class ReflectionAttributeProvider : IAttributeProvider + { + private readonly object _attributeProvider; + + /// + /// Initializes a new instance of the class. + /// + public ReflectionAttributeProvider(object attributeProvider) + { + ValidationUtils.ArgumentNotNull(attributeProvider, "attributeProvider"); + _attributeProvider = attributeProvider; + } + + /// + /// Returns a collection of all of the attributes, or an empty collection if there are no attributes. + /// + /// When true, look up the hierarchy chain for the inherited custom attribute. + /// A collection of s, or an empty collection. + public IList GetAttributes(bool inherit) + { + return ReflectionUtils.GetAttributes(_attributeProvider, null, inherit); + } + + /// + /// Returns a collection of attributes, identified by type, or an empty collection if there are no attributes. + /// + /// The type of the attributes. + /// When true, look up the hierarchy chain for the inherited custom attribute. + /// A collection of s, or an empty collection. + public IList GetAttributes(Type attributeType, bool inherit) + { + return ReflectionUtils.GetAttributes(_attributeProvider, attributeType, inherit); + } + } +} \ No newline at end of file diff --git a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs index 2de8dca3..6714ba4f 100644 --- a/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs +++ b/Src/Newtonsoft.Json/Utilities/ReflectionUtils.cs @@ -683,24 +683,43 @@ namespace Newtonsoft.Json.Utilities // ICustomAttributeProvider doesn't do inheritance if (provider is Type) - return (Attribute[])((Type)provider).GetCustomAttributes(attributeType, inherit); + { + Type t = (Type)provider; + object[] attributes = (attributeType != null) ? t.GetCustomAttributes(attributeType, inherit) : t.GetCustomAttributes(inherit); + return (Attribute[])attributes; + } if (provider is Assembly) - return Attribute.GetCustomAttributes((Assembly)provider, attributeType); + { + Assembly a = (Assembly)provider; + return (attributeType != null) ? Attribute.GetCustomAttributes(a, attributeType) : Attribute.GetCustomAttributes(a); + } if (provider is MemberInfo) - return Attribute.GetCustomAttributes((MemberInfo)provider, attributeType, inherit); + { + MemberInfo m = (MemberInfo)provider; + return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit); + } #if !PORTABLE40 if (provider is Module) - return Attribute.GetCustomAttributes((Module)provider, attributeType, inherit); + { + Module m = (Module)provider; + return (attributeType != null) ? Attribute.GetCustomAttributes(m, attributeType, inherit) : Attribute.GetCustomAttributes(m, inherit); + } #endif if (provider is ParameterInfo) - return Attribute.GetCustomAttributes((ParameterInfo)provider, attributeType, inherit); + { + ParameterInfo p = (ParameterInfo)provider; + return (attributeType != null) ? Attribute.GetCustomAttributes(p, attributeType, inherit) : Attribute.GetCustomAttributes(p, inherit); + } #if !PORTABLE40 - return (Attribute[])((ICustomAttributeProvider)attributeProvider).GetCustomAttributes(attributeType, inherit); + ICustomAttributeProvider customAttributeProvider = (ICustomAttributeProvider)attributeProvider; + object[] result = (attributeType != null) ? customAttributeProvider.GetCustomAttributes(attributeType, inherit) : customAttributeProvider.GetCustomAttributes(inherit); + + return (Attribute[])result; #else throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider)); #endif @@ -714,19 +733,36 @@ namespace Newtonsoft.Json.Utilities public static Attribute[] GetAttributes(object provider, Type attributeType, bool inherit) { if (provider is Type) - return ((Type) provider).GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray(); + { + Type t = (Type)provider; + return (attributeType != null) + ? t.GetTypeInfo().GetCustomAttributes(attributeType, inherit).ToArray() + : t.GetTypeInfo().GetCustomAttributes(inherit).ToArray(); + } if (provider is Assembly) - return ((Assembly) provider).GetCustomAttributes(attributeType).ToArray(); + { + Assembly a = (Assembly)provider; + return (attributeType != null) ? a.GetCustomAttributes(attributeType).ToArray() : a.GetCustomAttributes().ToArray(); + } if (provider is MemberInfo) - return ((MemberInfo) provider).GetCustomAttributes(attributeType, inherit).ToArray(); + { + MemberInfo m = (MemberInfo)provider; + return (attributeType != null) ? m.GetCustomAttributes(attributeType, inherit).ToArray() : m.GetCustomAttributes(inherit).ToArray(); + } if (provider is Module) - return ((Module) provider).GetCustomAttributes(attributeType).ToArray(); + { + Module m = (Module)provider; + return (attributeType != null) ? m.GetCustomAttributes(attributeType).ToArray() : m.GetCustomAttributes().ToArray(); + } if (provider is ParameterInfo) - return ((ParameterInfo) provider).GetCustomAttributes(attributeType, inherit).ToArray(); + { + ParameterInfo p = (ParameterInfo)provider; + return (attributeType != null) ? p.GetCustomAttributes(attributeType, inherit).ToArray() : p.GetCustomAttributes(inherit).ToArray(); + } throw new Exception("Cannot get attributes from '{0}'.".FormatWith(CultureInfo.InvariantCulture, provider)); }