- 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
- }
- }
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 }}
These are all created on the class libraries provided by Microsoft. The namespace must be referenced during the call.
Tests on the Internet show that json.net is at least twice faster than the JSON speed provided by Microsoft, and supports LINQ to JSON. For more information, see:
Json.net Official Website: http://james.newtonking.com/projects/json-net.aspx
Json.net Introduction: http://www.cnblogs.com/jams742003/archive/2009/11/04/1595737.html