Paracle VS Serialization in android

17 篇文章 0 订阅

 

Serialization and Paracelable Object. 

        Parcelable is faster than Serialization makes it a perferred choice of approach while passing an object.


implementing the Parcelable interface and overriding the writeToParcel() method in its own class. and then to create a static Parcelable.Creator objcet to de-serialize the Java object.

 

Parcelable and Serilization are used for marshaling and unmarshaling java objects. 

    Parcelable is well documented in the Android SDK; Serialization on the other hand is avaliable in java. 

 

Seralization is a marker interface, which implies the user cannot mashal the data. In Serialization, a mashaling operation is performed on a JVM using the java reflection API. This helps identify the java objcets member and behavior, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in comparsion to Parcelable. 


Speed test in different mobile showing like this picture.



I will find some pictures in google when searching a unfamiliar words. 



Parcelable

Interface for classes whose instances can be written to and restored from a  Parcel . Classes implementing the Parcelable interface must also have a non-null static field called CREATOR  of a type that implements the  Parcelable.Creator  interface.


Parcel

Container for a message (data and object references) that can be sent through an IBinder. A Parcel can contain both flattened data that will be unflattened on the other side of the IPC (using the various methods here for writing specific types, or the general  Parcelable  interface), and references to live  IBinder objects that will result in the other side receiving a proxy IBinder connected with the original IBinder in the Parcel. 

Parcel is not a general-purpose serialization mechanism. This class (and the corresponding Parcelable API for placing arbitrary objects into a Parcel) is designed as a high-performance IPC transport. As such, it is not appropriate to place any Parcel data in to persistent storage: changes in the underlying implementation of any of the data in the Parcel can render older data unreadable.

The bulk of the Parcel API revolves around reading and writing data of various types. There are six major classes of such functions available.

writeByte(byte),   readByte(),   writeDouble(double),   readDouble(), writeFloat(float),   readFloat(),   writeInt(int),   readInt(),   writeLong(long),   readLong(),   writeString(String),   readString().

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public class OrderItem implements Parcelable {  
  2.     private long id;  
  3.     private String name;  
  4.     private int sequence;  
  5.     protected double price;  
  6.     protected boolean isPass;  
  7.     private List<OtherObject> otherObjs;  
  8.     private List<Long> cIds;    
  9.   
  10.     public OrderItem() {  
  11.         otherObjs = new ArrayList<OtherObject>();  
  12.         cIds = new ArrayList<Long>();  
  13.     }  
  14.   
  15.     public OrderItem clone() {  
  16.         Parcel p = Parcel.obtain();  
  17.         p.writeValue(this);  
  18.         p.setDataPosition(0);  
  19.         OrderItem newOrderItem = (OrderItem) p.readValue(OrderItem.class.getClassLoader());  
  20.         p.recycle();  
  21.         return newOrderItem;  
  22.     }  
  23.       
  24.         
  25.     @Override  
  26.     public int hashCode() {  
  27.         final int prime = 31;  
  28.         int result = 1;  
  29.         result = prime * result + (int) (id ^ (id >>> 32));  
  30.         return result;  
  31.     }  
  32.   
  33.     @Override  
  34.     public boolean equals(Object obj) {  
  35.         if (this == obj)  
  36.             return true;  
  37.         if (obj == null)  
  38.             return false;  
  39.         if (getClass() != obj.getClass())  
  40.             return false;  
  41.         OrderItem other = (OrderItem) obj;  
  42.         if (id != other.id)  
  43.             return false;  
  44.         return true;  
  45.     }  
  46.   
  47.     @Override  
  48.     public int describeContents() {  
  49.         return 0;  
  50.     }  
  51.   
  52.     protected OrderItem(Parcel in) {  
  53.         id = in.readLong();  
  54.         name = in.readString();  
  55.         price = in.readDouble();  
  56.         sequence = in.readInt();  
  57.         if (in.readByte() == 0x01) {  
  58.             otherObjs = new ArrayList<OtherObject>();  
  59.             in.readList(otherObjs, OtherObject.class.getClassLoader());  
  60.         } else {  
  61.             otherObjs = null;  
  62.         }  
  63.         isPass = in.readByte() != 0x00;  
  64.         if (in.readByte() == 0x01) {  
  65.             cIds = new ArrayList<Long>();  
  66.             in.readList(cIds, Long.class.getClassLoader());  
  67.         } else {  
  68.             cIds = null;  
  69.         }  
  70.     }  
  71.   
  72.     @Override  
  73.     public void writeToParcel(Parcel dest, int flags) {  
  74.         dest.writeLong(id);  
  75.         dest.writeString(name);  
  76.         dest.writeDouble(price);  
  77.         dest.writeInt(sequence);  
  78.         if (otherObjs == null) {  
  79.             dest.writeByte((byte) (0x00));  
  80.         } else {  
  81.             dest.writeByte((byte) (0x01));  
  82.             dest.writeList(otherObjs);  
  83.         }  
  84.         dest.writeByte((byte) (isPass ? 0x01 : 0x00));  
  85.         if (cIds == null) {  
  86.             dest.writeByte((byte) (0x00));  
  87.         } else {  
  88.             dest.writeByte((byte) (0x01));  
  89.             dest.writeList(cIds);  
  90.         }  
  91.     }  
  92.       
  93.     @SuppressWarnings("unused")  
  94.     public static final Parcelable.Creator<OrderItem> CREATOR = new Parcelable.Creator<OrderItem>() {  
  95.         @Override  
  96.         public OrderItem createFromParcel(Parcel in) {  
  97.             return new OrderItem(in);  
  98.         }  
  99.   
  100.         @Override  
  101.         public OrderItem[] newArray(int size) {  
  102.             return new OrderItem[size];  
  103.         }  
  104.     };  
  105. }  

if u want copy list of Objcet, here is the code.

[java]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. public static List<OrderItem> cloneArrayList(List<OrderItem> orderItems) {  
  2.         Parcel p = Parcel.obtain();  
  3.         p.writeList(orderItems);  
  4.         p.setDataPosition(0);  
  5.         List<OrderItem> orderItemList = (List<OrderItem>) p.readArrayList(OrderItem.class.getClassLoader());  
  6.         p.recycle();  
  7.         return orderItemList;  
  8.     }  


by the way, if one field not add in writeToParcel()  when transfer data rely on Bundle. the other side will get a null object. Such as  Activity A transfer data to Activity B.


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值