You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
164 lines
4.8 KiB
164 lines
4.8 KiB
using MessagePack; |
|
using MessagePack.Resolvers; |
|
using System; |
|
using System.IO; |
|
|
|
namespace AX.Serialization |
|
{ |
|
public static partial class SerializationExtensions |
|
{ |
|
public static ArraySegment<byte> Serialize<T>(this T obj) |
|
{ |
|
return Serializer.SerializeUnsafe(obj); |
|
} |
|
|
|
public static void Serialize<T>(this T obj, Stream stream) |
|
{ |
|
Serializer.Serialize(stream, obj); |
|
} |
|
|
|
public static T Deserialize<T>(this byte[] data) |
|
{ |
|
return Serializer.Deserialize<T>(data); |
|
} |
|
|
|
public static T Deserialize<T>(this ArraySegment<byte> data) |
|
{ |
|
return Serializer.Deserialize<T>(data); |
|
} |
|
|
|
public static T Deserialize<T>(Stream stream) |
|
{ |
|
return Serializer.Deserialize<T>(stream); |
|
} |
|
|
|
public static byte[] CompressedSerialize<T>(this T obj) |
|
{ |
|
return Serializer.CompressedSerialize<T>(obj); |
|
} |
|
|
|
public static void CompressedSerialize<T>(this T obj, Stream stream) |
|
{ |
|
Serializer.CompressedSerialize<T>(stream, obj); |
|
} |
|
|
|
public static T CompressedDeserialize<T>(this byte[] data) |
|
{ |
|
return Serializer.CompressedDeserialize<T>(data); |
|
} |
|
|
|
public static T CompressedDeserialize<T>(Stream stream) |
|
{ |
|
return Serializer.CompressedDeserialize<T>(stream); |
|
} |
|
|
|
public static T CompressedDeserialize<T>(this ArraySegment<byte> segment) |
|
{ |
|
return Serializer.CompressedDeserialize<T>(segment); |
|
} |
|
|
|
/// <summary> |
|
/// 把经过压缩的序列化数据放入一个指定数组中。 |
|
/// </summary> |
|
/// <returns>返回压缩后的长度</returns> |
|
public static int CompressedSerialize<T>(this T obj, ref byte[] buffer,int offset) |
|
{ |
|
return Serializer.CompressedSerialize<T>(ref buffer, offset, obj); |
|
} |
|
} |
|
|
|
public static class Serializer |
|
{ |
|
private static IFormatterResolver resolver = ContractlessStandardResolver.Instance; |
|
|
|
#region 正常序列化和反序列化 |
|
public static byte[] Serialize<T>(T obj) |
|
{ |
|
return MessagePackSerializer.Serialize<T>(obj, resolver); |
|
} |
|
|
|
public static void Serialize<T>(Stream stream, T obj) |
|
{ |
|
MessagePackSerializer.Serialize<T>(stream, obj, resolver); |
|
} |
|
|
|
public static ArraySegment<byte> SerializeUnsafe<T>(T obj) |
|
{ |
|
return MessagePackSerializer.SerializeUnsafe<T>(obj, resolver); |
|
} |
|
|
|
public static T Deserialize<T>(byte[] data) |
|
{ |
|
return MessagePackSerializer.Deserialize<T>(data, resolver); |
|
} |
|
|
|
public static T Deserialize<T>(ArraySegment<byte> data) |
|
{ |
|
return MessagePackSerializer.Deserialize<T>(data, resolver); |
|
} |
|
|
|
public static T Deserialize<T>(Stream stream) |
|
{ |
|
return MessagePackSerializer.Deserialize<T>(stream, resolver); |
|
} |
|
#endregion |
|
|
|
#region 压缩序列化和反序列化 |
|
public static byte[] CompressedSerialize<T>(T obj) |
|
{ |
|
return LZ4MessagePackSerializer.Serialize<T>(obj, resolver); |
|
} |
|
|
|
public static void CompressedSerialize<T>(Stream stream, T obj) |
|
{ |
|
LZ4MessagePackSerializer.Serialize<T>(stream, obj, resolver); |
|
} |
|
|
|
/// <summary> |
|
/// 把经过压缩的序列化数据放入一个指定数组中。 |
|
/// </summary> |
|
/// <returns>返回压缩后的长度</returns> |
|
public static int CompressedSerialize<T>(ref byte[] buffer, int offset, T obj) |
|
{ |
|
return LZ4MessagePackSerializer.SerializeToBlock<T>(ref buffer, offset, obj, resolver); |
|
} |
|
|
|
public static T CompressedDeserialize<T>(byte[] data) |
|
{ |
|
return LZ4MessagePackSerializer.Deserialize<T>(data, resolver); |
|
} |
|
|
|
public static T CompressedDeserialize<T>(ArraySegment<byte> data) |
|
{ |
|
return LZ4MessagePackSerializer.Deserialize<T>(data, resolver); |
|
} |
|
|
|
public static T CompressedDeserialize<T>(Stream stream) |
|
{ |
|
return LZ4MessagePackSerializer.Deserialize<T>(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 |
|
} |
|
} |