Serialization and deserialization

Source: Internet
Author: User

I am a newbie, and many technologies are the first to come into contact. I learned something after learning. I wrote this article to record what I learned about these things, if there is anything wrong or imperfect, I hope you will give more advice.

I have learned something about serialization and deserialization over the past few days. To tell the truth, I didn't even notice this content at school. Today I have come into use to understand its practicality, first, let me explain my understanding of this. In general, serialization and deserialization are applied to cross-origin operations. For example, data transmission between multiple servers or clients and servers converts large objects into XML, soap, in Json or Binary format, my image is interpreted as if we want to put a large ball of wool through a very fine pipe, hard stuffing will certainly cause congestion, if we split the wool into a line, it will be much easier to pass it over. After all the wool lines are passed in, we will roll them into a group. Splitting the wool will be serialized, it is deserialization to roll the wool into a group, and so is the data application.

Next I will talk about the implementation process of serialization and deserialization. I wrote three simple examples to illustrate how this is implemented, the technical requirements are not very high. A programmer can understand it. By the way, I use the C # language, and the implementation process of other languages is not all written, first, I don't have any technical skills, and second, I think it is easier to understand other languages.

The first is the positive and negative serialization of XML. I think there are a lot of people who should use XML. In my opinion, if I need to insert a few data with the same attributes but different attributes, I have to re-write all the attributes, that is to say, there will be a lot of duplicate data in the XML, so it is more troublesome, and it will also cause a large XML, (It is said that soap will be bigger ...), however, XML is quite human-readable and easy to write, so it is easier for people to read it. Before Json is available, I think there should be a lot of XML, after all, the first thing I learned before is XML. In C #, the serialization of XML must reference System. xml. xmlSerializer class in Serialization. To create a new object of this class, you must input the type of the target object class, that is, typeof (Class Name of the target object class ), by the way, I forgot to mention that the target object class must have the [Serializable] feature. Otherwise, it cannot be serialized. If you do not need to serialize an attribute in the object class, you only need to add the [NonSerialized] attribute to this attribute. After creating an object, we can use the Serialize (Stream, object) method to Serialize the object. My writing method is to write the serialized content into a text file, the following is the instance code:

ObjectInfo info = new objectInfo ();

XmlSerializer xs = new XmlSerializer (typeof (objectInfo ));
String path = Server. MapPath ("webinfoxml.txt ");
Try
{
Stream file = new FileStream (path, FileMode. Create, FileAccess. Write );
Xs. Serialize (file, info );
File. Close ();
File. Dispose ();
Response. Write ("serialized xml format successful ");
}

Then there is the implementation of deserialization. With the previous text, we can perform deserialization Based on this. First, we can store the text content into a stream, then create a new object for the XmlSerializer class, and then use the Deserialize (Stream) method to implement deserialization. The following is the instance code:

ObjectInfo info = new objectInfo ();
XmlSerializer xs = new XmlSerializer (typeof (objectInfo ));

String path = Server. MapPath ("webinfoxml.txt ");
Stream file = new FileStream (path, FileMode. Open, FileAccess. Read );
Info = (objectInfo) xs. Deserialize (file );

In this way, the data in the stream is deserialized into the object objectInfo, and then the operation on the object is completed.

The usage of Binary and Json is similar. The advantage of Binary is that its size is the smallest in these methods after serialization, and the transmission speed is also the fastest, to implement Binary serialization in C #, System is required. runtime. serialization. formatters. the BinaryFormatter class in Binary is also used to create new objects. This class also contains the Serialize (Stream, object) method. The following is the instance code:

ObjectInfo info = new objectInfo ();
BinaryFormatter bf = new BinaryFormatter ();
String path = Server. MapPath ("webinfoBinary.txt ");
FileStream fs = new FileStream (path, FileMode. Create );
Bf. Serialize (fs, info );
Fs. Close ();
Response. Write ("serialized Binary format successful ");

Then there is the deserialization instance code:

ObjectInfo info = new objectInfo ();
BinaryFormatter bf = new BinaryFormatter ();
String path = Server. MapPath ("webinfoBinary.txt ");
Stream file = new FileStream (path, FileMode. Open, FileAccess. Read );
Info = (objectInfo) bf. Deserialize (file );

One major difference between Json and XML is that Json can greatly reduce the complexity of array expression. If the XML structure is suitable for reading, the Json structure is more suitable for machine reading. Json is equivalent to a precise data block. Such a structure not only inherits the advantages of XML, but also has a huge volume like XML, it is a very practical structure. Json is used in java at the beginning. To implement Json in C #, You need to reference System. web. script. the JavaScriptSerializer class in Serialization is also used to create a new object. The difference is that the parameter passed in Serialize (object) in JavaScriptSerializer does not have a Stream, and it returns a string. The following is the instance code:

ObjectInfo info = new objectInfo ();
JavaScriptSerializer jss = new JavaScriptSerializer ();
String path = Server. MapPath ("webinfoJson.txt ");
Try
{
FileStream fs = new FileStream (path, FileMode. Create, FileAccess. Write );
StreamWriter sw = new StreamWriter (fs );
Sw. Write (jss. Serialize (info ));
Sw. Close ();
Sw. Dispose ();
Response. Write ("serialized json format succeeded ");
Response. Write (jss. Serialize (info ));
}

Then there is the deserialization instance code:

ObjectInfo info = new objectInfo ();
JavaScriptSerializer jss = new JavaScriptSerializer ();
String path = Server. MapPath ("webinfoJson.txt ");
StreamReader rd = new StreamReader (path, System. Text. Encoding. UTF8 );
String aa = rd. ReadToEnd ();
Info = (objectInfo) jss. Deserialize (aa, typeof (objectInfo ));

The above is my experience in serialization and deserialization. I just published it as my own record. There will certainly be many shortcomings and unexpected points. I hope you can give me some advice in a timely manner, in the future, I will also work harder to learn new technologies and new knowledge, and then write my experiences into my blog so that I can communicate with you a lot.

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.