No poetic female program apologise not good cook ~ reprint please indicate the source, the From Li Shiyu – blog.csdn.net/cjm24848365…

(PS: I feel that if we can really say these points during the interview, it will seem more advanced, and it will make our interview time seem very long, hahahaha ~)

Interviewer: “Can you talk about how Java and Android serialize?”

Yes, we reviewed the difference between Serializable and Parcelable earlier, but Serializable and Parcelable interfaces are not equal to serialization. If we want to talk about serialization again, we need to know a little more about it.

First we need to understand a concept: what is serialization?

  • Serialization and deserialization are a process, not an interface!

    Serialization means that when I need to save information in memory or in a file, I need to mark and standardize the information.

    Deserialization means that when you need that information in the future you can restore it to the way it was when you saved it.

Let’s take another example from real life.

Everyone has to move, when moving, such as you have a large removable cabinet, this time for convenience, we will pack after taking down the cabinet, when remove the packing I will do some tags, or set some standards, because after I make sure I have been hurt, still can spell it back. — Then the process of marking or packing according to standards can be understood as serialization.

When we arrived at our new home, we unpacked the boxes and were able to easily restore them to their original shape according to the markers (or standards) we had made. — Then the process of following certain rules to restore the original, we can understand it as deserialization.

So, at this point we understand that Serializable and Parcelable are just one of the two ways to implement serialization.

Now, let me take a closer look at how they actually serialize and how they work.

Serializable first, here is its source code:

  • Since it’s empty, how does it serialize?

    Well, the interface itself doesn’t do anything, it just acts as a tag here.

    ObjectOutputStream ObjectIntputStream ObjectOutput ObjectInput ObjectOutputStream ObjectIntputStream ObjectOutput ObjectInput

    That is, as long as your class is marked “Serializable”, you can write it using outputStream, and if you write it without the tag, it will throw an exception.

  • So how does it serialize and deserialize once it’s marked?

    This is done by the JDK itself.

    For example, if I have a Student class that needs to write to a file via an IO stream, I have to make it implement the Serializable interface.

    Once you use it, the JDK itself packages all the properties (type/value/name, etc.) of the class into a file when you stream it.

    That way, when someone else wants to use it later, they can restore it based on the tag. This is a reverse process, so it’s called deserialization.

    Of course, it actually uses reflection, with reflection to get the attributes of the class, the name and so on.

Since Parcelable is an interface provided by Android. So it’s no longer an empty implementation.

Let’s take a look at the source code:

Since Parcelable does its own serialization and deserialization, it is more complicated to use, and its comments tell us how to implement Parcelable.

The writeToParcel() method is called when serialization is performed to record data to the Parcel.

It must be serialized and deserialized in the same order, without resorting to IO.

By looking at it, we can know:

  • Serializable is a JDK rule, the specific serialization and deserialization operations are completed by JDK.

  • Parcelable requires programmers to implement their own serialization and deserialization processes, so Parcelable is more efficient to use than Serializable, but more complex to use than Serializable (write your own code).

  • But we also found that the common purpose of both of them was the same – to record our data according to certain rules, and to parse and recover the data according to those rules if you needed deserialization.

Serialization and deserialization are a process, not an interface.

We also understand that Serializable and Parcelable are just a way to implement serialization and de-sequence.

And then we thought, well, we’re constantly sending data to and getting data from the network. They do not implement Serializable/Parcelable, so how do they serialize and deserialize?

This problem is actually not difficult, you think about what we use when we carry out network data transmission?

Gson, FastJson, etc. It’s not hard to guess that they also implement serialization and deserialization, but they use their own rules, such as JSON.

Json this serialization scheme, it has a point or very prominent, that is convenient for us to carry out network debugging, where there is a problem, the data at a glance know, readable drop.

Accumulate drip, do yourself ~