Skip to content

Commit e6b43e4

Browse files
authored
Merge pull request #282 from ShrenikOne/master
Issue #278: Support skipping of Member for a Type on serialization without MessagePackIgnoreAttribute
2 parents ead1f72 + 6f3e0e6 commit e6b43e4

File tree

9 files changed

+427
-88
lines changed

9 files changed

+427
-88
lines changed

src/MsgPack.Silverlight.5/MsgPack.Silverlight.5.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@
673673
</Compile>
674674
<Compile Include="..\MsgPack\Serialization\SerializationCompatibilityOptions.cs">
675675
<Link>Serialization\SerializationCompatibilityOptions.cs</Link>
676+
</Compile>
677+
<Compile Include="..\MsgPack\Serialization\BindingOptions.cs">
678+
<Link>Serialization\BindingOptions.cs</Link>
676679
</Compile>
677680
<Compile Include="..\MsgPack\Serialization\SerializationContext.cs">
678681
<Link>Serialization\SerializationContext.cs</Link>

src/MsgPack.Silverlight.WindowsPhone/MsgPack.Silverlight.WindowsPhone.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,9 @@
680680
</Compile>
681681
<Compile Include="..\MsgPack\Serialization\SerializationCompatibilityOptions.cs">
682682
<Link>Serialization\SerializationCompatibilityOptions.cs</Link>
683+
</Compile>
684+
<Compile Include="..\MsgPack\Serialization\BindingOptions.cs">
685+
<Link>Serialization\BindingOptions.cs</Link>
683686
</Compile>
684687
<Compile Include="..\MsgPack\Serialization\SerializationContext.cs">
685688
<Link>Serialization\SerializationContext.cs</Link>

src/MsgPack.Unity.Full/MsgPack.Unity.Full.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,9 @@
651651
</Compile>
652652
<Compile Include="..\MsgPack\Serialization\SerializationCompatibilityOptions.cs">
653653
<Link>Serialization\SerializationCompatibilityOptions.cs</Link>
654+
</Compile>
655+
<Compile Include="..\MsgPack\Serialization\BindingOptions.cs">
656+
<Link>Serialization\BindingOptions.cs</Link>
654657
</Compile>
655658
<Compile Include="..\MsgPack\Serialization\SerializationContext.cs">
656659
<Link>Serialization\SerializationContext.cs</Link>

src/MsgPack.Unity/MsgPack.Unity.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,9 @@
234234
</Compile>
235235
<Compile Include="..\MsgPack\ReflectionAbstractions.cs">
236236
<Link>ReflectionAbstractions.cs</Link>
237+
</Compile>
238+
<Compile Include="..\MsgPack\Serialization\BindingOptions.cs">
239+
<Link>Serialization\BindingOptions.cs</Link>
237240
</Compile>
238241
<Compile Include="..\MsgPack\Serialization\Tracer.cs">
239242
<Link>Serialization\Tracer.cs</Link>

src/MsgPack.Uwp/MsgPack.Uwp.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,9 @@
723723
</Compile>
724724
<Compile Include="..\MsgPack\Serialization\SerializationCompatibilityOptions.cs">
725725
<Link>Serialization\SerializationCompatibilityOptions.cs</Link>
726+
</Compile>
727+
<Compile Include="..\MsgPack\Serialization\BindingOptions.cs">
728+
<Link>Serialization\BindingOptions.cs</Link>
726729
</Compile>
727730
<Compile Include="..\MsgPack\Serialization\SerializationContext.cs">
728731
<Link>Serialization\SerializationContext.cs</Link>
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
#region -- License Terms --
2+
//
3+
// MessagePack for CLI
4+
//
5+
// Copyright (C) 2016 FUJIWARA, Yusuke and contributors
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
// Contributors:
20+
// Shrenik Jhaveri (ShrenikOne)
21+
//
22+
#endregion -- License Terms --
23+
24+
25+
using System;
26+
using System.Collections.Generic;
27+
using System.Linq;
28+
29+
namespace MsgPack.Serialization
30+
{
31+
/// <summary>
32+
/// The Binding Options provide serializer clue on including property/field as part of packing.
33+
/// </summary>
34+
public class BindingOptions
35+
{
36+
/// <summary>
37+
/// Private mapping of types & their member skip list, which needs to ignore as part of serialization.
38+
/// </summary>
39+
private readonly IDictionary<Type, IEnumerable<string>> typeIgnoringMembersMap = new Dictionary<Type, IEnumerable<string>>();
40+
41+
/// <summary>
42+
/// Sets the member skip list for a specific target type.
43+
/// </summary>
44+
/// <param name="targetType">Type of the target.</param>
45+
/// <param name="memberSkipList">The member skip list.</param>
46+
public void SetIgnoringMembers( Type targetType, IEnumerable<string> memberSkipList )
47+
{
48+
lock ( this.typeIgnoringMembersMap )
49+
{
50+
if ( this.typeIgnoringMembersMap.ContainsKey( targetType ) )
51+
{
52+
this.typeIgnoringMembersMap[ targetType ] = memberSkipList;
53+
}
54+
else
55+
{
56+
this.typeIgnoringMembersMap.Add( targetType, memberSkipList );
57+
}
58+
}
59+
}
60+
61+
/// <summary>
62+
/// Gets the member skip list for a specific target type.
63+
/// </summary>
64+
/// <param name="targetType">Type of the target.</param>
65+
/// <returns>Returns member skip list for a specific target type.</returns>
66+
public IEnumerable<string> GetIgnoringMembers( Type targetType )
67+
{
68+
lock ( this.typeIgnoringMembersMap )
69+
{
70+
if ( this.typeIgnoringMembersMap.ContainsKey( targetType ) )
71+
{
72+
return this.typeIgnoringMembersMap[ targetType ];
73+
}
74+
else
75+
{
76+
return Enumerable.Empty<string>();
77+
}
78+
}
79+
}
80+
81+
/// <summary>
82+
/// Gets all registered types specific ignoring members.
83+
/// </summary>
84+
/// <returns>Returns all registered types specific ignoring members.</returns>
85+
public IDictionary<Type, IEnumerable<string>> GetAllIgnoringMembers()
86+
{
87+
lock ( this.typeIgnoringMembersMap )
88+
{
89+
return this.typeIgnoringMembersMap.ToDictionary( item => item.Key, item => ( IEnumerable<string> )item.Value.ToArray() );
90+
}
91+
}
92+
}
93+
}

src/MsgPack/Serialization/SerializationContext.cs

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
using System;
2626
#if !UNITY || MSGPACK_UNITY_FULL
27-
using System.ComponentModel;
2827
#endif // !UNITY || MSGPACK_UNITY_FULL
2928
#if FEATURE_CONCURRENT
3029
using System.Collections.Concurrent;
@@ -117,6 +116,27 @@ public static SerializationContext Default
117116

118117
private readonly object _generationLock;
119118

119+
private readonly BindingOptions _bindingOptions;
120+
121+
/// <summary>
122+
/// Gets the option settings for binding of type with serializer for field/property.
123+
/// </summary>
124+
/// <value>
125+
/// The option settings for binding of type's property/field in serializer generation.
126+
/// This value will not be <c>null</c>.
127+
/// </value>
128+
public BindingOptions BindingOptions
129+
{
130+
get
131+
{
132+
#if DEBUG
133+
Contract.Ensures( Contract.Result<BindingOptions>() != null );
134+
#endif // DEBUG
135+
136+
return this._bindingOptions;
137+
}
138+
}
139+
120140
/// <summary>
121141
/// Gets the current <see cref="SerializerRepository"/>.
122142
/// </summary>
@@ -221,13 +241,13 @@ public SerializationMethod SerializationMethod
221241
{
222242
case SerializationMethod.Array:
223243
case SerializationMethod.Map:
224-
{
225-
break;
226-
}
244+
{
245+
break;
246+
}
227247
default:
228-
{
229-
throw new ArgumentOutOfRangeException( "value" );
230-
}
248+
{
249+
throw new ArgumentOutOfRangeException( "value" );
250+
}
231251
}
232252

233253
Contract.EndContractBlock();
@@ -336,13 +356,13 @@ public DateTimeConversionMethod DefaultDateTimeConversionMethod
336356
case DateTimeConversionMethod.Native:
337357
case DateTimeConversionMethod.UnixEpoc:
338358
case DateTimeConversionMethod.Timestamp:
339-
{
340-
break;
341-
}
359+
{
360+
break;
361+
}
342362
default:
343-
{
344-
throw new ArgumentOutOfRangeException( "value" );
345-
}
363+
{
364+
throw new ArgumentOutOfRangeException( "value" );
365+
}
346366
}
347367

348368
Contract.EndContractBlock();
@@ -543,6 +563,7 @@ public SerializationContext( PackerCompatibilityOptions packerCompatibilityOptio
543563
this._serializerGeneratorOptions = new SerializerOptions();
544564
this._dictionarySerializationOptions = new DictionarySerlaizationOptions();
545565
this._enumSerializationOptions = new EnumSerializationOptions();
566+
this._bindingOptions = new BindingOptions();
546567
}
547568

548569
internal bool ContainsSerializer( Type rootType )
@@ -882,7 +903,7 @@ public MessagePackSerializer GetSerializer( Type targetType, object providerPara
882903
try
883904
{
884905
#endif // UNITY
885-
return SerializerGetter.Instance.Get( this, targetType, providerParameter );
906+
return SerializerGetter.Instance.Get( this, targetType, providerParameter );
886907
#if UNITY
887908
}
888909
catch ( Exception ex )

0 commit comments

Comments
 (0)