In my previous blog, i have talked about why parcelable is efficient and fast compared to serializable and also said about Android Parcelable plugin, does the auto generate code for your class. Recently I have come across a library which does the parcelable and avoid the boiler plate code such as
writeToParcel, readFromParcel and Parcelable.Creator
One of the issue with these code, you need to write and read in same order. Though not a big deal, but still if we can do better than this, then why not?. Parceler does the Job. No need to write the boiler plate codes and check for the order of read and write.
Refer the Github Link for more info. Parceler
Ok, how its works. Just annote with @Parcel, its does the serialization for you without have to override all the above mentioned code.
Configuration:
compile 'org.parceler:parceler-api:1.1.6' annotationProcessor 'org.parceler:parceler:1.1.6'
If its throws a error saying “unable to find the generated parcelable class”, it might be due annotationProcessor not enabled for your project. In that case, refer the below.
compile 'org.parceler:parceler-api:1.1.6' apt 'org.parceler:parceler:1.1.6'
Next step, Just define Parcel Annotation for your class
@org.parceler.Parcel public class MyParcelClass { public String suburb; public String state; public String postcode; public String country; }
If suppose you have a Inner class , just do the same as you did to the parent class. Just annotate with @Parcel
@org.parceler.Parcel public class MyParcelClass { public String suburb; public String state; public String postcode; public String country; @Parcel public static class MyName{ public String firstname; public String LastName; } @Parcel public static class MyID{ public String DL; } }
“Be careful not to use private fields when using the default field serialization strategy as it will incur a performance penalty due to reflection.”
To avoid the performance penalty , just define the annotation as
@org.parceler.Parcel(org.parceler.Parcel.Serialization.BEAN)
Till now all Good, But how about Custom serialization? Yes, there is a way to do it.
All you need is to define annotation to the field which you want to do custom serialization.
@ParcelPropertyConverter(NameConverter.class) public MyName name;
@org.parceler.Parcel
public class NameConverter implements ParcelConverter<MyParcelClass.MyName> {
@Override
public void toParcel(MyParcelClass.MyName input, Parcel parcel) {
if (input == null) {
parcel.writeInt(-1);
} else {
parcel.writeParcelable(Parcels.wrap(input.firstName), 0);
parcel.writeParcelable(Parcels.wrap(input.LastName), 0);
}
}@Override
public LocationPoint.Address fromParcel(Parcel parcel) {MyClassName.MyName name = new MyClassName.MyName();
name.firstName = Parcels.unwrap(parcel.readParcelable(MyFirstName.MyName.class.getClassLoader()));
name.LastName = Parcels.unwrap(parcel.readParcelable(MyFirstName.MyName.class.getClassLoader()));return name;
}
}
There you are; now you don’t need to worry on how to read and write to a parcel class.
Voilla! Happy Coding :)