Example #1
0
 private static byte[] GetBytes <T>(MessagePackSerializer <T> target, T value)
 {
     using (var buffer = new MemoryStream())
     {
         target.Pack(buffer, value);
         return(buffer.ToArray());
     }
 }
Example #2
0
        /// <summary>
        ///		Serializes object as a single <see cref="MessagePackObject"/>.
        /// </summary>
        /// <typeparam name="T">The type of the object to be serialized.</typeparam>
        /// <param name="source"><see cref="MessagePackSerializer{T}"/> object.</param>
        /// <param name="obj">The object to be serialized.</param>
        /// <returns><see cref="MessagePackObject"/>.</returns>
        /// <exception cref="ArgumentNullException">
        ///		<paramref name="source"/> is <c>null</c>.
        /// </exception>
        /// <exception cref="System.Runtime.Serialization.SerializationException">
        ///		Failed to serialize.
        /// </exception>
        public static MessagePackObject ToMessagePackObject <T>(this MessagePackSerializer <T> source, T obj)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }

            using (var buffer = new MemoryStream())
            {
                source.Pack(buffer, obj);
                buffer.Position = 0;
                return(Unpacking.UnpackObject(buffer));
            }
        }