public override void Serialize(System.IO.Stream stream, object obj)
 {
     using (var packer = MsgPack.Packer.Create(stream))
     {
         var serializer = serializationContext.GetSerializer(obj.GetType());
         serializer.PackTo(packer, obj);
     }
 }
Beispiel #2
0
		public async Task RunAsync()
		{
			// Now supports async.
			var context = new SerializationContext();

			// Asynchronous is allowed by default.
			Assert.True( context.SerializerOptions.WithAsync );

			var serializer = context.GetSerializer<PhotoEntry>();
			var targetObject =
				new PhotoEntry // See Sample01_BasicUsage.cs
				{
					Id = 123,
					Title = "My photo",
					Date = DateTime.Now,
					Image = new byte[] { 1, 2, 3, 4 },
					Comment = "This is test object to be serialize/deserialize using MsgPack."
				};

			using ( var stream = new MemoryStream() )
			{
				// Note: PackAsync/UnpackAsync uses internal BufferedStream to avopid chatty asynchronous call for underlying I/O system.
				//       If you change this behavior, use PackToAsync/UnpackFromAsync with Packer/Unpacker factories to specify options.
				await serializer.PackAsync( stream, targetObject );
				stream.Position = 0;
				var roundTripped = await serializer.UnpackAsync( stream );
			}
		}
        public EntityPropertiesTests()
        {
            var context = new SerializationContext();
            context.SerializationMethod = SerializationMethod.Map;

            _serializer = context.GetSerializer<BasicEntity>();
        }
Beispiel #4
0
        private static void TestEnumForByName <T>(SerializationContext context, T value, string property)
        {
            var serializer = context.GetSerializer <T>();

            using (var stream = new MemoryStream())
            {
                serializer.Pack(stream, value);
                stream.Position = 0;
                var deserialized = serializer.Unpack(stream);
                TestEnumForByNameCore(stream, value, deserialized, property);
            }
        }
Beispiel #5
0
        private static void TestGenericDefaultSerializerCore <T>(T value, Func <T, T, bool> comparer)
        {
            var context    = new SerializationContext(PackerCompatibilityOptions.None);
            var serializer = context.GetSerializer <T>();

            using (var buffer = new MemoryStream())
            {
                serializer.Pack(buffer, value);
                buffer.Position = 0;
                var result = serializer.Unpack(buffer);
                Assert.That(comparer(value, result), "  Expected: {1}{0}  Actual :{2}", Environment.NewLine, value, result);
            }
        }
Beispiel #6
0
        public async Task TestIssue321_AsyncBuffering()
        {
            var context    = new SerializationContext();
            var serializer = context.GetSerializer <string>();

            using (var stream = new MyMemoryStream())
            {
                await serializer.PackAsync(stream, "hello");

                await serializer.PackAsync(stream, "world");

                stream.Position = 0;
                var result1 = await serializer.UnpackAsync(stream);

                // Ensure no exceptions are thrown.
                var result2 = await serializer.UnpackAsync(stream);
            }
        }
Beispiel #7
0
		public void Issue147_161_162_HidingMembersWillNotBeDuplicated()
		{
			var context = new SerializationContext();
			context.SerializationMethod = SerializationMethod.Array;
			var serializer = context.GetSerializer<Child>();
			using ( var buffer = new MemoryStream() )
			{
				var obj = new Child { Field = new List<string> { "A", "B" }, Property = new List<string> { "C", "D" } };
				serializer.Pack( buffer, obj );
				
				buffer.Position = 0;

				using ( var unpacker = Unpacker.Create( buffer, ownsStream: false ) )
				{
					Assert.That( unpacker.Read() );
					// If the hiding is not respected, this value will be 4.
					Assert.That( unpacker.LastReadData.AsInt32(), Is.EqualTo( 2 ) );
				}
			}
		}
		public void Polymorphism()
		{
			// As of 0.7, polymorphism is implemented.

			// Setup the context to use map for pretty printing.
			var context = new SerializationContext { SerializationMethod = SerializationMethod.Map };

			var serializer = context.GetSerializer<PolymorphicHolder>();

			var rootObject =
				new PolymorphicHolder
				{
					WithRuntimeType = new FileObject { Path = "/path/to/file" },
					WithKnownType = new DirectoryObject { Path = "/path/to/dir/" }
				};

			using ( var buffer = new MemoryStream() )
			{
				serializer.Pack(
					buffer, new PolymorphicHolder
					{
						WithRuntimeType = new FileObject { Path = "/path/to/file" },
						WithKnownType = new DirectoryObject { Path = "/path/to/dir/" }
					}
				);

				buffer.Position = 0;

				Print( buffer );

				buffer.Position = 0;
				var deserialized = serializer.Unpack( buffer );

				Assert.That( deserialized.WithRuntimeType, Is.TypeOf<FileObject>() );
				Assert.That( deserialized.WithRuntimeType.Path, Is.EqualTo( "/path/to/file" ) );
				Assert.That( deserialized.WithKnownType, Is.TypeOf<DirectoryObject>() );
				Assert.That( deserialized.WithKnownType.Path, Is.EqualTo( "/path/to/dir/" ) );
			}
		}
Beispiel #9
0
		private static void TestDeserialize( Stopwatch stopWatch, Stream buffer, SerializationContext serializationContext )
		{
			var serializer = serializationContext.GetSerializer<PhotoData>();
			stopWatch.Restart();
			serializer.Unpack( buffer );
			stopWatch.Stop();
		}
Beispiel #10
0
		private static void TestSerialize( Stopwatch stopWatch, Stream buffer, SerializationContext serializationContext )
		{
			var target =
				new PhotoData
				{
					Id = 123,
					Image = Image,
					Rating = Rating.Good,
					TakenDateTime = DateTimeOffset.Now,
					Title = "Favorite"
				};
			target.Tags.Add( "Example" );
			target.Tags.Add( "Foo" );
			target.Tags.Add( "Bar" );

			var serializer = serializationContext.GetSerializer<PhotoData>();
			stopWatch.Restart();
			serializer.Pack( buffer, target );
			stopWatch.Stop();
		}
Beispiel #11
0
		private static void TestCreateSerializer( Stopwatch stopWatch, SerializationContext serializationContext )
		{
			stopWatch.Restart();
			serializationContext.GetSerializer<PhotoData>();
			stopWatch.Stop();
		}
 public IntKeySerializerTarget MsgPackCliArray()
 {
     return(arrayContext.GetSerializer <IntKeySerializerTarget>().UnpackSingleObject(arrayObj));
 }
		public void TestIssue111()
		{
			var context = new SerializationContext
			{
				SerializationMethod = SerializationMethod.Map,
				EnumSerializationMethod = EnumSerializationMethod.ByName,
				CompatibilityOptions =
				{
					PackerCompatibilityOptions = PackerCompatibilityOptions.None
				}
			};
			var serializer = context.GetSerializer<Dictionary<string, object>>();

			var dict = new Dictionary<string, object>();
			dict[ "a" ] = "x";
			dict[ "b" ] = true;
			dict[ "c" ] = 5;
			dict[ "myclass" ] = new Issue111Class() { x = 8, y = "ola" };

			byte[] body = serializer.PackSingleObject( dict );

			using ( var stream = new MemoryStream( body ) )
			{
				var unpackedDictionary = Unpacking.UnpackDictionary( stream );
				Assert.That( unpackedDictionary.Count, Is.EqualTo( 4 ) );
				Assert.That( unpackedDictionary[ "a" ] == "x" );
				Assert.That( unpackedDictionary[ "b" ] == true );
				Assert.That( unpackedDictionary[ "c" ] == 5 );
				Assert.That( unpackedDictionary[ "myclass" ].IsDictionary );
				var myClass = unpackedDictionary[ "myclass" ].AsDictionary();
				Assert.That( myClass[ "x" ] == 8 );
				Assert.That( myClass[ "y" ] == "ola" );
			}
		}
		public void DirectPolymorphism()
		{
			// As of 0.9, you can serialize polymorphic directly.

			// Setup the context to use map for pretty printing.
			var context = new SerializationContext { SerializationMethod = SerializationMethod.Map };

			// With type information embedding (runtime type)
			// The argument is "decoded" info from MessagePackRuntimeTypeAttribute or its family.
			var serializerWithTypeInfoEmbedding =
				context.GetSerializer<IFileSystemObject>( PolymorphismSchema.ForPolymorphicObject( typeof( IFileSystemObject ), SampleTypeVerifier.Verify ) );

			using ( var buffer = new MemoryStream() )
			{
				serializerWithTypeInfoEmbedding.Pack( buffer, new FileObject { Path = "/path/to/file" } );
				buffer.Position = 0;

				Print( buffer );

				buffer.Position = 0;
				var deserialized = serializerWithTypeInfoEmbedding.Unpack( buffer );
				Assert.That( deserialized, Is.TypeOf<FileObject>() );
				Assert.That( deserialized.Path, Is.EqualTo( "/path/to/file" ) );
			}

			// With type code mapping (known type)
			// The argument is "decoded" info from MessagePackKnownTypeAttribute or its family.
			var serializerWithTypeCodeMapping =
				context.GetSerializer<IFileSystemObject>(
					PolymorphismSchema.ForPolymorphicObject(
						typeof( IFileSystemObject ),
						new Dictionary<string, Type>
						{
							{ "f", typeof( FileObject ) },
							{ "d", typeof( DirectoryObject ) }
						}
					)
				);

			using ( var buffer = new MemoryStream() )
			{
				serializerWithTypeCodeMapping.Pack( buffer, new DirectoryObject { Path = "/path/to/dir/" } );
				buffer.Position = 0;

				Print( buffer );

				buffer.Position = 0;
				var deserialized = serializerWithTypeCodeMapping.Unpack( buffer );
				Assert.That( deserialized, Is.TypeOf<DirectoryObject>() );
				Assert.That( deserialized.Path, Is.EqualTo( "/path/to/dir/" ) );
			}
		}