using MessagePack; using MessagePack.Resolvers; using System; using System.IO; namespace AX.Serialization { public static partial class SerializationExtensions { public static ArraySegment Serialize(this T obj) { return Serializer.SerializeUnsafe(obj); } public static void Serialize(this T obj, Stream stream) { Serializer.Serialize(stream, obj); } public static T Deserialize(this byte[] data) { return Serializer.Deserialize(data); } public static T Deserialize(this ArraySegment data) { return Serializer.Deserialize(data); } public static T Deserialize(Stream stream) { return Serializer.Deserialize(stream); } public static byte[] CompressedSerialize(this T obj) { return Serializer.CompressedSerialize(obj); } public static void CompressedSerialize(this T obj, Stream stream) { Serializer.CompressedSerialize(stream, obj); } public static T CompressedDeserialize(this byte[] data) { return Serializer.CompressedDeserialize(data); } public static T CompressedDeserialize(Stream stream) { return Serializer.CompressedDeserialize(stream); } public static T CompressedDeserialize(this ArraySegment segment) { return Serializer.CompressedDeserialize(segment); } /// /// 把经过压缩的序列化数据放入一个指定数组中。 /// /// 返回压缩后的长度 public static int CompressedSerialize(this T obj, ref byte[] buffer,int offset) { return Serializer.CompressedSerialize(ref buffer, offset, obj); } } public static class Serializer { private static IFormatterResolver resolver = ContractlessStandardResolver.Instance; #region 正常序列化和反序列化 public static byte[] Serialize(T obj) { return MessagePackSerializer.Serialize(obj, resolver); } public static void Serialize(Stream stream, T obj) { MessagePackSerializer.Serialize(stream, obj, resolver); } public static ArraySegment SerializeUnsafe(T obj) { return MessagePackSerializer.SerializeUnsafe(obj, resolver); } public static T Deserialize(byte[] data) { return MessagePackSerializer.Deserialize(data, resolver); } public static T Deserialize(ArraySegment data) { return MessagePackSerializer.Deserialize(data, resolver); } public static T Deserialize(Stream stream) { return MessagePackSerializer.Deserialize(stream, resolver); } #endregion #region 压缩序列化和反序列化 public static byte[] CompressedSerialize(T obj) { return LZ4MessagePackSerializer.Serialize(obj, resolver); } public static void CompressedSerialize(Stream stream, T obj) { LZ4MessagePackSerializer.Serialize(stream, obj, resolver); } /// /// 把经过压缩的序列化数据放入一个指定数组中。 /// /// 返回压缩后的长度 public static int CompressedSerialize(ref byte[] buffer, int offset, T obj) { return LZ4MessagePackSerializer.SerializeToBlock(ref buffer, offset, obj, resolver); } public static T CompressedDeserialize(byte[] data) { return LZ4MessagePackSerializer.Deserialize(data, resolver); } public static T CompressedDeserialize(ArraySegment data) { return LZ4MessagePackSerializer.Deserialize(data, resolver); } public static T CompressedDeserialize(Stream stream) { return LZ4MessagePackSerializer.Deserialize(stream, resolver); } #endregion #region Json 相关 public static string ToJson(byte[] data) { return MessagePackSerializer.ToJson(data); } public static byte[] FromJson(string json) { return MessagePackSerializer.FromJson(json); } public static string CompressedToJson(byte[] data) { return LZ4MessagePackSerializer.ToJson(data); } public static byte[] CompressedFromJson(string json) { return LZ4MessagePackSerializer.FromJson(json); } #endregion } }