.net序列化与反序列化包括XML,SOAP,Binary,JSON
2010-06-22 14:41
447 查看
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
using System.Runtime.Serialization.Json;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace Common.Serialization
{
class SerializeHelper
{
#region XmlSerializer
public string ToXml<T>(T item)
{
XmlSerializer serializer = new XmlSerializer(item.GetType());
StringBuilder sb = new StringBuilder();
using (XmlWriter writer = XmlWriter.Create(sb))
{
serializer.Serialize(writer, item);
return sb.ToString();
}
}
public T FromXml<T>(string str)
{
XmlSerializer serializer = new XmlSerializer(typeof(T));
using (XmlReader reader = new XmlTextReader(new StringReader(str)))
{
return (T)serializer.Deserialize(reader);
}
}
#endregion
#region BinaryFormatter
public string ToBinary<T>(T item)
{
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
formatter.Serialize(ms, item);
ms.Position = 0;
byte[] bytes = ms.ToArray();
StringBuilder sb = new StringBuilder();
foreach (byte bt in bytes)
{
sb.Append(string.Format("{0:X2}", bt));
}
return sb.ToString();
}
}
public T FromBinary<T>(string str)
{
int intLen = str.Length / 2;
byte[] bytes = new byte[intLen];
for (int i = 0; i < intLen; i++)
{
int ibyte = Convert.ToInt32(str.Substring(i * 2, 2), 16);
bytes[i] = (byte)ibyte;
}
BinaryFormatter formatter = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream(bytes))
{
return (T)formatter.Deserialize(ms);
}
}
#endregion
#region SoapFormatter
public string ToSoap<T>(T item)
{
SoapFormatter formatter = new SoapFormatter();
using (MemoryStream ms = new MemoryStream())
{
formatter.Serialize(ms, item);
ms.Position = 0;
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(ms);
return xmlDoc.InnerXml;
}
}
public T FromSoap<T>(string str)
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(str);
SoapFormatter formatter = new SoapFormatter();
using (MemoryStream ms = new MemoryStream())
{
xmlDoc.Save(ms);
ms.Position = 0;
return (T)formatter.Deserialize(ms);
}
}
#endregion
#region JsonSerializer
public string ToJson<T>(T item)
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType());
using (MemoryStream ms = new MemoryStream())
{
serializer.WriteObject(ms, item);
return Encoding.UTF8.GetString(ms.ToArray());
}
}
public T FromJson<T>(string str) where T : class
{
DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str)))
{
return serializer.ReadObject(ms) as T;
}
}
#endregion
}
}
以上这些都是在微软提供的类库上创建的,调用时需要引用命名空间。
网上相关测试表明,JSON.net速度至少强于微软提供的JSON速度2倍,且支持Linq to JSON,如有需要请参见:
JSON.net官方网站:http://james.newtonking.com/projects/json-net.aspx
JSON.net介绍:/article/4994235.html
dp.SyntaxHighlighter.ClipboardSwf = '/Common/dp.SyntaxHighlighter/clipboard.swf';dp.SyntaxHighlighter.HighlightAll('code');
using System; using System.IO; using System.Runtime.Serialization.Formatters.Binary; using System.Runtime.Serialization.Formatters.Soap; using System.Runtime.Serialization.Json; using System.Text; using System.Xml; using System.Xml.Serialization; namespace Common.Serialization { class SerializeHelper { #region XmlSerializer public string ToXml<T>(T item) { XmlSerializer serializer = new XmlSerializer(item.GetType()); StringBuilder sb = new StringBuilder(); using (XmlWriter writer = XmlWriter.Create(sb)) { serializer.Serialize(writer, item); return sb.ToString(); } } public T FromXml<T>(string str) { XmlSerializer serializer = new XmlSerializer(typeof(T)); using (XmlReader reader = new XmlTextReader(new StringReader(str))) { return (T)serializer.Deserialize(reader); } } #endregion #region BinaryFormatter public string ToBinary<T>(T item) { BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream()) { formatter.Serialize(ms, item); ms.Position = 0; byte[] bytes = ms.ToArray(); StringBuilder sb = new StringBuilder(); foreach (byte bt in bytes) { sb.Append(string.Format("{0:X2}", bt)); } return sb.ToString(); } } public T FromBinary<T>(string str) { int intLen = str.Length / 2; byte[] bytes = new byte[intLen]; for (int i = 0; i < intLen; i++) { int ibyte = Convert.ToInt32(str.Substring(i * 2, 2), 16); bytes[i] = (byte)ibyte; } BinaryFormatter formatter = new BinaryFormatter(); using (MemoryStream ms = new MemoryStream(bytes)) { return (T)formatter.Deserialize(ms); } } #endregion #region SoapFormatter public string ToSoap<T>(T item) { SoapFormatter formatter = new SoapFormatter(); using (MemoryStream ms = new MemoryStream()) { formatter.Serialize(ms, item); ms.Position = 0; XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(ms); return xmlDoc.InnerXml; } } public T FromSoap<T>(string str) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.LoadXml(str); SoapFormatter formatter = new SoapFormatter(); using (MemoryStream ms = new MemoryStream()) { xmlDoc.Save(ms); ms.Position = 0; return (T)formatter.Deserialize(ms); } } #endregion #region JsonSerializer public string ToJson<T>(T item) { DataContractJsonSerializer serializer = new DataContractJsonSerializer(item.GetType()); using (MemoryStream ms = new MemoryStream()) { serializer.WriteObject(ms, item); return Encoding.UTF8.GetString(ms.ToArray()); } } public T FromJson<T>(string str) where T : class { DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(T)); using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(str))) { return serializer.ReadObject(ms) as T; } } #endregion } }
以上这些都是在微软提供的类库上创建的,调用时需要引用命名空间。
网上相关测试表明,JSON.net速度至少强于微软提供的JSON速度2倍,且支持Linq to JSON,如有需要请参见:
JSON.net官方网站:http://james.newtonking.com/projects/json-net.aspx
JSON.net介绍:/article/4994235.html
dp.SyntaxHighlighter.ClipboardSwf = '/Common/dp.SyntaxHighlighter/clipboard.swf';dp.SyntaxHighlighter.HighlightAll('code');
相关文章推荐
- .NET 中对 PHP、Binary、SOAP、XML、JSON、Hessian 等序列化的效率对比
- .NET 对象的序列化与反序列化 Binary,Soap,Xml
- C# 序列化(Binary、Xml、Soap)
- c#序列化反序列化工具(json,binary,xml)
- [ServiceStack.Text] .net序列化,反序列化组件(号称最快,支持 JSON,XML,JSV格式)
- 序列化对象,使用的三种方式:Xml,Binary,Soap
- DataContractSerializer vs XmlSerializer vs DataContractJsonSerializer vs SoapFormatter vs BinaryFormatter 性能测试对比
- .NET Framewok 3.5 中 JSON 序列化和反序列化的简单实现
- 序列化与反序列化 - BinaryFormatter二进制(.dat)、SoapFormatter(.soap)、XmlSerializer(.xml)
- .Net的精髓——XML和SOAP
- C#中使用JSON.NET实现JSON、XML相互转换
- 在.net中序列化读写xml方法的总结
- WCF(10):序列化(DataContractSerializer, XmlSerializer, DataContractJsonSerializer, SoapFormatter, Binary
- python3----模块(序列化(json&pickle)+XML+requests)
- 新开张,写一些关于.net的东西!包括xml
- Json序列化之.NET开源类库Newtonsoft.Json的研究
- .NET对象的XML序列化和反序列化
- [.net 面向对象程序设计进阶] (12) 序列化(Serialization)(四) 快速掌握JSON的序列化和反序列化
- Json.Net系列教程 3.Json.Net序列化和反序列化设置
- 以读取博客园随笔备份为例 将xml 序列化成json,再序列化成对象