Javascript-serialization and deserialization are often heard. What is the purpose of this?

Source: Internet
Author: User
The question may be very extensive, because I don't understand it, so I asked this question. Can I solve it in several ways. Where is serialization and deserialization generally used? How does one use it? What are the benefits of using this feature? The question may be very extensive, because I don't understand it, so I asked this question. Can I solve it in several ways.

Where is serialization and deserialization generally used?
How does one use it?
What are the benefits of using this feature?

Reply content:

The question may be very extensive, because I don't understand it, so I asked this question. Can I solve it in several ways.

Where is serialization and deserialization generally used?
How does one use it?
What are the benefits of using this feature?

You also know that this problem is common. If I want to answer your question, I will definitely go to Baidu Yizi materials and paste them here. Why? Why don't you go to Baidu?
Google and Baidu are the best teachers.

The most common scenario should be persistence. For example

This is when you create an object or an array,
After a certain operation is executed, you can save it and use it again,
You play the game as you go offline. The next time you go online, you start playing the game. You don't have to apply for an account and upgrade it again;

There is also interaction between multiple languages. For example, if you want to eat chicken in a foreign restaurant and you can't understand Chinese,
You won't say what he understands,
You drew a chicken. You know it, and he knows it. The chicken is serialized...

In simple terms, computers communicate in binary mode. Therefore, you need to serialize the objects in the memory into binary data, and the receiver then deserializes them into objects.

In fact, the application scenario is very simple. You write a memory object to the socket at one end, and the receiving end will directly use this object after receiving it. Serialization is followed by deserialization. There are many ways to complete this function, such as the simplest json format. httpserver will nest its own data and dump the written json string to the browser, the browser js Code directly converts the received data into a js data structure and transparently converts the received string to an object.

For example, if you want to use localStorage, the data to be stored is an object, but localStorage can only store strings. What should you do? One way is to serialize the object into a json string. When you want to use the data in localStorage, You Can deserialize the json string into a js object. Do you understand?

The root cause is that you do not understand network programming!
Whether it is inter-process and inter-thread communication, the client and server communicate through socket, or write the object to db (actually socket ).
These network interactions are all through sending and receiving binary streams. Therefore, when sending, the object must be serialized and the binary data must be sent. Then, the receiver receives the binary data and then deserializes the data into an object.
First, you need to understand what you don't understand.

The conventions are as follows: if the Protocols, formats, and specifications are defined as special and runtime objects are defined as common objects, common objects are stored in memory that is difficult to interact, it is particularly useful for other interactions (for example, java objects are common in the Java ecosystem, and json format is special in the Java ecosystem ).

From this serialization, "general" is converted into "special", and vice versa, it is deserialization. Because serialization can also be from memory to hard disk, serialization includes persistence.

Generally, protocols, specifications, and formats are friendly to various ecosystems and are generally used for data interaction between ecosystems.

The specific usage varies.

Generally, serialization is based on a language ecosystem. For example, Java is used to serialize Java bean objects to xml. The statement "the browser deserializes html into a dom object" and "serializing database data to json" is omitted.

  1. Store Data

  2. Transmit data

Serialization means to save the things to be used in some way for future calls.

Deserialization refers to calling the previously serialized data.

My personal understanding (I only used php and json data for serialization and deserialization ...), For reference only ~

There are two basic purposes: storage and transmission. Dump, which is transferred from one storage medium to another. The most common one is from memory to hard disk. The representation of data or objects between the two is different, to restore objects between the two, you need to read and write data or objects in a specific way.
Serialization and deserialization are relative. Generally, it is serialization to dump data or objects from memory to other media, and deserialization to transfer data from other media to memory.

I just asked A question: two computers, A and B, A now have an object instance. How can I send it to B?

Scenario 1: json is not supported before mysql5.7. serialization is required for storing multiple diagrams of a field, that is, storing them in the database according to certain rules. This is deserialization.

Scenario 2: loading object/object conversion, for example, converting the queried object to an array reduces the io pressure

Object To String storage; character to object. The object here can be of different data types in different languages, because it is a character in network transmission and has no type, so serialization is required. Serialization of other application scenarios also includes Persistent Object Storage.

Where to use: During Android development, I usually serialize objects and pass objects, or save serialized objects locally. I will deserialize them when they are in use for your reference.

How to Use: Serializable interface for object implementation,

The Gson package is usually used for serialization or deserialization. Example:

String str = gson.toJson(yourObj);
if (!TextUtils.isEmpty(str)) {    Type type = (Type) new TypeToken
  
   >() {    }.getType();    List
   
     list = new Gson().fromJson(str, type);}
   
  

You can also implement the Code by yourself. Take List as an example. The Code is as follows:

// Convert list to String type data public static String list2String (List
  
   
List) throws IOException {// instantiate a ByteArrayOutputStream object to load the compressed Byte File ByteArrayOutputStream baos = new ByteArrayOutputStream (); // then load the obtained character data to ObjectOutputStreamObjectOutputStream oos = new ObjectOutputStream (baos); // The writeObject method is responsible for writing the status of the object of the specific class, so that the corresponding readObject can restore its oos. writeObject (list); // Finally, use Base64.encode to convert the byte file to Base64 encoding, and save String listString = new String (Base64.encode (baos. toByteArray (), Base64.DEFAULT); // disable oosoos. close (); return listString ;}
  
// Restore the string-saved list to the public static List
  
   
String2List (String str) throws StreamCorruptedException, IOException {byte [] mByte = Base64.decode (str. getByte (), Base64.DEFAULT); ByteArrayInputStream bais = new ByteArrayInputStream (); ObjectInputStream ois = new ObjectInputStream (bais); List
   
    
StringList = (List
    
     
) Ois. readObject (); return stringList ;}
    
   
  

Benefits: Unknown. Generally, the object cannot be identified. Data or storage can be transferred only after serialization.

It's like packaging by express delivery.

Serialization and deserialization, you can understand it as the data encoding and decoding process. The data structure in a language system can be identified and run only in this system; when data needs to be transferred across languages and systems, it must be converted into an intermediate structure that can be identified and restored by both parties. This is serialization and deserialization ~~~
For example, if the structure of a Data Object represented in java is different from the structure of the object represented in Javascript, a data object in java can be serialized into a Javascript to recognize the structure (in JSON format ), javascript can restore the data with the same meanings and run it in the Javascript execution environment ~~~
You can also consider the transmission of image data as a serialization and deserialization process. Before transmission, the image information is serialized into a binary data stream with image format information, after receiving the binary stream, the receiver identifies the image format and restores it to the corresponding image object for display ~~~

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.