天天看點

Android序列化-Serial庫

這個代碼包括了 int數組,對象的序列化和反序列化。

Serial是由Twitter高性能序列化方案,它力求幫助開發者實作高性能和高可控的序列化過程。

GitHub:​​​https://github.com/twitter/Serial/blob/master/README-CHINESE.rst/​​

  • 相比起傳統的反射序列化方案更加高效(沒有使用反射)
  • 性能相比傳統方案提升了3倍 (序列化的速度提升了5倍,反序列化提升了2.5倍)
  • 序列化生成的資料量(byte[])大約是之前的1/5
  • 開發者對于序列化過程的控制較強,可定義哪些object、field需要被序列化
  • 有很強的debug能力,可以調試序列化的過程(詳見:​​調試​​)
/**
 * @author Administrator
 * @date 2020/7/20
 */
public class Person {
    String name;
    int age;
    int[] arr;
    Color c;

    public Person(String name, int age, int[] arr,Color c) {
        this.name = name;
        this.age = age;
        this.arr = arr;
        this.c = c;
    }

    // 需要單獨定義一個序列化操作相關的變量
    public static final ObjectSerializer<Person> SERIALIZER = new PersonSerializer();

    // 實際用來定義序列化和解序列化的操作,類似Parcelable的Creator
    private static final class PersonSerializer extends ObjectSerializer<Person> {
        @Override
        protected void serializeObject(SerializationContext context,
                                       SerializerOutput output,
                                       Person object) throws IOException {
            // 第三個參數位址指向要序列化類,通過第二個參數output進行序列化
            output.writeString(object.name);
            output.writeInt(object.age);
            for (int i = 0; i < object.arr.length; i++) {
                output.writeInt(object.arr[i]);
            }
            output.writeString(object.c.red);
            output.writeString(object.c.green);
            output.writeString(object.c.blue);
//            output.writeObjectStart()

        }

        @Override
        protected Person deserializeObject(SerializationContext context,
                                           SerializerInput input,
                                           int versionNumber) throws IOException {
            // versionNumber 為版本号,控制版本資訊。具體隻要在
            // public static final ObjectSerializer<People> SERIALIZER = new PeopleSerializer(1);
            // 構造函數添加版本号即可,就可通過versionNumber進行控制版本
            String name = input.readString();
            int age = input.readInt();
            int[] arr = new int[3];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = input.readInt();
            }
            String r = input.readString();
            String g = input.readString();
            String b = input.readString();


            return new Person(name, age, arr,new Color(r,g,b));
        }
    }
}      
public class Color {
    String red;
    String green;
    String blue;

    public Color(String red, String green, String blue) {
        this.red = red;
        this.green = green;
        this.blue = blue;
    }
}      
Person person = new Person(str, 23, new int[]{2, 3, 4},new Color("z","d","s"));
final Serial serial = new ByteBufferSerial();
try {
      final byte[] serializedData = serial.toByteArray(person, person.SERIALIZER);
      Log.i("serial", serializedData.length + "");

       Person output = serial.fromByteArray(serializedData, Person.SERIALIZER);
       Log.i("Serial", "name : " + output.name + " ; age : " + output.age + " " + output.arr[2] +" " + output.c.red);
} catch (Exception e) {
       e.printStackTrace();
}      

繼續閱讀