serialization, deserialization

Source: Internet
Author: User

The role of serialization and deserialization is to transform objects into data that can be stored and transmitted as well as reversed (binary, SOAP, XML, JSON, and so on) for preservation, transmission, and reversal processes.

1, object: This object can be a class, file, video, running games and so on a series of content

2. Serialization: An action that transforms an object into data (binary, SOAP, XML, JSON, and so on)

3. Storage and transport: the basis of the serialized data can be stored or transmitted to other programs (SOAP, WEBAPI, WCF, and existing popular Ajax, etc.), which is the process of serializing the transmission

4, deserialization: is the receiver, or the use of the serialized data, reverse the process of restoration

The current mainstream process is about 4 kinds:

1, JSON (file smaller, when complex when not easy to read)

2, XML (easy to read, file small)

3, SOAP (file is too large, not easy to read)

4, binary (file small, can not read)

JSON can be parsed only once, XML may be partitioned

They have advantages and disadvantages, applicability is not the same, I only know a little bit simple. A deep dive requires a reader to do

Code:

usingNewtonsoft.json;usingSystem;usingSystem.Collections.Generic;usingSystem.IO;usingSystem.Linq;usingSystem.Runtime.Serialization.Formatters.Binary;usingSystem.Runtime.Serialization.Formatters.Soap;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Xml;usingSystem.Xml.Serialization;namespaceserialize{/// <summary>    ///JSON serialization Helper/// </summary>     Public classJsonserializehelper { Public StaticT deserialize<t> (stringContentwhereT:class,New()        {            returnJsonconvert.deserializeobject<t>(content); }         Public Static stringserialize<t> (T obj)whereT:class,New()        {            returnjsonconvert.serializeobject (obj); }         Public Static voidSerialize<t, s> (T obj, S stream)whereS:streamwhereT:class,New()        {            using(stream) {byte[] content =System.Text.Encoding.UTF8.GetBytes (Jsonconvert.serializeobject (obj)); Stream. Write (Content,0, content.            Length); }        }    }    /// <summary>    ///XML Serialization Helper/// </summary>     Public classXmlserializehelper { Public StaticT deserialize<t, s> (S stream)whereS:streamwhereT:class,New()        {            using(stream) {XmlSerializer Xmlsearializer=NewXmlSerializer (typeof(T)); //filestream.position = 0; //filestream.seek (0, seekorigin.begin);                return(T) xmlsearializer.deserialize (stream); }        }         Public Static voidSerialize<t, s> (T obj, S stream)whereS:streamwhereT:class,New()        {            using(stream) {XmlSerializer Xmlformat=NewXmlSerializer (typeof(T));            Xmlformat.serialize (stream, obj); }        }    }    /// <summary>    ///Binary Serialization Helper/// </summary>     Public classBinaryserializehelper { Public StaticT deserialize<t, s> (S stream)whereS:streamwhereT:class,New()        {            using(stream) {BinaryFormatter Formatter=NewBinaryFormatter (); return(T) formatter.            Deserialize (stream); }        }         Public Static voidSerialize<t, s> (T obj, S stream)whereS:streamwhereT:class,New()        {            using(stream) {BinaryFormatter Formatter=NewBinaryFormatter (); Formatter.            Serialize (stream, obj); }        }    }    /// <summary>    ///SOAP Serialization deserialization helper/// </summary>     Public classSoapserializehelper { Public StaticT deserialize<t, s> (S stream)whereS:streamwhereT:class,New()        {            using(stream) {SoapFormatter formatter=NewSoapFormatter (); return(T) formatter.            Deserialize (stream); }        }         Public Static voidSerialize<t, s> (T obj, S stream)whereS:streamwhereT:class,New()        {            using(stream) {SoapFormatter formatter=NewSoapFormatter (); Formatter.            Serialize (stream, obj); }        }    }}

serialization, deserialization

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.