序列化帮助类
序列化实是将对象信息转换成制定格式(二进制、json或者xml等格式)的数据。
反序列化则是将制定格式(二进制、json或者xml等格式)的数据转换成对象信息
注:序列化与反序列化前后的对象不是同一个对象。因为反序列化相当于使用序列数据重新创建了新的对象实体。
序列化帮助类
1 using System; 2 using System.Collections.Generic; 3 using System.Threading.Tasks; 4 using System.Runtime.Serialization; 5 using System.ServiceModel; 6 using System.IO; 7 using System.Runtime.Serialization.Json; 8 using System.Xml.Serialization; 9 using System.Runtime.Serialization.Formatters.Binary; 10 using System.Runtime.Serialization.Formatters.Soap; 11 using System.Text; 12 13 namespace Common 14 { 15 /// <summary> 16 /// 序列化帮助类 17 /// </summary> 18 public sealed class SerializeHelper 19 { 20 #region DataContract序列化 21 /// <summary> 22 /// DataContract序列化 23 /// DataContract标记 DataMember标记 24 /// </summary> 25 /// <param name="value"></param> 26 /// <param name="knowTypes"></param> 27 /// <returns></returns> 28 public static string SerializeDataContract(object value, List<Type> knowTypes = null) 29 { 30 DataContractSerializer dataContractSerializer = new DataContractSerializer(value.GetType(), knownTypes); 31 using (MemoryStream ms = new MemoryStream()) 32 { 33 dataContractSerializer.WriteObject(ms, value); 34 ms.Seek(0, SeekOrigin.Begin); 35 using (StreamReader sr = new StreamReader(ms)) 36 { 37 return sr.ReadToEnd(); 38 } 39 } 40 } 41 /// <summary> 42 /// DataContract反序列化 43 /// DataContract标记 DataMember标记 44 /// </summary> 45 /// <typeparam name="T"></typeparam> 46 /// <param name="xml"></param> 47 /// <returns></returns> 48 public static T DeserializeDataContract<T>(string xml) 49 { 50 using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xml))) 51 { 52 DataContractSerializer dataContractSerializer = new DataContractSerializer(typeof(T)); 53 return (T)dataContractSerializer.ReadObject(ms); 54 } 55 } 56 #endregion 57 #region Json序列化 58 /// <summary> 59 /// Json序列化 60 /// </summary> 61 /// <param name="value"></param> 62 /// <returns></returns> 63 public static string SerializDataContractJson(object value) 64 { 65 DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(value.GetType()); 66 using (MemoryStream ms = new MemoryStream()) 67 { 68 dataContractJsonSerializer.WriteObject(ms, value); 69 return Encoding.UTF8.GetString(ms.ToArray()); 70 } 71 } 72 /// <summary> 73 /// Json反序列化 74 /// </summary> 75 /// <param name="type"></param> 76 /// <param name="value"></param> 77 /// <returns></returns> 78 public static object DeserializeDataContractJson(Type type, string value) 79 { 80 DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(type); 81 using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(value))) 82 { 83 return dataContractJsonSerializer.ReadObject(ms); 84 } 85 } 86 87 #endregion 88 #region xml序列化 89 /// <summary> 90 /// xml序列化 91 /// </summary> 92 /// <param name="value"></param> 93 /// <returns></returns> 94 public static string SerializeXml(object value) 95 { 96 XmlSerializer xmlSerializer = new XmlSerializer(value.GetType()); 97 using (MemoryStream ms = new MemoryStream()) 98 { 99 xmlSerializer.Serialize(ms, value); 100 ms.Seek(0, SeekOrigin.Begin); 101 using (StreamReader sr = new StreamReader(ms)) 102 { return sr.ReadToEnd(); } 103 } 104 } 105 /// <summary> 106 /// xml反序列化 107 /// </summary> 108 /// <param name="type"></param> 109 /// <param name="value"></param> 110 /// <returns></returns> 111 public static object DeserializeXml(Type type, string value) 112 { 113 XmlSerializer xmlSerialzer = new XmlSerializer(type); 114 using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(value))) 115 { 116 return xmlSerialzer.Deserialize(ms); 117 } 118 } 119 #endregion 120 #region Binary 121 /// <summary> 122 /// Binary序列化 123 /// 必须类型必须标记为Serializable 124 /// </summary> 125 /// <param name="value"></param> 126 /// <returns></returns> 127 public static string SerializeBinaryFormatter(object value) 128 { 129 BinaryFormatter binaryFormatter = new BinaryFormatter(); 130 using (MemoryStream ms = new MemoryStream()) 131 { 132 binaryFormatter.Serialize(ms, value); 133 return Encoding.Default.GetString(ms.ToArray()); 134 } 135 } 136 /// <summary> 137 /// Binary反序列化 138 /// T 必须类型必须标记为Serializable 139 /// </summary> 140 /// <typeparam name="T"></typeparam> 141 /// <param name="value"></param> 142 /// <returns></returns> 143 public static T DeserializeBinaryFormatter<T>(string value) 144 { 145 BinaryFormatter binaryFormatter = new BinaryFormatter(); 146 using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(value))) 147 { 148 return (T)binaryFormatter.Deserialize(ms); 149 } 150 } 151 #endregion 152 #region SoapFormatter序列化 153 /// <summary> 154 /// soap序列化 155 /// value 必须类型必须标记为Serializable 156 /// </summary> 157 /// <param name="value"></param> 158 /// <returns></returns> 159 public static string SerializeSoapFormatter(object value) 160 { 161 SoapFormatter soapFormatter = new SoapFormatter(); 162 using (MemoryStream ms = new MemoryStream()) 163 { 164 soapFormatter.Serialize(ms, value); 165 return Encoding.UTF8.GetString(ms.ToArray()); 166 } 167 } 168 /// <summary> 169 /// soap反序列化 170 /// 必须类型必须标记为Serializable 171 /// </summary> 172 /// <typeparam name="T"></typeparam> 173 /// <param name="value"></param> 174 /// <returns></returns> 175 public static T DeserializeSoapFormatter<T>(string value) 176 { 177 SoapFormatter soapFormatter = new SoapFormatter(); 178 using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(value))) 179 { 180 return (T)soapFormatter.Deserialize(ms); 181 } 182 } 183 #endregion 184 185 } 186 }
json序列化帮助类(以下对Newtonsoft进行简单封装)
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Threading.Tasks; 6 using Newtonsoft.Json; 7 namespace Common 8 { 9 public class JsonHelper 10 { 11 /// <summary> 12 /// 13 /// </summary> 14 /// <param name="obj"></param> 15 /// <returns></returns> 16 public static string Serial (object obj) 17 { 18 return JsonConvert.SerializeObject(obj); 19 } 20 /// <summary> 21 /// 22 /// </summary> 23 /// <typeparam name="T"></typeparam> 24 /// <param name="str"></param> 25 /// <returns></returns> 26 public static T Deserial<T>(string str) 27 { 28 return JsonConvert.DeserializeObject<T>(str); 29 } 30 } 31 }