public void SimpleBufferCase()
        {
            // Assumes that we know maximum serialized data size, so just use it!
            var buffer = new byte[1024 * 64];

            var context    = new SerializationContext();
            var serializer = context.GetSerializer <PhotoEntry>();

            var obj =
                new PhotoEntry
            {
                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."
            };

            // Note that the packer automatically increse buffer.
            using (var bytePacker = Packer.Create(buffer))
            {
                serializer.PackTo(bytePacker, obj);
                // Note: You can get actual bytes with GetResultBytes(), but it causes array copy.
                //       You can avoid copying using original buffer (when you prohibits buffer allocation on Packer.Create) or GetFinalBuffers() instead.
                Console.WriteLine("Serialized: {0}", BitConverter.ToString(buffer, 0, ( int )bytePacker.BytesUsed));

                using (var byteUnpacker = Unpacker.Create(buffer))
                {
                    var deserialized = serializer.UnpackFrom(byteUnpacker);
                }
            }
        }
Exemple #2
0
        public void SerializeThenDeserialize()
        {
            // They are object for just description.
            var targetObject =
                new PhotoEntry
            {
                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();

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

            // 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. Deserialize object from the specified stream.
            var deserializedObject = serializer.Unpack(stream);

            // Test deserialized value.
            Debug.WriteLine("Same object? {0}", Object.ReferenceEquals(targetObject, deserializedObject));
            Debug.WriteLine("Same Id? {0}", targetObject.Id == deserializedObject.Id);
            Debug.WriteLine("Same Title? {0}", targetObject.Title == deserializedObject.Title);
            // Note that MsgPack defacto-standard is Unix epoc in milliseconds precision, so micro- and nano- seconds will be lost. See sample 04 for workaround.
            Debug.WriteLine("Same Date? {0}", targetObject.Date.ToString("YYYY-MM-DD HH:mm:ss.fff") == deserializedObject.Date.ToString("YYYY-MM-DD HH:mm:ss.fff"));
            // Image and Comment tests are ommitted here.
            // Collection elements are deserialzed.
            Debug.WriteLine("Items count: {0}", deserializedObject.Tags.Count);
        }
		public void SerializeThenDeserialize()
		{
			// They are object for just description. 
			var targetObject =
				new PhotoEntry
				{
					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();

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

			// 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. Deserialize object from the specified stream.
			var deserializedObject = serializer.Unpack( stream );

			// Test deserialized value.
			Debug.WriteLine( "Same object? {0}", Object.ReferenceEquals( targetObject, deserializedObject ) );
			Debug.WriteLine( "Same Id? {0}", targetObject.Id == deserializedObject.Id );
			Debug.WriteLine( "Same Title? {0}", targetObject.Title == deserializedObject.Title );
			// Note that MsgPack defacto-standard is Unix epoc in milliseconds precision, so micro- and nano- seconds will be lost. See sample 04 for workaround.
			Debug.WriteLine( "Same Date? {0}", targetObject.Date.ToString( "YYYY-MM-DD HH:mm:ss.fff" ) == deserializedObject.Date.ToString( "YYYY-MM-DD HH:mm:ss.fff" ) );
			// Image and Comment tests are ommitted here.
			// Collection elements are deserialzed.
			Debug.WriteLine( "Items count: {0}", deserializedObject.Tags.Count );
		}