I would like to know why object exchange between activities is made as Parcelable and why not as a JSON string. I understand Parcelable is better than Serialization. Can anyone make a comparison between Parcelable and JSON.
I would like to know why object exchange between activities is made as Parcelable and why not as a JSON string
Not everything that can go in a Parcel can go in a JSON string, such as:
IBinder objects (associated with remote services using AIDL)
ParcelFileDescriptor
Exception
Also, while you could convert a byte[] into a String (e.g., base-64 encoding), that will be inefficient.
Parcelable is very low level. Therefore its much faster then everything else available nowadays on android (like Serializeable). Json wouldn't make no sense regarding performance (think of performance issues while writing and reading json) and memory space.
Related
I am aware that there are some answers to my question, but the answers are not very elaborate or convincing.
In my program, in order to transmit data from one activity to another, I'm converting the contents of the POJO class into a Json String and passing via bundle (Method1).
METHOD1
String jsonString = JacksonSingleton.getObjectMapper().writeValueAsString(object);
bundle.putExtra(KEY_STR, jsonString)
In Method 2, I am passing a Parcelable object in the bundle.
METHOD2
bundle.putExtra(KEY_STR, parcelableObject);
If I were to implement Method 2, I'll have to implement Parcelable interface and write custom code for marshaling and unmarshaling my POJO class fields.
My question, which of these methods is better/recommended in terms of performance? Method 1 is very convenient but I want to follow best practices.
EDIT:
If you hate writing 'Parcelable' boilerplate code like me, you could use the Parcelable code generator plugin with your Android Studio/IntelliJ IDE. It will auto generate the methods for marshalling and unmarshalling your class fields. Very easy to use and highly recommended.
As much as I hate answering my own questions, I thought of sharing my observations that could help other developers in future.
Transmission of data from one activity to another can be done using either passing Serializable or Parcelable Objects in an activity intent. Android developer website recommends using Parcelable interface for this purpose.
However, my question was pertaining to the efficiency comparison between passing Parcelable object and JSON string.
To test this, I used an old and low-end Android device. I launched an activity by sending a large Parcelable Object in an activity intent. Next, I launched the same activity using the JSON String of the same object in the activity intent. What I observed was a significant observable latency while launching an activity by sending a JSON String instead of Parcelable Object.
In conclusion, even if we pass a JSON String, Java String object always implements Serializable. Google recommends using Parcelable instead of Serializable objects. This will usually be insignificant in case of strings of negligible length. However, in the case of massive Json Strings of massive objects, The efficiency will certainly take a toll.
You could refer this for performance benchmark of Parcelable vs Serializable.
TLDR:
Parcelable - More boilerplate code, better performance and a better engineering practice overall.
Serializable - Less code, easy to learn and acceptable if you're not obsessed with performance/best practices.
The JSON object/array is global usage, and can transfer over internet, we can handle it in the same way by different programming language on different platforms, and is easily stored. If the machine is not so slow and the data is not so huge, I prefer use JSON.
What for put extra with a large data to send to another activity? Sometimes I just send an ID for stored data or use a singleton to handle object... (Can another app access the Parcelable object?)
So the Parcelable is for cross process used in the same app.. Just like RemoteView and Service use it to transfer data for convenient...
I'm working on Android app with Google App Engine JAVA backend.
I've got Entity object from app engine backend, and I need to pass it from one Activity to another in Android app. Puting it into bundle as serializable extra throws exception, most probably because my Entity class doesn't implements Serializable (and I guess it can't implement it).
What would be the proper way to serializing entities objects at client side, for bundle packing purposes?
You better use GSON to do this.
Gson is a Java library that can be used to
1. Convert Java Objects into their JSON representation.
2. And also Convert a JSON string to an equivalent Java object.
Gson can work with arbitrary Java objects including pre-existing objects that you do not have source-code of.
I found that Gson isn't all that reliable and it takes a lot to write the Adapter. I went to EventBus for help and it works great! :)
I'm currently using JSON (org.json) to serialize one of my data classes. When I pass it in a Bundle or with an Intent, I just call .toString() on the sender side and then recreate the class on the receiving side. From everything I've read so far, I should not implement Java's Serializable due to performance concerns. I'm in the process of rewriting certain portions of the app and I was considering making my data classes implement Parcelable and transfer them that way. What would the advantages be if I did it that way? Would it be preferable if I used the Jackson JSON library instead? Most of the JSON work is based on the API; server responds only with JSON. I also store some of the JSON for caching on the app side.
I think JSON is by far the most convenient mechanism for typical POJOs; and it seems unlikely that performance should be significantly worse that with Parcelable. Parcelable implementation could be more compact; but if that is problematic, you could even compress cached JSON payloads. So I would probably try out JSON first and see how it works.
I am in the process of implementing an IntentService that I will use to perform RESTful web service calls, and then plan on using sendBroadcast to broadcast the results.
I receive JSON from the web service calls.
After I receive the JSON string, I'm not sure where I should actually parse the text and convert it into an object(s) (using GSON). Would I be better off making all my domain objects Parcelable, and send the objects in the broadcast, or just send the JSON text, and convert them when I actually need the objects? Any other advice?
Thanks.
It's a matter of personal preference, but I just write my own classes to serialize and deserialize between JSON strings and model objects. It might be a little bit of work to write the code as opposed to using some ORM like tool, but I find it to be very good for hunting down bugs or updating keys or nested hierarchies.
For each model object, I have a Translator class. For example Thing and ThingTranslator. This way, I can pass strings around between activities, and I have one single location where I can serialize and deserialize, so if there's ever anything wrong or something that needs to be updated (for ex, service changes object schema, etc) there's only one place to look.
I need to store an object content in Sqlite.
I was wondering which was the best way to do it with a serialization of the object or with Parcelable.
Is it possible to store it as Parcelable? How can I do it?
You are welcome to convert your object into some sort of persistable data structure (XML, JSON, Serializable) and stuff it in some database column. Bear in mind that you will still need to deal with compatibility issues (e.g., Version 2 of your app changes a class, which now needs to deal with both Version 1 and Version 2 structures). Also bear in mind that, going this route, you lose a lot of database capabilities (e.g., querying on something in the object).
You are also welcome to experiment with object databases, or CouchDb, or storing your persistable data structure to a file, if SQLite is not a requirement.
What most certainly will not work reliably is to pour the Parcelable into a Parcel and try storing the Parcel. A Parcel is meant for IPC use only and is not designed to be persisted. This is one of the reasons why Parcelable is faster than Serializable.
If you need to persist data, use Serializable. Parcelable is meant for IPC use. It is a binary format and not recommended for persistence.