Serialization in Android

Serialization

Serialization: the process of converting an object into a sequence of bytes;
deserialization: the process of converting a sequence of bytes into an object;
when using Intent or Binder to pass an object, you need to serialize the object. There are two ways to serialize objects in Android, the first is Serializable and the other is Parcelable.

Serializable

Using Serializable can easily serialize objects, just inherit this interface and add serialVersionUID, as shown below

public class Student implements Serializable{
    private static final long serialVersionUID=12345;
    private String name;
    private int gender;

    public Student(String name,int gender){
        this.name = name;
        this.gender = gender;
    }
}

Of course, serialVersionUID can be omitted, and the system will calculate a serialVersionUID according to the structure of the class, so if the structure of the class changes, the corresponding serialVersionUID will also change. Therefore, if the class has changed, but you want to restore from the previous sequence, it is best to specify the serialVersionUID yourself, so as to restore the original object as much as possible.

Parcelable

To use Parcelable serialization, you need to inherit the Parcelable interface and implement the corresponding methods, as shown below

public class Student implements Parcelable{

    private String name;
    private int gender;

    public Student(String name,int gender){
        this.name = name;
        this.gender = gender;
    }

    protected Student(Parcel in) {
        this.name = in.readString();
        this.gender = in.readInt();
    }

    public static final Creator<Student> CREATOR = new Creator<Student>() {
        @Override
        public Student createFromParcel(Parcel in) {
            return new Student(in);
        }

        @Override
        public Student[] newArray(int size) {
            return new Student[size];
        }
    };

    //返回当前对象的内容描述,几乎所有情况都返回0,仅在当前对象中存在文件描述符时返回1
    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeInt(gender);
    }
}

It should be noted that the order of reading and writing should be consistent. As can be seen from the above code, Parcelable is much more complicated to implement than Serializable.

Compare

Reflection is used in the implementation of Serializable, so the performance is not particularly good, but it can be applied to all scenarios that require serialization.
Parcelable has better performance than Serializable, but if you want to write objects to disk, it is better to use Serializable, because when external data changes, Parcelable cannot guarantee the persistence of data well.

the difference Serializable Parcelable
Affiliation API Java Android
performance Difference Okay
scenes to be used Serialize to local, network transfer data transfer in memory

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324607922&siteId=291194637