|
| 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 | +} |
0 commit comments