From 7e7ce182cad7e3672f00127fb4291079aacf811c Mon Sep 17 00:00:00 2001 From: yfakariya Date: Sat, 27 Dec 2014 03:25:20 +0900 Subject: [PATCH] Add MessagePackIgnoreAttribute to exclude properties. Issue #55 --- src/MsgPack.Net35/MsgPack.Net35.csproj | 3 + src/MsgPack.Net40/MsgPack.Net40.csproj | 3 + .../MsgPack.Silverlight.5.csproj | 3 + .../MsgPack.Silverlight.WindowsPhone.csproj | 3 + src/MsgPack.Unity3D/MsgPack.Unity3D.csproj | 3 + .../MsgPack.WinRT.Portable.csproj | 3 + .../MsgPack.Xamarin.Android.csproj | 3 + .../MsgPack.Xamarin.iOS.csproj | 3 + src/MsgPack/MsgPack.csproj | 1 + .../MessagePackIgnoreAttribute.cs | 39 ++++++ .../Serialization/SerializationTarget.cs | 39 +++++- ...deDomBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ ...deDomBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ ...ntextBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ ...ssionBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ ...FieldBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ ...ctionBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ .../AutoMessagePackSerializerTest.ttinclude | 123 ++++++++++++++++++ ...ntextBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ ...ssionBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ ...FieldBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ ...ctionBasedAutoMessagePackSerializerTest.cs | 116 +++++++++++++++++ 22 files changed, 1380 insertions(+), 6 deletions(-) create mode 100644 src/MsgPack/Serialization/MessagePackIgnoreAttribute.cs diff --git a/src/MsgPack.Net35/MsgPack.Net35.csproj b/src/MsgPack.Net35/MsgPack.Net35.csproj index bd38d56a6..932aaa3a3 100644 --- a/src/MsgPack.Net35/MsgPack.Net35.csproj +++ b/src/MsgPack.Net35/MsgPack.Net35.csproj @@ -521,6 +521,9 @@ Serialization\MessagePackEnumMemberAttribute.cs + + Serialization\MessagePackIgnoreAttribute.cs + Serialization\MessagePackMemberAttribute.cs diff --git a/src/MsgPack.Net40/MsgPack.Net40.csproj b/src/MsgPack.Net40/MsgPack.Net40.csproj index 8f41e5e2e..c47635028 100644 --- a/src/MsgPack.Net40/MsgPack.Net40.csproj +++ b/src/MsgPack.Net40/MsgPack.Net40.csproj @@ -532,6 +532,9 @@ Serialization\MessagePackEnumMemberAttribute.cs + + Serialization\MessagePackIgnoreAttribute.cs + Serialization\MessagePackMemberAttribute.cs diff --git a/src/MsgPack.Silverlight.5/MsgPack.Silverlight.5.csproj b/src/MsgPack.Silverlight.5/MsgPack.Silverlight.5.csproj index dc548840e..04356c64c 100644 --- a/src/MsgPack.Silverlight.5/MsgPack.Silverlight.5.csproj +++ b/src/MsgPack.Silverlight.5/MsgPack.Silverlight.5.csproj @@ -523,6 +523,9 @@ Serialization\MessagePackEnumMemberAttribute.cs + + Serialization\MessagePackIgnoreAttribute.cs + Serialization\MessagePackMemberAttribute.cs diff --git a/src/MsgPack.Silverlight.WindowsPhone/MsgPack.Silverlight.WindowsPhone.csproj b/src/MsgPack.Silverlight.WindowsPhone/MsgPack.Silverlight.WindowsPhone.csproj index 4c3979630..0967743c6 100644 --- a/src/MsgPack.Silverlight.WindowsPhone/MsgPack.Silverlight.WindowsPhone.csproj +++ b/src/MsgPack.Silverlight.WindowsPhone/MsgPack.Silverlight.WindowsPhone.csproj @@ -481,6 +481,9 @@ Serialization\MessagePackEnumMemberAttribute.cs + + Serialization\MessagePackIgnoreAttribute.cs + Serialization\MessagePackMemberAttribute.cs diff --git a/src/MsgPack.Unity3D/MsgPack.Unity3D.csproj b/src/MsgPack.Unity3D/MsgPack.Unity3D.csproj index 40c7e92b7..145165c9e 100644 --- a/src/MsgPack.Unity3D/MsgPack.Unity3D.csproj +++ b/src/MsgPack.Unity3D/MsgPack.Unity3D.csproj @@ -363,6 +363,9 @@ Serialization\MessagePackEnumMemberAttribute.cs + + Serialization\MessagePackIgnoreAttribute.cs + Serialization\MessagePackMemberAttribute.cs diff --git a/src/MsgPack.WinRT.Portable/MsgPack.WinRT.Portable.csproj b/src/MsgPack.WinRT.Portable/MsgPack.WinRT.Portable.csproj index 15650c116..f6c243d6c 100644 --- a/src/MsgPack.WinRT.Portable/MsgPack.WinRT.Portable.csproj +++ b/src/MsgPack.WinRT.Portable/MsgPack.WinRT.Portable.csproj @@ -424,6 +424,9 @@ Serialization\MessagePackEnumMemberAttribute.cs + + Serialization\MessagePackIgnoreAttribute.cs + Serialization\MessagePackMemberAttribute.cs diff --git a/src/MsgPack.Xamarin.Android/MsgPack.Xamarin.Android.csproj b/src/MsgPack.Xamarin.Android/MsgPack.Xamarin.Android.csproj index 6896307ee..2899de8f2 100644 --- a/src/MsgPack.Xamarin.Android/MsgPack.Xamarin.Android.csproj +++ b/src/MsgPack.Xamarin.Android/MsgPack.Xamarin.Android.csproj @@ -355,6 +355,9 @@ Serialization\MessagePackEnumMemberAttribute.cs + + Serialization\MessagePackIgnoreAttribute.cs + Serialization\MessagePackMemberAttribute.cs diff --git a/src/MsgPack.Xamarin.iOS/MsgPack.Xamarin.iOS.csproj b/src/MsgPack.Xamarin.iOS/MsgPack.Xamarin.iOS.csproj index c9e435b71..0c9a483da 100644 --- a/src/MsgPack.Xamarin.iOS/MsgPack.Xamarin.iOS.csproj +++ b/src/MsgPack.Xamarin.iOS/MsgPack.Xamarin.iOS.csproj @@ -359,6 +359,9 @@ Serialization\MessagePackEnumMemberAttribute.cs + + Serialization\MessagePackIgnoreAttribute.cs + Serialization\MessagePackMemberAttribute.cs diff --git a/src/MsgPack/MsgPack.csproj b/src/MsgPack/MsgPack.csproj index 08c8210a7..aa902636d 100644 --- a/src/MsgPack/MsgPack.csproj +++ b/src/MsgPack/MsgPack.csproj @@ -167,6 +167,7 @@ + diff --git a/src/MsgPack/Serialization/MessagePackIgnoreAttribute.cs b/src/MsgPack/Serialization/MessagePackIgnoreAttribute.cs new file mode 100644 index 000000000..cab436322 --- /dev/null +++ b/src/MsgPack/Serialization/MessagePackIgnoreAttribute.cs @@ -0,0 +1,39 @@ +#region -- License Terms -- +// +// MessagePack for CLI +// +// Copyright (C) 2014 FUJIWARA, Yusuke +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// 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. +// +// Contributors: +// Takeshi KIRIYA +// +#endregion -- License Terms -- + +using System; + +namespace MsgPack.Serialization +{ + /// + /// Marks the field or the property should not be serialized/deserialized with MessagePack for CLI serialization mechanism. + /// + [AttributeUsage( AttributeTargets.Field | AttributeTargets.Property, Inherited = false, AllowMultiple = true)] + public sealed class MessagePackIgnoreAttribute : Attribute + { + /// + /// Initializes a new instance of the class. + /// + public MessagePackIgnoreAttribute() { } + } +} diff --git a/src/MsgPack/Serialization/SerializationTarget.cs b/src/MsgPack/Serialization/SerializationTarget.cs index 97b9606f6..cce510721 100644 --- a/src/MsgPack/Serialization/SerializationTarget.cs +++ b/src/MsgPack/Serialization/SerializationTarget.cs @@ -137,6 +137,28 @@ internal static IEnumerable GetTargetMembers( Type type ) if ( filtered.Length > 0 ) { + var duplicated = + filtered.FirstOrDefault( + member => +#if !NETFX_CORE + Attribute.IsDefined( member, typeof( MessagePackIgnoreAttribute ) ) +#else + member.IsDefined( typeof( MessagePackIgnoreAttribute ) ) +#endif // !NETFX_CORE + ); + + if ( duplicated != null ) + { + throw new SerializationException( + String.Format( + CultureInfo.CurrentCulture, + "A member '{0}' of type '{1}' is marked with both MessagePackMemberAttribute and MessagePackIgnoreAttribute.", + duplicated.Name, + type + ) + ); + } + return filtered.Select( member => new SerializingMember( @@ -187,13 +209,18 @@ internal static IEnumerable GetTargetMembers( Type type ) } ); } -#if SILVERLIGHT || NETFX_CORE - return members.Where( member => member.GetIsPublic() ).Select( member => new SerializingMember( member, new DataMemberContract( member ) ) ); -#else return - members.Where( item => item.GetIsPublic() && !Attribute.IsDefined( item, typeof( NonSerializedAttribute ) ) ) - .Select( member => new SerializingMember( member, new DataMemberContract( member ) ) ); -#endif + members.Where( + member => member.GetIsPublic() +#if !SILVERLIGHT && !NETFX_CORE + && !Attribute.IsDefined( member, typeof( NonSerializedAttribute ) ) +#endif // !SILVERLIGHT && !NETFX_CORE +#if !NETFX_CORE + && !Attribute.IsDefined( member, typeof( MessagePackIgnoreAttribute ) ) +#else + && !member.IsDefined( typeof( MessagePackIgnoreAttribute ) ) +#endif // !NETFX_CORE + ).Select( member => new SerializingMember( member, new DataMemberContract( member ) ) ); } private static bool CheckTargetEligibility( MemberInfo member ) diff --git a/test/MsgPack.UnitTest.CodeDom/Serialization/ArrayCodeDomBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest.CodeDom/Serialization/ArrayCodeDomBasedAutoMessagePackSerializerTest.cs index 9c8618d33..021517d3b 100644 --- a/test/MsgPack.UnitTest.CodeDom/Serialization/ArrayCodeDomBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest.CodeDom/Serialization/ArrayCodeDomBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest.CodeDom/Serialization/MapCodeDomBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest.CodeDom/Serialization/MapCodeDomBasedAutoMessagePackSerializerTest.cs index 7d0159197..5fad5a8cf 100644 --- a/test/MsgPack.UnitTest.CodeDom/Serialization/MapCodeDomBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest.CodeDom/Serialization/MapCodeDomBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest/Serialization/ArrayContextBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest/Serialization/ArrayContextBasedAutoMessagePackSerializerTest.cs index 1fae34a5d..9181854ba 100644 --- a/test/MsgPack.UnitTest/Serialization/ArrayContextBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest/Serialization/ArrayContextBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest/Serialization/ArrayExpressionBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest/Serialization/ArrayExpressionBasedAutoMessagePackSerializerTest.cs index 53f15b1c6..791ef7e57 100644 --- a/test/MsgPack.UnitTest/Serialization/ArrayExpressionBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest/Serialization/ArrayExpressionBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest/Serialization/ArrayFieldBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest/Serialization/ArrayFieldBasedAutoMessagePackSerializerTest.cs index bd3af237d..91021891c 100644 --- a/test/MsgPack.UnitTest/Serialization/ArrayFieldBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest/Serialization/ArrayFieldBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest/Serialization/ArrayReflectionBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest/Serialization/ArrayReflectionBasedAutoMessagePackSerializerTest.cs index 4ea588ac3..0079c6592 100644 --- a/test/MsgPack.UnitTest/Serialization/ArrayReflectionBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest/Serialization/ArrayReflectionBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest/Serialization/AutoMessagePackSerializerTest.ttinclude b/test/MsgPack.UnitTest/Serialization/AutoMessagePackSerializerTest.ttinclude index 96b82eaaf..0030a3d07 100644 --- a/test/MsgPack.UnitTest/Serialization/AutoMessagePackSerializerTest.ttinclude +++ b/test/MsgPack.UnitTest/Serialization/AutoMessagePackSerializerTest.ttinclude @@ -1288,6 +1288,129 @@ namespace MsgPack.Serialization #endregion -- ReadOnly / Private Members -- +<#+ + if ( !forIos ) + { +#> +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- +<#+ + } +#> public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest/Serialization/MapContextBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest/Serialization/MapContextBasedAutoMessagePackSerializerTest.cs index c5a58e912..d76b9836d 100644 --- a/test/MsgPack.UnitTest/Serialization/MapContextBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest/Serialization/MapContextBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest/Serialization/MapExpressionBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest/Serialization/MapExpressionBasedAutoMessagePackSerializerTest.cs index f85d745ef..25d5266a1 100644 --- a/test/MsgPack.UnitTest/Serialization/MapExpressionBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest/Serialization/MapExpressionBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest/Serialization/MapFieldBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest/Serialization/MapFieldBasedAutoMessagePackSerializerTest.cs index fc2dc04b0..256a37030 100644 --- a/test/MsgPack.UnitTest/Serialization/MapFieldBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest/Serialization/MapFieldBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC"; diff --git a/test/MsgPack.UnitTest/Serialization/MapReflectionBasedAutoMessagePackSerializerTest.cs b/test/MsgPack.UnitTest/Serialization/MapReflectionBasedAutoMessagePackSerializerTest.cs index dc889efde..fe8765df7 100644 --- a/test/MsgPack.UnitTest/Serialization/MapReflectionBasedAutoMessagePackSerializerTest.cs +++ b/test/MsgPack.UnitTest/Serialization/MapReflectionBasedAutoMessagePackSerializerTest.cs @@ -1178,6 +1178,122 @@ private void TestNonPublicWritableMemberCore( T original, params string[] exp #endregion -- ReadOnly / Private Members -- +#region -- Exclusion -- + + private void TestIgnoreCore( Action setter, Action assertion ) + where T : new() + { + var target = GetSerializationContext().GetSerializer(); + var obj = new T(); + setter( obj ); + using ( var buffer = new MemoryStream() ) + { + target.Pack( buffer, obj ); + buffer.Position = 0; + var actual = target.Unpack( buffer ); + assertion( obj, actual ); + } + } + + private void TestIgnoreCore() + where TException : Exception + { + Assert.Throws( () => GetSerializationContext().GetSerializer() ); + } + + [Test] + public void TestIgnore_Normal() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotIgnored = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotIgnored, Is.EqualTo( expected.NotIgnored ) ); + } + ); + } + + [Test] + public void TestIgnore_ExcludedOnly() + { + TestIgnoreCore(); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionMixed() + { + TestIgnoreCore( + target => { + target.IgnoredField = "ABC"; + target.IgnoredProperty = "ABC"; + target.NotMarked = "ABC"; + target.Marked = "ABC"; + }, + ( expected, actual ) => + { + Assert.That( actual.IgnoredField, Is.Null ); + Assert.That( actual.IgnoredProperty, Is.Null ); + Assert.That( actual.NotMarked, Is.Null ); + Assert.That( actual.Marked, Is.EqualTo( expected.Marked ) ); + } + ); + } + + [Test] + public void TestIgnore_ExclusionAndInclusionSimulatously() + { + TestIgnoreCore(); + } + + + public class OnlyExcluded + { + [MessagePackIgnore] + public string Ignored { get; set; } + } + + public class Excluded + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotIgnored { get; set; } + } + + public class ExclusionAndInclusionMixed + { + [MessagePackIgnore] + public string IgnoredField; + + [MessagePackIgnore] + public string IgnoredProperty { get; set; } + + public string NotMarked { get; set; } + + [MessagePackMember( 0 )] + public string Marked { get; set; } + } + + public class ExclusionAndInclusionSimulatously + { + [MessagePackMember( 0 )] + public string Marked { get; set; } + + [MessagePackIgnore] + [MessagePackMember( 1 )] + public string DoubleMarked { get; set; } + } + +#endregion -- Exclusion -- public class HasInitOnlyField { public readonly string Field = "ABC";