Tuesday, May 17, 2016

Serialization and Parcelable in android

Serialization and Parcelable in android:

Serialization and parcelable both used for marshaling and unmarshaling of object and send data from one android component to other component.

Parcelable:

When we want to pass objects between two components in Android, these objects have to implements Android Parcelable interface. Parcelable in Android is similar to serialization and deserialization mechanism as we are used in Java.

 In Parcelable, developers write custom code for marshaling and unmarshaling so it creates less garbage objects in comparison to Serialization. The performance of Parcelable over Serialization dramatically improves, because of this custom implementation.

Ex:
public class MyParcelable implements Parcelable {
     private String name;
     private String lastName;


     public int describeContents() {
         return 0;
     }

     public void writeToParcel(Parcel out, int flags) {
         out.writeInt(name);
         out.writeInt(lastName);



     }



     public static final Parcelable.Creator<MyParcelable> CREATOR

             = new Parcelable.Creator<MyParcelable>() {

         public MyParcelable createFromParcel(Parcel in) {

             return new MyParcelable(in);

         }



         public MyParcelable[] newArray(int size) {

             return new MyParcelable[size];

         }

     };

     

     private MyParcelable(Parcel in) {

         name = in.readString();
   lastName = in.readString();



     }

 }


Syntax to send object

Intent i = new Intent();
i.putExtra("name_of_extra", myParcelableObject);


Syntax to receive object

Intent i = getIntent();
MyParcelable myParcelableObject = (MyParcelable) i.getParcelableExtra("name_of_extra");
 
 
 
 
Serialization:

Serialization is the process of converting an object's state (including its references) to a sequence of bytes, as well as the process of rebuilding those bytes into a live object at some future time.
This process is in java therefore we can use the same in android for passing object from one component to other component.

Serialization is a marker interface, which implies the user cannot marshal the data according to their requirements. In Serialization, a marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API. This helps identify the Java objects member and behavior, but also ends up creating a lot of garbage objects. Due to this, the Serialization process is slow in compare to parcelable.

Ex:


import java.io.Serializable;
 
public class Person implements Serializable {
  private String firstName;
  private String lastName;
  
 
  public Person(String firstName, String lastName) {
    this.firstName = firstName;
    this.lastName = lastName;
  }
 
  public String getFirstName() {
    return firstName;
  }
 
  public void setFirstName(String firstName) {
    this.firstName = firstName;
  }
 
  public String getLastName() {
    return lastName;
  }
 
  public void setLastName(String lastName) {
    this.lastName = lastName;
  }
} 

 
Syntax to send object

Intent i = new Intent();
i.putExtra("name_of_extra", mySerialableObject);

Syntax to receive object

Intent i = getIntent();
Person mySerialableObject = (Person) 
i.getSerializableExtra ("name_of_extra");
 
 

Difference between Serialization and Parcelable:

 Differences between the two are often cited around implementation techniques and performance results
1)      Parcelable is well documented in the Android SDK whereas serialization is available in Java.

2)      Developers needs to write custom code for marshaling and unmarshaling so it creates less garbage objects in compare to Serialization.


3)      In Serialization object marshaling operation is performed on a Java Virtual Machine (JVM) using the Java reflection API and Serializable is a marker interface, which implies the user cannot marshal the data according to their requirements therefore it create lots of garbage object due to that it is slow in compare to parcelable.



Which one is better to use in android:

Parcelable takes little more time in implementation due to custom marshaling but it performs around two times faster to serialization therefore always use parcalable in android instead of serialization for passing object between android components.




8 comments: