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 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);
            }
        }
Exemple #3
0
        public void SerializeThenDeserialize()
        {
            // They are object for just description.
            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."
            };

            targetObject.Tags.Add("Sample");
            targetObject.Tags.Add("Excellent");
            var stream = new MemoryStream();

            // Set using Map instead of Array to serialize complex object. See Sample03 for details.
            var context = new SerializationContext();

            context.SerializationMethod = SerializationMethod.Map;
            // You can use default context if you want to use map in all serializations which use default context.
            // SerializationContext.Default.SerializationMethod = SerializationMethod.Map;

            // 1. Create serializer instance.
            var serializer = MessagePackSerializer.Get <PhotoEntry>(context);

            // 2. Serialize object to the specified stream.
            serializer.Pack(stream, targetObject);

            // Set position to head of the stream to demonstrate deserialization.
            stream.Position = 0;

            // 3. Unpack MessagePackObject to get raw representation.
            var rawObject = Unpacking.UnpackObject(stream);

            // You can read MPO tree via Unpacker
            // var unpacker = Unpacker.Create( stream );

            // Check its type
            Debug.WriteLine("Is array? {0}", rawObject.IsArray);           // IsList is alias
            Debug.WriteLine("Is map? {0}", rawObject.IsMap);               // IsDictionary is alias
            Debug.WriteLine("Type: {0}", rawObject.UnderlyingType);

            // Gets serialized fields.
            // Note: When the object was serialized as array instead of map, use index instead.
            var asDictionary = rawObject.AsDictionary();

            Debug.WriteLine("Id : {0}({1})", asDictionary["Id"], asDictionary["Id"].UnderlyingType);
            // String is encoded as utf-8 by default.
            Debug.WriteLine("Title : {0}({1})", asDictionary["Title"], asDictionary["Title"].UnderlyingType);
            // Non-primitive is serialized as complex type or encoded primitive type.
            // DateTimeOffset is encoded as array[2]{ticks,offset}
            Debug.WriteLine("Date : {0}({1})", asDictionary["Date"], asDictionary["Date"].UnderlyingType);
            // byte[] is byte[], as you know.
            Debug.WriteLine("Image : {0}({1})", asDictionary["Image"], asDictionary["Image"].UnderlyingType);
        }
		public void SerializeThenDeserialize()
		{
			// They are object for just description. 
			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."
				};
			targetObject.Tags.Add( "Sample" );
			targetObject.Tags.Add( "Excellent" );
			var stream = new MemoryStream();

			// Set using Map instead of Array to serialize complex object. See Sample03 for details.
			var context = new SerializationContext();
			context.SerializationMethod = SerializationMethod.Map;
			// You can use default context if you want to use map in all serializations which use default context.
			// SerializationContext.Default.SerializationMethod = SerializationMethod.Map;

			// 1. Create serializer instance.
			var serializer = MessagePackSerializer.Get<PhotoEntry>( context );

			// 2. Serialize object to the specified stream.
			serializer.Pack( stream, targetObject );

			// Set position to head of the stream to demonstrate deserialization.
			stream.Position = 0;

			// 3. Unpack MessagePackObject to get raw representation.
			var rawObject = Unpacking.UnpackObject( stream );
			// You can read MPO tree via Unpacker
			// var unpacker = Unpacker.Create( stream );

			// Check its type
			Debug.WriteLine( "Is array? {0}", rawObject.IsArray ); // IsList is alias
			Debug.WriteLine( "Is map? {0}", rawObject.IsMap ); // IsDictionary is alias
			Debug.WriteLine( "Type: {0}", rawObject.UnderlyingType );

			// Gets serialized fields.
			// Note: When the object was serialized as array instead of map, use index instead.
			var asDictionary = rawObject.AsDictionary();
			Debug.WriteLine( "Id : {0}({1})", asDictionary[ "Id" ], asDictionary[ "Id" ].UnderlyingType );
			// String is encoded as utf-8 by default.
			Debug.WriteLine( "Title : {0}({1})", asDictionary[ "Title" ], asDictionary[ "Title" ].UnderlyingType );
			// Non-primitive is serialized as complex type or encoded primitive type.
			// DateTimeOffset is encoded as array[2]{ticks,offset}
			Debug.WriteLine( "Date : {0}({1})", asDictionary[ "Date" ], asDictionary[ "Date" ].UnderlyingType );
			// byte[] is byte[], as you know.
			Debug.WriteLine( "Image : {0}({1})", asDictionary[ "Image" ], asDictionary[ "Image" ].UnderlyingType );
		}