1616// See the License for the specific language governing permissions and
1717// limitations under the License.
1818//
19+ // Contributors:
20+ // Shrenik Jhaveri (ShrenikOne)
21+ //
1922#endregion -- License Terms --
2023
2124using System ;
@@ -32,7 +35,81 @@ namespace MsgPack
3235 public class MessagePackMemberSkipTest
3336 {
3437 [ Test ]
35- public void SerializeThenDeserialize ( )
38+ public void SerializeThenDeserialize_Array ( )
39+ {
40+ // They are object for just description.
41+ var targetObject = new PhotoEntry
42+ {
43+ Id = 123 ,
44+ Title = "My photo" ,
45+ Date = DateTime . Now ,
46+ Image = new byte [ ] { 1 , 2 , 3 , 4 } ,
47+ Comment = "This is test object to be serialize/deserialize using MsgPack."
48+ } ;
49+
50+ targetObject . Tags . Add ( new PhotoTag { Name = "Sample" , Id = 123 } ) ;
51+ targetObject . Tags . Add ( new PhotoTag { Name = "Excellent" , Id = 456 } ) ;
52+ var stream = new MemoryStream ( ) ;
53+
54+ // 1. Create serializer instance.
55+ SerializationContext context = new SerializationContext ( ) ;
56+ context . SerializationMethod = SerializationMethod . Array ;
57+ context . DefaultDateTimeConversionMethod = DateTimeConversionMethod . Native ;
58+ context . BindingOptions . SetIgnoringMembers ( typeof ( PhotoEntry ) , new [ ] { nameof ( PhotoEntry . Image ) } ) ;
59+ context . BindingOptions . SetIgnoringMembers ( typeof ( PhotoTag ) , new [ ] { nameof ( PhotoTag . Name ) } ) ;
60+ var serializer = MessagePackSerializer . Get < PhotoEntry > ( context ) ;
61+
62+ // 2. Serialize object to the specified stream.
63+ serializer . Pack ( stream , targetObject ) ;
64+
65+ // Set position to head of the stream to demonstrate deserialization.
66+ stream . Position = 0 ;
67+
68+ // 3. Deserialize object from the specified stream.
69+ var deserializedObject = serializer . Unpack ( stream ) ;
70+
71+ Assert . AreEqual ( targetObject . Comment , deserializedObject . Comment ) ;
72+ Assert . AreEqual ( targetObject . Id , deserializedObject . Id ) ;
73+ Assert . AreEqual ( targetObject . Date , deserializedObject . Date ) ;
74+ Assert . AreEqual ( targetObject . Title , deserializedObject . Title ) ;
75+ Assert . Null ( deserializedObject . Image ) ;
76+ Assert . AreEqual ( targetObject . Tags . Count , deserializedObject . Tags . Count ) ;
77+ for ( int i = 0 ; i < deserializedObject . Tags . Count ; i ++ )
78+ {
79+ Assert . AreEqual ( targetObject . Tags [ i ] . Id , deserializedObject . Tags [ i ] . Id ) ;
80+ Assert . Null ( deserializedObject . Tags [ i ] . Name ) ;
81+ }
82+
83+ //// TODO: @yfakariya, Need help, how i can achieve below....
84+ //// How i can inject Nil or Null for Skipped/Ignored member, to support interoperability...
85+
86+
87+ //// SerializationContext newContext = new SerializationContext();
88+ //// newContext.SerializationMethod = SerializationMethod.Array;
89+ //// newContext.DefaultDateTimeConversionMethod = DateTimeConversionMethod.Native;
90+ //// serializer = MessagePackSerializer.Get<PhotoEntry>( newContext );
91+
92+ //// // Set position to head of the stream to demonstrate deserialization.
93+ //// stream.Position = 0;
94+
95+ //// // 3. Deserialize object from the specified stream.
96+ //// deserializedObject = serializer.Unpack( stream );
97+
98+ //// Assert.AreEqual( targetObject.Comment, deserializedObject.Comment );
99+ //// Assert.AreEqual( targetObject.Id, deserializedObject.Id );
100+ //// Assert.AreEqual( targetObject.Date, deserializedObject.Date );
101+ //// Assert.AreEqual( targetObject.Title, deserializedObject.Title );
102+ //// Assert.Null( deserializedObject.Image );
103+ //// Assert.AreEqual( targetObject.Tags.Count, deserializedObject.Tags.Count );
104+ //// for ( int i = 0; i < deserializedObject.Tags.Count; i++ )
105+ //// {
106+ //// Assert.AreEqual( targetObject.Tags[ i ].Id, deserializedObject.Tags[ i ].Id );
107+ //// Assert.Null( deserializedObject.Tags[ i ].Name );
108+ //// }
109+ }
110+
111+ [ Test ]
112+ public void SerializeThenDeserialize_Map ( )
36113 {
37114 // They are object for just description.
38115 var targetObject = new PhotoEntry
@@ -50,9 +127,10 @@ public void SerializeThenDeserialize()
50127
51128 // 1. Create serializer instance.
52129 SerializationContext context = new SerializationContext ( ) ;
130+ context . SerializationMethod = SerializationMethod . Map ;
53131 context . DefaultDateTimeConversionMethod = DateTimeConversionMethod . Native ;
54- context . TypesMemberIgnoreList . Add ( typeof ( PhotoEntry ) , new [ ] { nameof ( PhotoEntry . Image ) } ) ;
55- context . TypesMemberIgnoreList . Add ( typeof ( PhotoTag ) , new [ ] { nameof ( PhotoTag . Name ) } ) ;
132+ context . BindingOptions . SetIgnoringMembers ( typeof ( PhotoEntry ) , new [ ] { nameof ( PhotoEntry . Image ) } ) ;
133+ context . BindingOptions . SetIgnoringMembers ( typeof ( PhotoTag ) , new [ ] { nameof ( PhotoTag . Name ) } ) ;
56134 var serializer = MessagePackSerializer . Get < PhotoEntry > ( context ) ;
57135
58136 // 2. Serialize object to the specified stream.
@@ -75,6 +153,29 @@ public void SerializeThenDeserialize()
75153 Assert . AreEqual ( targetObject . Tags [ i ] . Id , deserializedObject . Tags [ i ] . Id ) ;
76154 Assert . Null ( deserializedObject . Tags [ i ] . Name ) ;
77155 }
156+
157+ SerializationContext newContext = new SerializationContext ( ) ;
158+ newContext . SerializationMethod = SerializationMethod . Map ;
159+ newContext . DefaultDateTimeConversionMethod = DateTimeConversionMethod . Native ;
160+ serializer = MessagePackSerializer . Get < PhotoEntry > ( context ) ;
161+
162+ // Set position to head of the stream to demonstrate deserialization.
163+ stream . Position = 0 ;
164+
165+ // 3. Deserialize object from the specified stream.
166+ deserializedObject = serializer . Unpack ( stream ) ;
167+
168+ Assert . AreEqual ( targetObject . Comment , deserializedObject . Comment ) ;
169+ Assert . AreEqual ( targetObject . Id , deserializedObject . Id ) ;
170+ Assert . AreEqual ( targetObject . Date , deserializedObject . Date ) ;
171+ Assert . AreEqual ( targetObject . Title , deserializedObject . Title ) ;
172+ Assert . Null ( deserializedObject . Image ) ;
173+ Assert . AreEqual ( targetObject . Tags . Count , deserializedObject . Tags . Count ) ;
174+ for ( int i = 0 ; i < deserializedObject . Tags . Count ; i ++ )
175+ {
176+ Assert . AreEqual ( targetObject . Tags [ i ] . Id , deserializedObject . Tags [ i ] . Id ) ;
177+ Assert . Null ( deserializedObject . Tags [ i ] . Name ) ;
178+ }
78179 }
79180 }
80181
0 commit comments