Created
April 8, 2016 14:03
-
-
Save lukasz-pyrzyk/19f7563cba4aad7cf15a53d29bd64009 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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