//------------------------------------------------------------------------------ // 此代码版权(除特别声明或在XREF结尾的命名空间的代码)归作者本人若汝棋茗所有 // 源代码使用协议遵循本仓库的开源协议及附加协议,若本仓库没有设置,则按MIT开源协议授权 // CSDN博客:https://blog.csdn.net/qq_40374647 // 哔哩哔哩视频:https://space.bilibili.com/94253567 // Gitee源代码仓库:https://gitee.com/RRQM_Home // Github源代码仓库:https://github.com/RRQM // API首页:https://www.yuque.com/rrqm/touchsocket/index // 交流QQ群:234762506 // 感谢您的下载和使用 //------------------------------------------------------------------------------ //------------------------------------------------------------------------------ using System; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Binary; using System.Text; using System.Xml.Serialization; namespace TouchSocket.Core { /// /// 高性能序列化器 /// //[IntelligentCoder.AsyncMethodPoster(Flags = IntelligentCoder.MemberFlags.Public)] public static partial class SerializeConvert { #pragma warning disable SYSLIB0011 // 微软觉得不安全,不推荐使用 #region 普通二进制序列化 /// /// 普通二进制序列化对象 /// /// 数据对象 /// public static byte[] BinarySerialize(object obj) { using (MemoryStream serializeStream = new MemoryStream()) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(serializeStream, obj); return serializeStream.ToArray(); } } /// /// 二进制序列化对象至文件 /// /// 数据对象 /// 路径 public static void BinarySerializeToFile(object obj, string path) { using (FileStream serializeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(serializeStream, obj); serializeStream.Close(); } } /// /// 二进制序列化对象 /// /// /// public static void BinarySerialize(Stream stream, object obj) { BinaryFormatter bf = new BinaryFormatter(); bf.Serialize(stream, obj); } #endregion 普通二进制序列化 #region 普通二进制反序列化 /// /// 从Byte[]中反序列化 /// /// /// /// /// /// /// public static T BinaryDeserialize(byte[] data, int offset, int length, SerializationBinder binder = null) { using (MemoryStream DeserializeStream = new MemoryStream(data, offset, length)) { DeserializeStream.Position = 0; BinaryFormatter bf = new BinaryFormatter(); if (binder != null) { bf.Binder = binder; } return (T)bf.Deserialize(DeserializeStream); } } /// /// 反序列化 /// /// /// /// /// /// public static object BinaryDeserialize(byte[] data, int offset, int length, SerializationBinder binder = null) { using (MemoryStream DeserializeStream = new MemoryStream(data, offset, length)) { DeserializeStream.Position = 0; BinaryFormatter bf = new BinaryFormatter(); if (binder != null) { bf.Binder = binder; } return bf.Deserialize(DeserializeStream); } } /// /// 从Stream中反序列化 /// /// /// /// /// public static T BinaryDeserialize(Stream stream, SerializationBinder binder = null) { BinaryFormatter bf = new BinaryFormatter(); if (binder != null) { bf.Binder = binder; } return (T)bf.Deserialize(stream); } /// /// 将二进制文件数据反序列化为指定类型对象 /// /// /// /// public static T BinaryDeserializeFromFile(string path) { using (FileStream serializeStream = new FileStream(path, FileMode.Open, FileAccess.Read)) { BinaryFormatter bf = new BinaryFormatter(); return (T)bf.Deserialize(serializeStream); } } /// /// 将二进制数据反序列化为指定类型对象 /// /// /// /// public static T BinaryDeserialize(byte[] data) { return BinaryDeserialize(data, 0, data.Length); } /// /// 从Byte[]中反序列化 /// /// /// /// /// public static T BinaryDeserialize(byte[] data, SerializationBinder binder = null) { return BinaryDeserialize(data, 0, data.Length, binder); } #endregion 普通二进制反序列化 #pragma warning restore SYSLIB0011 // 微软觉得不安全,不推荐使用 #region Fast二进制序列化 /// /// Fast二进制序列化对象 /// /// /// /// public static void FastBinarySerialize(ByteBlock stream, T obj) { FastBinaryFormatter.Serialize(stream, obj); } /// /// Fast二进制序列化对象 /// /// /// public static byte[] FastBinarySerialize(T obj) { using (ByteBlock byteBlock = new ByteBlock()) { FastBinarySerialize(byteBlock, obj); return byteBlock.ToArray(); } } #endregion Fast二进制序列化 #region Fast二进制反序列化 /// /// Fast反序列化 /// /// /// /// /// public static T FastBinaryDeserialize(byte[] data, int offset) { return (T)FastBinaryFormatter.Deserialize(data, offset, typeof(T)); } /// /// Fast反序列化 /// /// /// /// /// public static object FastBinaryDeserialize(byte[] data, int offset, Type type) { return FastBinaryFormatter.Deserialize(data, offset, type); } /// /// 从Byte[]中反序列化 /// /// /// /// public static T FastBinaryDeserialize(byte[] data) { return FastBinaryDeserialize(data, 0); } #endregion Fast二进制反序列化 #region Xml序列化和反序列化 /// /// Xml序列化数据对象 /// /// 数据对象 /// 编码格式 /// public static string XmlSerializeToString(object obj, Encoding encoding) { return encoding.GetString(XmlSerializeToBytes(obj)); } /// /// Xml序列化数据对象 /// /// 数据对象 /// public static string XmlSerializeToString(object obj) { return XmlSerializeToString(obj, Encoding.UTF8); } /// /// Xml序列化数据对象 /// /// 数据对象 /// public static byte[] XmlSerializeToBytes(object obj) { using (MemoryStream fileStream = new MemoryStream()) { XmlSerializer xml = new XmlSerializer(obj.GetType()); xml.Serialize(fileStream, obj); return fileStream.ToArray(); } } /// /// Xml序列化至文件 /// /// /// public static void XmlSerializeToFile(object obj, string path) { using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { XmlSerializer xml = new XmlSerializer(obj.GetType()); xml.Serialize(fileStream, obj); fileStream.Close(); } } /// /// Xml反序列化 /// /// 反序列化类型 /// 数据 /// public static T XmlDeserializeFromBytes(byte[] datas) { XmlSerializer xmlserializer = new XmlSerializer(typeof(T)); using (Stream xmlstream = new MemoryStream(datas)) { return (T)xmlserializer.Deserialize(xmlstream); } } /// /// Xml反序列化 /// /// /// /// public static object XmlDeserializeFromBytes(byte[] datas, Type type) { XmlSerializer xmlserializer = new XmlSerializer(type); using (Stream xmlstream = new MemoryStream(datas)) { return xmlserializer.Deserialize(xmlstream); } } /// /// Xml反序列化 /// /// 类型 /// xml字符串 /// 编码格式 /// public static T XmlDeserializeFromString(string xmlString, Encoding encoding) { XmlSerializer xmlserializer = new XmlSerializer(typeof(T)); using (Stream xmlstream = new MemoryStream(encoding.GetBytes(xmlString))) { return (T)xmlserializer.Deserialize(xmlstream); } } /// /// Xml反序列化 /// /// 类型 /// xml字符串 /// public static T XmlDeserializeFromString(string json) { return XmlDeserializeFromString(json, Encoding.UTF8); } /// /// Xml反序列化 /// /// 反序列化类型 /// 文件路径 /// public static T XmlDeserializeFromFile(string path) { using (Stream xmlstream = new FileStream(path, FileMode.Open, FileAccess.Read)) { XmlSerializer xmlserializer = new XmlSerializer(typeof(T)); return (T)xmlserializer.Deserialize(xmlstream); } } #endregion Xml序列化和反序列化 #region Json序列化和反序列化 /// /// 首先使用NewtonsoftJson.默认True。 /// /// 当设置True时,json序列化会优先使用NewtonsoftJson(需要将dll加载到程序)。 /// 当设置为FALSE,或者NewtonsoftJson不可用时,netstandard2.0和net45平台将使用。 /// 其他平台将使用System.Text.Json。 /// /// public static bool NewtonsoftJsonFirst { get; set; } = true; /// /// 判断是否支持NewtonsoftJson /// public static bool NewtonsoftJsonIsSupported => JsonNet.IsSupported; /// /// 主动载入NewtonsoftJson。 /// /// 传入命名为JsonConvert的类型 /// public static bool LoadNewtonsoftJson(Type jsonConvertType) { return JsonNet.InitJsonNet(jsonConvertType); } /// /// 转换为Json /// /// /// public static string ToJson(this object item) { if (NewtonsoftJsonFirst && JsonNet.IsSupported) { return JsonNet.SerializeObject(item); } #if NETCOREAPP3_1_OR_GREATER return System.Text.Json.JsonSerializer.Serialize(item); #else return JsonFastConverter.JsonTo(item); #endif } /// /// 从字符串到json /// /// /// /// public static object FromJson(this string json, Type type) { if (NewtonsoftJsonFirst && JsonNet.IsSupported) { return JsonNet.DeserializeObject(json, type); } #if NETCOREAPP3_1_OR_GREATER return System.Text.Json.JsonSerializer.Deserialize(json,type); #else return JsonFastConverter.JsonFrom(json, type); #endif } /// /// 从字符串到json /// /// /// /// public static T FromJson(this string json) { return (T)FromJson(json, typeof(T)); } /// /// Json序列化数据对象 /// /// 数据对象 /// public static byte[] JsonSerializeToBytes(object obj) { return ToJson(obj).ToUTF8Bytes(); } /// /// Json序列化至文件 /// /// /// public static void JsonSerializeToFile(object obj, string path) { using (FileStream fileStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.ReadWrite)) { var date = JsonSerializeToBytes(obj); fileStream.Write(date, 0, date.Length); fileStream.Close(); } } /// /// Json反序列化 /// /// 反序列化类型 /// 数据 /// public static T JsonDeserializeFromBytes(byte[] datas) { return (T)JsonDeserializeFromBytes(datas, typeof(T)); } /// /// Xml反序列化 /// /// /// /// public static object JsonDeserializeFromBytes(byte[] datas, Type type) { return FromJson(Encoding.UTF8.GetString(datas), type); } /// /// Json反序列化 /// /// 类型 /// json字符串 /// public static T JsonDeserializeFromString(string json) { return FromJson(json); } /// /// Json反序列化 /// /// 反序列化类型 /// 文件路径 /// public static T JsonDeserializeFromFile(string path) { return JsonDeserializeFromString(File.ReadAllText(path)); } #endregion Json序列化和反序列化 } }