serialization

concept

Serialization: The process of converting data structures or objects into binary strings. 1. The process of converting binary strings generated during serialization into data structures or objects. Data serialization is the process of converting a data structure or object into a data format that can be stored or transmitted. (JSON, XML, protobuffer) Serialization reason: Due to the underlying system, data is transmitted in the form of byte sequence. So inter-process communication, local data storage, and network data transfer all require serialization.

Serializable

Serializable is serialized in Java. It’s just a marker. Flag that the class is serializable.

SerialVersionUID (translation) serialVersionUID. Each class has a serialVersionUID, which defaults to a Hashcode value if not assigned. It marks the version of the object. SerialVersionUID error: serialVersionUID error: serialVersionUID error: serialVersionUID error: serialVersionUID error: serialVersionUID

Transient transient tense

If you do not want to serialize a parameter, declare transient. (Static is not serialized either)

NotSerializableException serializes the parent class, inherits and then serializes the enumerations as well. Special, enumerations are serialized with the name of the enumeration, deserialized with the name of the enumeration. If the parent class is not serialized, When the subclass deserialization reads, the object in the parent class reads as empty (and the parent class must have an argument constructor). Mainly overwrite the ObjectOutputStream. WriteObject (), ObjectInputStream. ReadObject () method

The Serializable principle

Serializable is serialized and deserialized via ObjectOutputStream and ObjectInputStream, respectively.

ObjectOutputStream.writeObject()



It goes down to the various types, and then parses them separately, String, int, etc., and then writes them in binary

ObjectInputStream. ReadObject () method

Parcelable

Parcelable is implemented with Binder. Parcelable is provided by the Android SDK. It is based on memory. Since memory reads and writes are faster than hard disk, Parcelable is generally used for cross-process object transfer in Android. Parcel (translation) Parcelable wraps the data as a Parcel object, and a Parcel in Binder (which transfers data across processes) provides a mechanism for writing serialized data to a shared memory from which other processes can read byte streams. And deserialize into objects. Here is a model of this process.

use

contrast

Does the deserialized object call the constructor again? A: No, because it is directly resolved from binary. Object is not the original Object

What is the relationship between serialization and deserialized objects? A: It is a deep copy with different reference addresses.

Why does Android design bundles instead of using HashMap structures? A: The bundle uses ArrayMap. ArrayMap has an advantage over Hashmap in that it can be easily expanded by half of its original capacity. At the [hundred] level, ArrayMap can find keys and values by dichotomy. A hashcode for keys and an Entry for keys + values are much more efficient than a HashMap. Since there is generally a small amount of data transferred in memory or within Android, bundles are more suitable

Intents/bundles in Android A: Bundle implements the serialization interface for Parcelable to communicate between processes. Different processes share a chunk of memory of a fixed size. Parcelable uses the read/write method of a parcel object. BINDER_VM_SIZE = 1 * 1024 * 1024-4096 * 2; BINDER_VM_SIZE = 1 * 1024 * 1024-4096 * 2; BINDER_VM_SIZE = 1 * 1024 * 1024-4096 * 2; Even by modifying the Framework code, the bundle kernel mapping is only 4M, and can only be extended up to 4M.

Why can’t IntEnts pass objects directly between components rather than through serialization? A: Because the Activity startup process needs to interact with AMS, AMS is a different process from the UI process, so the process needs to interact with data, so it must be serialized

Json

JSON(JavaScript Object Notation) is a lightweight data interchange format.

Data marking, storage and transmission

Features 1. Fast read and write speed 2. Simple parsing 3. It is self-descriptive

Json is also a means of serialization in a broad sense.

Gson parsing

  • Parsing principle: Event-driven.

DOM parsing (XML parsing) :



Advantage: can add delete change check, fast speed

Disadvantages: because all of a sudden to the memory, so memory consumption

Event-driven:



Advantages: Small memory consumption, fast data query

Disadvantages: can not add, delete, change, etc

  • Use Gson

javaBean –> json

json –> javaBean

Or use GsonBuilder(). New a GsonBuilder (). The create ();

This is what we’re using hereBuilder pattern, Facade pattern (Facade pattern)Hidden specific complex implementation, do not want to configure the parameter field, the system will give the default configuration.



More important method

Custom TypeAdapter:If you need special treatment for parsing, you can customize the TypeAdapter, primarily by overriding the write and read methods. (In fact, every object needs to be resolved by TypeAdapter. The system defines a bunch of basic TypeAdapters for us, and the rest are generated by reflection, so it is not efficient.)

Comments:





Gson principle

1. Parse the JSON string into a JsonElment (list or tree), which is then parsed by Adapter

JsonElment Type = JsonElment Type = JsonElment Type = JsonElment Type = JsonElment Type = JsonElment The system defines a generic (basic) TypeAdapter for us, but otherwise we have to define our own. If we don’t have a custom TypeAdapter (see using the re-write,read method above), we need to create it using reflection.

TypeAdapter defined by the system



A String of TypeAdapter

New Gson().fromjson (); new Gson().fromjson ();

1. Enter a jsonString and a class type



2. Create different TypeAdapters based on different TypeTokens (types), and then use your own typeTokens

3. In the reflection mode, read the key first to check whether the key supports serialization, and then read the value4. Read the value, recurse if not, and then reflect the value through the constructor you built earlier

4 summarizes