Skip to content

Instantly share code, notes, and snippets.

@lukasz-pyrzyk
Created April 8, 2016 14:03
Show Gist options
  • Save lukasz-pyrzyk/19f7563cba4aad7cf15a53d29bd64009 to your computer and use it in GitHub Desktop.
Save lukasz-pyrzyk/19f7563cba4aad7cf15a53d29bd64009 to your computer and use it in GitHub Desktop.
public class SerializationUtils
{
public const PrefixStyle Style = PrefixStyle.Fixed32;
public static int GetLengthOfPackage(byte[] buffer)
{
int size;
Serializer.TryReadLengthPrefix(buffer, 0, buffer.Length, Style, out size);
return size;
}
public static byte[] Serialize<T>(T obj)
{
byte[] buffer;
using (MemoryStream ms = new MemoryStream())
{
Serializer.SerializeWithLengthPrefix(ms, obj, Style);
buffer = ms.ToArray();
}
return buffer;
}
public static T Deserialize<T>(byte[] buffer)
{
using (MemoryStream ms = new MemoryStream(buffer))
{
return Serializer.DeserializeWithLengthPrefix<T>(ms, Style);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment