using MessagePack; using System; namespace HaBookCms.Common.JsonHelper { public class MessagePackHelper { /// /// Json字符串转Byte /// /// /// byte[] public static byte[] JsonToByte(string json) { return LZ4MessagePackSerializer.FromJson(json); } /// /// Json字符串转对象 /// /// /// /// T public static T JsonToObject(string json){ Type type = typeof(T); return (T)LZ4MessagePackSerializer.NonGeneric.Deserialize(type, JsonToByte(json)); } /// /// 对象转Byte /// /// /// byte[] public static byte[] ObjectToByte(Object obj) { return LZ4MessagePackSerializer.Serialize(obj); } /// /// 对象转Json /// /// /// string public static string ObjectToJson(Object obj) { return LZ4MessagePackSerializer.ToJson(ObjectToByte(obj)); } /// /// byte转Json /// /// /// public static string byteToJson(byte[] bt) { return LZ4MessagePackSerializer.ToJson(bt); } /// /// byte转对象 /// /// /// /// public static T byteToObject(byte[] bt) { Type type = typeof(T); return (T)LZ4MessagePackSerializer.NonGeneric.Deserialize(type, bt); } } }