1.序列化为二进制流
using
System; using
System.Collections.Generic; using
System.Text; using
System.Runtime.Serialization.Formatters.Binary; using
System.IO; namespace
Common.Serialization.Byte { public
class ByteSerializer {
/// <summary>
/// 对象转换为二进制流
/// </summary>
/// <param name="obj">要序列化的对象</param>
/// <returns>对象序列化后的二进制流</returns>
public static
byte [] Serialize( object
obj) {
if (obj ==
null ) { return
null ;}
MemoryStream ms = new
MemoryStream();
try {
BinaryFormatter formatter = new
BinaryFormatter();
formatter.Serialize(ms, obj);
return ms.ToArray();
} finally
{
ms.Close();
}
}
/// <summary>
/// 二进制流转换为对象
/// </summary>
/// <param name="buffer">二进制流缓存</param>
/// <returns>反序列化后的对象</returns>
public static
object Deserialize( byte [] buffer) {
if ((buffer ==
null ) || (buffer.Length == 0)) {
return null ;
}
MemoryStream ms = new
MemoryStream(buffer);
try {
BinaryFormatter formatter = new
BinaryFormatter();
return formatter.Deserialize(ms);
} finally
{
ms.Close();
}
} } } |
2.序列化为XML
using
System; using
System.Text; using
System.IO; namespace
Common.Serialization.Xml { public
class XmlSerialization {
/// <summary>
/// 对象转换为XML字符串
/// </summary>
/// <param name="obj">要序列化的对象</param>
/// <returns>对象序列化后的XML字符串</returns>
public static
string Serialize( object
obj) {
if (obj ==
null ) {
return null ;
}
MemoryStream ms = new
MemoryStream();
try {
System.Xml.Serialization.XmlSerializer formatter =
new System.Xml.Serialization.XmlSerializer(obj.GetType());
formatter.Serialize(ms, obj);
return Encoding.Default.GetString(ms.ToArray());
} finally
{
ms.Close();
}
}
/// <summary>
/// XML字符串转换为对象
/// </summary>
/// <param name="xml">XML字符串</param>
/// <param name="targetType">要反序列化为的对象类型</param>
/// <returns>反序列化后的对象</returns>
public static
object Deserialize( string
xml, Type targetType) {
if ((xml ==
null ) || (xml.Length == 0)) {
return null ;
}
MemoryStream ms = new
MemoryStream(Encoding.Default.GetBytes(xml));
try {
System.Xml.Serialization.XmlSerializer formatter =
new System.Xml.Serialization.XmlSerializer(targetType);
return formatter.Deserialize(ms);
} finally
{
ms.Close();
}
} } } |
3.序列化为JSON
using
System; using
System.Text; using
System.IO; using
System.Runtime.Serialization.Json; namespace
Common.Serialization.Json { public
class JsonSerializer {
/// <summary>
/// 对象转换为JSON字符串
/// </summary>
/// <param name="obj">要序列化的对象</param>
/// <returns>对象序列化后的JSON字符串</returns>
public static
string Serialize( object
obj) {
if (obj ==
null ) { return
null ;}
MemoryStream ms = new
MemoryStream();
try {
DataContractJsonSerializer formatter = new
DataContractJsonSerializer(obj.GetType());
formatter.WriteObject(ms, obj);
return Encoding.UTF8.GetString(ms.ToArray());
} finally
{
ms.Close();
}
}
/// <summary>
/// JSON字符串转换为对象
/// </summary>
/// <param name="json">JSON字符串</param>
/// <param name="targetType">要反序列化为的对象类型</param>
/// <returns>反序列化后的对象</returns>
public static
object Deserialize( string
json, Type targetType) {
if ((json ==
null ) || (json.Length == 0)) { return
null ;}
MemoryStream ms = new
MemoryStream(Encoding.UTF8.GetBytes(json));
try {
DataContractJsonSerializer formatter = new
DataContractJsonSerializer(targetType);
return formatter.ReadObject(ms);
} finally
{
ms.Close();
}
} } } |