- 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介绍:http://www.cnblogs.com/jams742003/archive/2009/11/04/1595737.html