private static void TestMissingFieldCore(SerializationMethod method, EmitterFlavor flavor) { var context = new SerializationContext() { SerializationMethod = method, EmitterFlavor = flavor }; var serializer = MessagePackSerializer.Create <VersioningTestTarget>(context); using (var stream = new MemoryStream()) { if (method == SerializationMethod.Array) { stream.Write(new byte[] { 0x91, 0x1 }); } else { var packer = Packer.Create(stream, false); packer.PackMapHeader(1); packer.Pack("Field1"); packer.Pack(1); } stream.Position = 0; var result = serializer.Unpack(stream); Assert.That(result.Field1, Is.EqualTo(1)); Assert.That(result.Field2, Is.EqualTo(0)); Assert.That(result.Field3, Is.Null); } }
private void TestUnpackingFail <T, TException>( SerializationMethod method, Func <T> inputFactory, Func <byte[]> packedStreamFactory ) where T : new() where TException : Exception { var context = this.CreateSerializationContext(); context.SerializationMethod = method; var seraizlier = MessagePackSerializer.Create <T>(context); using (var buffer = new MemoryStream()) { if (packedStreamFactory != null) { var bytes = packedStreamFactory(); buffer.Write(bytes, 0, bytes.Length); } else { seraizlier.Pack(buffer, inputFactory == null ? new T() : inputFactory()); } buffer.Position = 0; Assert.Throws <TException>(() => seraizlier.Unpack(buffer)); } }
private static void TestFieldSwappedCore(EmitterFlavor flavor) { var context = new SerializationContext() { SerializationMethod = SerializationMethod.Map, EmitterFlavor = flavor }; var serializer = MessagePackSerializer.Create <VersioningTestTarget>(context); using (var stream = new MemoryStream()) { var packer = Packer.Create(stream, false); packer.PackMapHeader(4); packer.Pack("Field1"); packer.Pack(1); packer.Pack("Field2"); packer.Pack(-1); packer.Pack("Extra"); packer.Pack(2); // Issue6 stream.Position = 0; var result = serializer.Unpack(stream); Assert.That(result.Field1, Is.EqualTo(1)); Assert.That(result.Field2, Is.EqualTo(-1)); Assert.That(result.Field3, Is.Null); } }
private void TestUnpackSuccess <T>( SerializationMethod method, Func <T> inputFactory, Func <byte[]> packedStreamFactory, Action <T, T> assertion ) where T : new() { var context = this.CreateSerializationContext(); context.SerializationMethod = method; var seraizlier = MessagePackSerializer.Create <T>(context); using (var buffer = new MemoryStream()) { if (packedStreamFactory != null) { var bytes = packedStreamFactory(); buffer.Write(bytes, 0, bytes.Length); } else { seraizlier.Pack(buffer, inputFactory == null ? new T() : inputFactory()); } buffer.Position = 0; assertion(seraizlier.Unpack(buffer), new T()); } }
public void TestElelementTooManyInTheFirstPlace_Map_ExtrasAreIgnored() { using (var buffer = new MemoryStream()) { var valueOfValue1 = "123"; var valueOfValue2 = "234"; var valueOfValue3 = "345"; using (var packer = Packer.Create(buffer, false)) { packer.PackMapHeader(1); packer.PackString("Value1"); packer.PackString(valueOfValue1); packer.PackString("Value2"); packer.PackString(valueOfValue2); packer.PackString("Value3"); packer.PackString(valueOfValue3); } buffer.Position = 0; var target = MessagePackSerializer.Create <ComplexTypeWithTwoMember>(new SerializationContext() { SerializationMethod = SerializationMethod.Map }); var result = target.Unpack(buffer); Assert.That(result.Value1, Is.EqualTo(valueOfValue1)); Assert.That(result.Value2, Is.EqualTo(valueOfValue2)); } }
private void TestIssue10_Reader(Inner inner) { var serializer = MessagePackSerializer.Create <Outer>(); var outer = new Outer(); outer.Inner = inner; var bytes = serializer.PackSingleObject(outer); using (var buffer = new MemoryStream(bytes)) using (var unpacker = Unpacker.Create(buffer)) { Action <Unpacker, MessagePackObject> assertion = (u, o) => { Assert.That(u.Read(), Is.True); Assert.That(u.Data.Value == o, "{0} == {1}", u.Data.Value, o); }; assertion(unpacker, 3); Assert.That(unpacker.IsArrayHeader); assertion(unpacker, outer.A); assertion(unpacker, 3); Assert.That(unpacker.IsArrayHeader); using (var subtreeUnpacker = unpacker.ReadSubtree()) { assertion(subtreeUnpacker, outer.Inner.A); assertion(subtreeUnpacker, outer.Inner.Bytes); assertion(subtreeUnpacker, outer.Inner.C); } assertion(unpacker, outer.O); } }
private static void TestExtraFieldCore <T>(SerializationMethod method, EmitterFlavor flavor) { var context = new SerializationContext() { SerializationMethod = method, EmitterFlavor = flavor }; var serializer = MessagePackSerializer.Create <T>(context); using (var stream = new MemoryStream()) { if (method == SerializationMethod.Array) { stream.Write(new byte[] { 0x94, 0x1, 0xFF, 0xA1, ( byte )'a', 0xC0 }); } else { var packer = Packer.Create(stream, false); packer.PackMapHeader(4); packer.Pack("Field1"); packer.Pack(1); packer.Pack("Field2"); packer.Pack(-1); packer.Pack("Field3"); packer.Pack("a"); packer.Pack("Extra"); packer.PackNull(); } stream.Position = 0; var result = serializer.Unpack(stream); /* * if ( result is IMessagePackExtensibleObject ) * { * var extensionData = ( ( IMessagePackExtensibleObject )result ).ExtensionData; * Assert.That( extensionData.IsNil, Is.False ); * * if ( method == SerializationMethod.Array ) * { * Assert.That( extensionData.AsList().Count, Is.EqualTo( 4 ) ); * Assert.That( extensionData.AsList()[ 0 ] == 1 ); * Assert.That( extensionData.AsList()[ 1 ] == -1 ); * Assert.That( extensionData.AsList()[ 2 ] == "a" ); * Assert.That( extensionData.AsList()[ 3 ].IsNil ); * } * else * { * Assert.That( extensionData.AsDictionary().Count, Is.EqualTo( 4 ) ); * Assert.That( extensionData.AsDictionary()[ "Field1" ] == 1 ); * Assert.That( extensionData.AsDictionary()[ "Field2" ] == -1 ); * Assert.That( extensionData.AsDictionary()[ "Field3" ] == "a" ); * Assert.That( extensionData.AsDictionary()[ "Extra" ].IsNil ); * } * } */ } }
public void TestCreation_ValueType_OnlyNullIsInvalid() { Assert.NotNull(MessagePackSerializer.Create <NilImplicationTestTargetForValueTypeMemberDefault>(this.CreateSerializationContext())); Assert.Throws <SerializationException>( () => MessagePackSerializer.Create <NilImplicationTestTargetForValueTypeNull>(this.CreateSerializationContext()) ); Assert.NotNull(MessagePackSerializer.Create <NilImplicationTestTargetForValueTypeProhibit>(this.CreateSerializationContext())); }
public void TestCreation_ReadOnlyCollectionProperty_OnlyNullIsInvalid() { Assert.NotNull(MessagePackSerializer.Create <NilImplicationTestTargetForReadOnlyCollectionPropertyMemberDefault>(this.CreateSerializationContext())); Assert.Throws <SerializationException>( () => MessagePackSerializer.Create <NilImplicationTestTargetForReadOnlyCollectionPropertyNull>(this.CreateSerializationContext()) ); Assert.NotNull(MessagePackSerializer.Create <NilImplicationTestTargetForReadOnlyCollectionPropertyProhibit>(this.CreateSerializationContext())); }
public void TestCreate_WithoutContext_SameTypeAsCreate1() { var first = MessagePackSerializer.Create(typeof(Image)); Assert.That(first, Is.Not.Null); var second = MessagePackSerializer.Create <Image>(); Assert.That(second, Is.Not.Null); Assert.That(first.GetType(), Is.EqualTo(second.GetType())); }
public void TestCreate_WithoutContext_NewInstance() { var first = MessagePackSerializer.Create(typeof(Image)); Assert.That(first, Is.Not.Null); var second = MessagePackSerializer.Create(typeof(Image)); Assert.That(second, Is.Not.Null); Assert.That(first, Is.Not.SameAs(second)); }
/// <summary> /// Gets the <see cref="MessagePackSerializer{T}"/> with this instance. /// </summary> /// <typeparam name="T">Type of serialization/deserialization target.</typeparam> /// <returns> /// <see cref="MessagePackSerializer{T}"/>. /// If there is exiting one, returns it. /// Else the new instance will be created. /// </returns> /// <remarks> /// This method automatically register new instance via <see cref="M:SerializationRepository.Register{T}(MessagePackSerializer{T})"/>. /// </remarks> public MessagePackSerializer <T> GetSerializer <T>() { Contract.Ensures(Contract.Result <MessagePackSerializer <T> >() != null); var serializer = this._serializers.Get <T>(this); if (serializer == null) { bool lockTaken = false; try { try { } finally { #if SILVERLIGHT || NETFX_35 lock (this._typeLock) { lockTaken = this._typeLock.Add(typeof(T)); } #else lockTaken = this._typeLock.TryAdd(typeof(T), null); #endif } if (!lockTaken) { return(new LazyDelegatingMessagePackSerializer <T>(this)); } serializer = MessagePackSerializer.Create <T>(this); } finally { if (lockTaken) { #if SILVERLIGHT || NETFX_35 lock (this._typeLock) { this._typeLock.Remove(typeof(T)); } #else object dummy; this._typeLock.TryRemove(typeof(T), out dummy); #endif } } if (!this._serializers.Register <T>(serializer)) { serializer = this._serializers.Get <T>(this); } } return(serializer); }
public void TestCreate1_WithContext_NewInstance() { var context = new SerializationContext(); var first = MessagePackSerializer.Create <Image>(context); Assert.That(first, Is.Not.Null); var second = MessagePackSerializer.Create <Image>(context); Assert.That(second, Is.Not.Null); Assert.That(first, Is.Not.SameAs(second)); }
public void TestCreate_WithContext_SameTypeAsCreate1() { var context = new SerializationContext(); var first = MessagePackSerializer.Create(typeof(int), context); Assert.That(first, Is.Not.Null); var second = MessagePackSerializer.Create <int>(context); Assert.That(second, Is.Not.Null); Assert.That(first.GetType(), Is.EqualTo(second.GetType())); }
/// <summary> /// Gets the serializer for the specified <see cref="Type"/>. /// </summary> /// <param name="targetType">Type of the serialization target.</param> /// <returns> /// <see cref="IMessagePackSingleObjectSerializer"/>. /// If there is exiting one, returns it. /// Else the new instance will be created. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="targetType"/> is <c>null</c>. /// </exception> /// <remarks> /// Although <see cref="GetSerializer{T}"/> is preferred, /// this method can be used from non-generic type or methods. /// </remarks> public IMessagePackSingleObjectSerializer GetSerializer(Type targetType) { if (targetType == null) { throw new ArgumentNullException("targetType"); } Contract.Ensures(Contract.Result <IMessagePackSerializer>() != null); #if UNITY_IOS var serializer = this._serializers.Get(targetType, this); if (serializer == null) { bool lockTaken = false; try { try { } finally { lock (this._typeLock) { lockTaken = this._typeLock.Add(targetType); } } if (!lockTaken) { return(new LazyDelegatingMessagePackSerializer(targetType, this)); } serializer = MessagePackSerializer.Create(targetType, this); } finally { if (lockTaken) { lock (this._typeLock) { this._typeLock.Remove(targetType); } } } if (!this._serializers.Register(targetType, serializer)) { serializer = this._serializers.Get(targetType, this); } } return(serializer); #else return(SerializerGetter.Instance.Get(this, targetType)); #endif }
private static void TestDataContractAndMessagePackMemberAndNonSerializedAreMixedCore(SerializationMethod method) { var context = new SerializationContext() { SerializationMethod = method }; using (var buffer = new MemoryStream()) { var target = new MessagePackMemberAndDataMemberMixedTarget(); target.ShouldSerialized1 = 111; target.ShouldSerialized2 = 222; target.ShouldSerialized3 = 333; target.ShouldNotSerialized1 = 444; target.ShouldNotSerialized2 = 555; var serializer = MessagePackSerializer.Create <MessagePackMemberAndDataMemberMixedTarget>(context); serializer.Pack(buffer, target); buffer.Position = 0; var intermediate = Unpacking.UnpackObject(buffer); if (method == SerializationMethod.Array) { var asArray = intermediate.AsList(); Assert.That(asArray.Count, Is.EqualTo(3)); Assert.That(asArray[0] == target.ShouldSerialized1); Assert.That(asArray[1] == target.ShouldSerialized2); Assert.That(asArray[2] == target.ShouldSerialized3); } else { var asMap = intermediate.AsDictionary(); Assert.That(asMap.Count, Is.EqualTo(3)); Assert.That(asMap["ShouldSerialized1"] == target.ShouldSerialized1); Assert.That(asMap["ShouldSerialized2"] == target.ShouldSerialized2); Assert.That(asMap["ShouldSerialized3"] == target.ShouldSerialized3); } buffer.Position = 0; var result = serializer.Unpack(buffer); Assert.That(result.ShouldSerialized1, Is.EqualTo(target.ShouldSerialized1)); Assert.That(result.ShouldSerialized2, Is.EqualTo(target.ShouldSerialized2)); Assert.That(result.ShouldSerialized3, Is.EqualTo(target.ShouldSerialized3)); Assert.That(result.ShouldNotSerialized1, Is.Not.EqualTo(target.ShouldNotSerialized1).And.EqualTo(0)); Assert.That(result.ShouldNotSerialized2, Is.Not.EqualTo(target.ShouldNotSerialized2).And.EqualTo(0)); } }
/// <summary> /// Gets the <see cref="MessagePackSerializer{T}"/> with this instance. /// </summary> /// <typeparam name="T">Type of serialization/deserialization target.</typeparam> /// <returns> /// <see cref="MessagePackSerializer{T}"/>. /// If there is exiting one, returns it. /// Else the new instance will be created. /// </returns> /// <remarks> /// This method automatically register new instance via <see cref="M:SerializationRepository.Register{T}(MessagePackSerializer{T})"/>. /// </remarks> public MessagePackSerializer <T> GetSerializer <T>() { Contract.Ensures(Contract.Result <MessagePackSerializer <T> >() != null); #if UNITY_IOS return((MessagePackSerializer <T>) this.GetSerializer(typeof(T))); #else var serializer = this._serializers.Get <T>(this); if (serializer == null) { bool lockTaken = false; try { try { } finally { lock (this._typeLock) { lockTaken = this._typeLock.Add(typeof(T)); } } if (!lockTaken) { return(new LazyDelegatingMessagePackSerializer <T>(this)); } serializer = MessagePackSerializer.Create <T>(this); } finally { if (lockTaken) { lock (this._typeLock) { this._typeLock.Remove(typeof(T)); } } } if (!this._serializers.Register <T>(serializer)) { serializer = this._serializers.Get <T>(this); } } return(serializer); #endif }
private void TestIssue10_ReadXxxCore(Inner inner) { var serializer = MessagePackSerializer.Create <Outer>(); var outer = new Outer(); outer.Inner = inner; var bytes = serializer.PackSingleObject(outer); var result = serializer.UnpackSingleObject(bytes); Assert.That(result.A, Is.EqualTo(outer.A)); Assert.That(result.O, Is.EqualTo(outer.O)); Assert.That(result.Inner, Is.Not.Null); Assert.That(result.Inner.A, Is.EqualTo(outer.Inner.A)); Assert.That(result.Inner.Bytes, Is.EqualTo(outer.Inner.Bytes)); Assert.That(result.Inner.C, Is.EqualTo(outer.Inner.C)); }
private static void TestNonCollectionCore( Action <NilImplicationTestTarget> adjuster, Action <Packer, MessagePackObject, MessagePackObject, MessagePackObject, MessagePackObject, MessagePackObject> packing, MessagePackObject?memberDefault = null, MessagePackObject?nullButValueType = null, MessagePackObject?nullAndNullableValueType = null, MessagePackObject?nullAndReferenceType = null, MessagePackObject?prohibitReferenceType = null ) { using (var buffer = new MemoryStream()) using (var packer = Packer.Create(buffer)) { var serializer = MessagePackSerializer.Create <NilImplicationTestTarget>(new SerializationContext()); var target = new NilImplicationTestTarget(); if (adjuster != null) { adjuster(target); } packing( packer, memberDefault ?? target.MemberDefault, nullButValueType ?? target.NullButValueType, nullAndNullableValueType ?? (target.NullAndNullableValueType == null ? MessagePackObject.Nil : target.NullAndNullableValueType.Value), nullAndReferenceType ?? target.NullAndReferenceType, prohibitReferenceType ?? target.ProhibitReferenceType ); buffer.Position = 0; var result = serializer.Unpack(buffer); Assert.That(result.MemberDefault, Is.EqualTo(target.MemberDefault)); Assert.That(result.NullButValueType, Is.EqualTo(target.NullButValueType)); Assert.That(result.NullAndNullableValueType, Is.EqualTo(target.NullAndNullableValueType)); Assert.That(result.NullAndReferenceType, Is.EqualTo(target.NullAndReferenceType)); Assert.That(result.ProhibitReferenceType, Is.EqualTo(target.ProhibitReferenceType)); var expectedBytes = buffer.ToArray(); using (var actual = new MemoryStream()) { serializer.Pack(actual, target); } } }
public MessagePackSerializer <T> GetSerializer <T>() { MessagePackSerializer <T> serializer = this._serializers.Get <T>(this); if (serializer == null) { HashSet <Type> set; bool flag = false; try { try { } finally { lock ((set = this._typeLock)) { flag = this._typeLock.Add(typeof(T)); } } if (!flag) { return(new LazyDelegatingMessagePackSerializer <T>(this)); } serializer = MessagePackSerializer.Create <T>(this); } finally { if (flag) { lock ((set = this._typeLock)) { this._typeLock.Remove(typeof(T)); } } } if (!this._serializers.Register <T>(serializer)) { serializer = this._serializers.Get <T>(this); } } return(serializer); }
// ------ Packing ------ private void TestPackFail <T, TException>( SerializationMethod method, Func <T> inputFactory ) where T : new() where TException : Exception { var context = this.CreateSerializationContext(); context.SerializationMethod = method; var seraizlier = MessagePackSerializer.Create <T>(context); using (var buffer = new MemoryStream()) { Assert.Throws <TException>(() => seraizlier.Pack(buffer, inputFactory == null ? new T() : inputFactory())); buffer.Position = 0; Assert.Throws <TException>(() => seraizlier.Unpack(buffer)); } }
private static void TestExtraFieldRoundTripCore <T>(SerializationMethod method, EmitterFlavor flavor) { var context = new SerializationContext() { SerializationMethod = method, EmitterFlavor = flavor }; var serializer = MessagePackSerializer.Create <T>(context); using (var stream = new MemoryStream()) { if (method == SerializationMethod.Array) { stream.Write(new byte[] { 0x94, 0x1, 0xFF, 0xA1, ( byte )'a', 0xC0 }); } else { var packer = Packer.Create(stream, false); packer.PackMapHeader(4); packer.Pack("Field1"); packer.Pack(1); packer.Pack("Field2"); packer.Pack(-1); packer.Pack("Field3"); packer.Pack("a"); packer.Pack("Extra"); packer.PackNull(); } byte[] bytes = stream.ToArray(); stream.Position = 0; var result = serializer.Unpack(stream); stream.SetLength(0); serializer.Pack(stream, result); Assert.That(stream.ToArray(), Is.EqualTo(bytes)); } }
private void TestCollectionCore( Action <NilImplicationCollectionTestTarget> adjuster, Action <Packer, List <int>, List <int>, List <int> > packing, Func <List <int> > memberDefault = null, Func <List <int> > @null = null, Func <List <int> > prohibit = null ) { using (var buffer = new MemoryStream()) using (var packer = Packer.Create(buffer)) { var serializer = MessagePackSerializer.Create <NilImplicationCollectionTestTarget>(this.CreateSerializationContext()); var target = new NilImplicationCollectionTestTarget(); if (adjuster != null) { adjuster(target); } packing( packer, memberDefault != null ? memberDefault() : target.MemberDefault, @null != null ? @null() : target.Null, prohibit != null ? prohibit() : target.Prohibit ); buffer.Position = 0; var result = serializer.Unpack(buffer); Assert.That(result.MemberDefault, Is.EqualTo(target.MemberDefault)); Assert.That(result.Null, Is.EqualTo(target.Null)); Assert.That(result.Prohibit, Is.EqualTo(target.Prohibit)); var expectedBytes = buffer.ToArray(); using (var actual = new MemoryStream()) { serializer.Pack(actual, target); } } }
public void TestElelementMissingInTheFirstPlace_Map_MissingMembersAreSkipped() { using (var buffer = new MemoryStream()) { var valueOfValue1 = "123"; using (var packer = Packer.Create(buffer, false)) { packer.PackMapHeader(1); packer.PackString("Value1"); packer.PackString(valueOfValue1); } buffer.Position = 0; var context = this.CreateSerializationContext(); context.SerializationMethod = SerializationMethod.Map; var target = MessagePackSerializer.Create <ComplexTypeWithTwoMember>(context); var result = target.Unpack(buffer); Assert.That(result.Value1, Is.EqualTo(valueOfValue1)); Assert.That(result.Value2, Is.EqualTo(new ComplexTypeWithTwoMember().Value2)); } }
private static void TestDataContractAndNonSerializableAreMixedCore(SerializationMethod method) { var context = new SerializationContext() { SerializationMethod = method }; using (var buffer = new MemoryStream()) { var target = new DataContractAndNonSerializedMixedTarget(); target.ShouldSerialized = 111; var serializer = MessagePackSerializer.Create <DataContractAndNonSerializedMixedTarget>(context); serializer.Pack(buffer, target); buffer.Position = 0; var intermediate = Unpacking.UnpackObject(buffer); if (method == SerializationMethod.Array) { var asArray = intermediate.AsList(); Assert.That(asArray.Count, Is.EqualTo(1)); Assert.That(asArray[0] == target.ShouldSerialized); } else { var asMap = intermediate.AsDictionary(); Assert.That(asMap.Count, Is.EqualTo(1)); Assert.That(asMap["ShouldSerialized"] == target.ShouldSerialized); } buffer.Position = 0; var result = serializer.Unpack(buffer); Assert.That(result.ShouldSerialized, Is.EqualTo(target.ShouldSerialized)); } }
/// <summary> /// Creates new <see cref="IMessagePackSerializer"/> instance with <see cref="SerializationContext.Default"/>. /// </summary> /// <param name="targetType">Target type.</param> /// <returns> /// New <see cref="IMessagePackSingleObjectSerializer"/> instance to serialize/deserialize the object tree which the top is <paramref name="targetType"/>. /// </returns> /// <exception cref="ArgumentNullException"> /// <paramref name="targetType"/> is <c>null</c>. /// </exception> /// <remarks> /// To avoid boxing and strongly typed API is prefered, use <see cref="Create{T}()"/> instead when possible. /// </remarks> public static IMessagePackSingleObjectSerializer Create(Type targetType) { return(MessagePackSerializer.Create(targetType, SerializationContext.Default)); }
/// <summary> /// Creates new <see cref="MessagePackSerializer{T}"/> instance with <see cref="SerializationContext.Default"/>. /// </summary> /// <typeparam name="T">Target type.</typeparam> /// <returns> /// New <see cref="MessagePackSerializer{T}"/> instance to serialize/deserialize the object tree which the top is <typeparamref name="T"/>. /// </returns> public static MessagePackSerializer <T> Create <T>() { Contract.Ensures(Contract.Result <MessagePackSerializer <T> >() != null); return(MessagePackSerializer.Create <T>(SerializationContext.Default)); }
public void TestCreate1_WithContext_Null_Fail() { Assert.Throws <ArgumentNullException>(() => MessagePackSerializer.Create <Image>(null)); }
public void TestCreate_WithContext_ContextIsNull_Fail() { Assert.Throws <ArgumentNullException>(() => MessagePackSerializer.Create(typeof(Image), null)); }
public void TestCreate_WithContext_TypeIsNull_Fail() { Assert.Throws <ArgumentNullException>(() => MessagePackSerializer.Create(null, new SerializationContext())); }