淺談Serializable和Parcelable

一、作用

兩者都是用于對象的序列化成二進(jìn)制的流,便于在intent或bundle中傳輸。

二、區(qū)別

1. Serializable

Serializable是java的序列化接口,核心實(shí)現(xiàn)是ObjectOutPutStream.writeObject()進(jìn)行序列化,ObjectInputStream.readObject()進(jìn)行反序列化。

serialVersionUID
可在序列化的類中定義serialVersionUID,用static和final修飾,用于在反序列化中驗(yàn)證類版本的差異。

  • 如果不定義serialVersionUID,系統(tǒng)會(huì)聲明個(gè)默認(rèn)值,當(dāng)類發(fā)生變化后,serialVersionUID將會(huì)被系統(tǒng)重新計(jì)算賦值,反序列化時(shí),如果前后serialVersionUID不一致,將會(huì)crash,拋出InvalidClassException錯(cuò)誤。
  • 如果定義serialVersionUID,盡管序列化后類發(fā)生變化,由于前后serialVersionUID一致,反序列化時(shí)也會(huì)盡最大的程度復(fù)原,如果復(fù)原失敗一樣會(huì)拋出InvalidClassException錯(cuò)誤。

在Android Serializable 的Api注釋中有段說明:針對Android N以上版本的設(shè)備時(shí),為了保證對之前版本的兼容性,強(qiáng)烈建議顯示的定義serialVersionUID,而不是由系統(tǒng)默認(rèn)賦值。

2. Parcelable

Parcelable是Android特有的序列化接口,核心實(shí)現(xiàn)是通過parcel的讀寫操作進(jìn)行序列化,里面的方法是Native方法。

  • 使用Serializable會(huì)頻繁進(jìn)行IO操作,開銷很大,在Android平臺上,Parcelable多用于內(nèi)存序列化,如IPC進(jìn)程間通信。

三、實(shí)現(xiàn)Parcelable步驟

public class Student implements Parcelable {

    private int id;
    private String name;
    private boolean isBoy;

    private Student(Parcel in) {
        id = in.readInt();
        name = in.readString();
        isBoy = in.readByte() != 0;
    }

    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];
        }
    };

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeInt(id);
        parcel.writeString(name);
        parcel.writeByte((byte) (isBoy ? 1 : 0));
    }
}

實(shí)現(xiàn)Parcelable必須實(shí)現(xiàn)describeContents()writeToParcel(Parcel parcel, int i)方法。還需要?jiǎng)?chuàng)建個(gè)Parcelable.Creator接口的成員,用于進(jìn)行反序列化操作。

3.1 describeContents()

方法返回當(dāng)前對象的內(nèi)容描述,如果含有文件描述符,則返回1,一般默認(rèn)返回0。

 /**
     * Describe the kinds of special objects contained in this Parcelable
     * instance's marshaled representation. For example, if the object will
     * include a file descriptor in the output of {@link #writeToParcel(Parcel, int)},
     * the return value of this method must include the
     * {@link #CONTENTS_FILE_DESCRIPTOR} bit.
     *  
     * @return a bitmask indicating the set of special object types marshaled
     * by this Parcelable object instance.
     */
    public @ContentsFlags int describeContents();
3.2 writeToParcel(Parcel, int)

在這方法進(jìn)行序列化,falg默認(rèn)為0;如果需要對象作為返回值返回,則值為1,這樣不能立即釋放資源。

 /**
     * Flatten this object in to a Parcel.
     * 
     * @param dest The Parcel in which the object should be written.
     * @param flags Additional flags about how the object should be written.
     * May be 0 or {@link #PARCELABLE_WRITE_RETURN_VALUE}.
     */
    public void writeToParcel(Parcel dest, @WriteFlags int flags);
3.3 Parcelable.Creator
 /**
     * Interface that must be implemented and provided as a public CREATOR
     * field that generates instances of your Parcelable class from a Parcel.
     */
    public interface Creator<T> {
        /**
         * Create a new instance of the Parcelable class, instantiating it
         * from the given Parcel whose data had previously been written by
         * {@link Parcelable#writeToParcel Parcelable.writeToParcel()}.
         * 
         * @param source The Parcel to read the object's data from.
         * @return Returns a new instance of the Parcelable class.
         */
        public T createFromParcel(Parcel source);
        
        /**
         * Create a new array of the Parcelable class.
         * 
         * @param size Size of the array.
         * @return Returns an array of the Parcelable class, with every entry
         * initialized to null.
         */
        public T[] newArray(int size);
    }

createFromParcel ():反序列化,創(chuàng)建原始序列化對象
newArray ():反序列化,創(chuàng)建原始序列化對象的數(shù)組
內(nèi)部通過Parcel的read方法實(shí)現(xiàn)。

成員也需實(shí)現(xiàn)Parcelable才能保證序列化的完整
如果Student不實(shí)現(xiàn)Parcelable,那么School實(shí)現(xiàn)序列化的成員不包括Student

public class School implements Parcelable {

    private String name;
    private Student student;

    private School(Parcel in) {
        name = in.readString();
        student = in.readParcelable(Student.class.getClassLoader());
    }

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

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

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel parcel, int i) {
        parcel.writeString(name);
        parcel.writeParcelable(student, i);
    }
}

student = in.readParcelable(Student.class.getClassLoader())在反序列化時(shí),需要獲取當(dāng)前線程的成員類的加載器,否則會(huì)報(bào)找不到該類的錯(cuò)誤。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容