Android Advanced 6: Two serialization modes Serializable and Parcelable

Source: Internet
Author: User
Tags bitmask ojs
<span id="Label3"></p>What is serialization<p><p>We always say or hear "serialization", what is the definition of it?</p></p> <blockquote> <blockquote> <p>Serialization (serialization) transforms the state information of an object into a process that can be stored or transmitted in a form. During serialization, an object writes its current state to a temporary or persistent store. later, the object can be recreated by reading or deserializing the state of the object from the Store.</p> <p>Binary serialization preserves the type fidelity, which is useful for preserving the state of an object between different invocations of the Application. For example, you can share objects between different applications by serializing them to the Clipboard. You can serialize objects to streams, disks, memory, networks, and so On. Remoting uses serialization to pass objects by value between a computer or an application Domain.</p> </blockquote> </blockquote><p><p>Simply put, "serialization" is the transformation of the object state of the runtime into binary, which is then saved to the stream, memory, or transmitted over the network to the other Side.</p></p><p><p>In Android development, we often use Intent to transfer data when we pass data in a component, such as Serializable or parcelable data, such as the Intent.putextra method:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs r">public Intent putExtra(String name, Parcelable value) {<span class="hljs-keyword">...</span>}public Intent putExtra(String name, Serializable value) {<span class="hljs-keyword">...</span>}</code></pre></pre><p><p>Binders are also used to pass Data.</p></p><p><p>Let's introduce both of these serialization methods Today.</p></p>Serializable interface<p><p>Serializable is a serialized interface provided by Java, which is an empty interface:</p></p><pre class="prettyprint"><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword">public</span><span class="hljs-keyword">interface</span> Serializable {}</code></pre></pre><p><p>The Serializable is used to identify that the current class can be <code>ObjectOutputStream</code> serialized and deserialized <code>ObjectInputStream</code> .</p></p><p><p>Serializable has the following features:</p></p> <ul> <ul> <li>In a serializable class, an attribute state that is not implemented Serializable cannot be serialized/deserialized</li> <li>That is, in the process of deserializing a class, its non-serializable properties will call the parameterless constructor to recreate</li> <li>therefore, the parameterless constructor of this property must be accessible and will be error-free when run</li> <li>A class that implements serialization, and its subclasses are also serializable</li> </ul> </ul><p><p>The following is an entity class that implements the Serializable:</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">Groupbean</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Serializable</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Long</span></span>Serialversionuid =<span class="hljs-number"><span class="hljs-number">8829975621220483374</span></span>L<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>String mname;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>List<string> mmembernamelist;<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Groupbean</span></span>() { }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>String<span class="hljs-title"><span class="hljs-title">GetName</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>mname; }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">SetName</span></span>(String Name) {mname = name; }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>List<string><span class="hljs-title"><span class="hljs-title">getmembernamelist</span></span>() {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>mmembernamelist; }<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">setmembernamelist</span></span>(list<string> Membernamelist) {mmembernamelist = membernamelist; }}</code></pre><p><p>You can see that implementing the Serializable implementation is very simple, except for the entity content, as long as creating a <code>serialVersionUID</code> property is Good.</p></p>Serialversionuid<p><p>From the name can be seen, this <code>serialVersionUID</code> , some similar to our usual interface version number, at run time this version number uniquely identifies a serializable class.</p></p><p><p>That is, when a class is serialized, the runtime saves its version number and then, when deserializing, checks to see if the version number of the object you want to deserialize is the same, and the inconsistency will be an error: · <code>InvalidClassException</code> .</p></p><p><p>If we do not create this version number ourselves, The runtime calculates a default version number based on many features of the class during Serialization. however, as long as you modify this class A little bit, this version number will Change. If this happens after serialization, it causes the error that is mentioned above to be Deserialized.</p></p><p><p>The JVM specification therefore strongly recommends that we manually declare a version number, which can be random, as long as it is Fixed. At the same time preferably private and final, as far as possible to ensure the Same.</p></p><p><p>In addition, static and transient decorated properties are not saved during serialization, which is well understood because static properties are managed by the class and are not object state, while the latter is a Java keyword that is specifically used to identify non-serialized Properties.</p></p><p><p>The default implementation Serializable does not automatically create <code>serialVersionUID</code> attributes, and in order to prompt us to create <code>serialVersionUID</code> them, you can search for them in Settings and <code>serializable</code> Select the options shown, <code>serialVersionUID</code> adding a warning to the classes and inner classes that do not have attributes Declared.</p></p><p><p></p></p><p><p>So when we create a class that does not declare the UID property, there is a yellow-yellow warning on the class name:</p></p><p><p></p></p><p><p>When you put the mouse on it, it will display the warning content:</p></p> <blockquote> <blockquote> <p>Groupbean ' does not define a ' serialversionuid ' field less ... (ctrl+f1)<br>Reports any Serializable classes which does not provide a serialversionuid field. Without a serialversionuid field, any change to a class would make previously serialized versions Unreadable.</p> </blockquote> </blockquote><p><p>At this point, we can generate Serialversionuid by code prompt shortcut key.</p></p>Serialization and deserialization of Serializable<p><p>The serialization and deserialization of Serializable are performed by ObjectOutputStream and objectinputstream, respectively, with the following instance Code:</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-javadoc"><span class="hljs-javadoc">/** * Serialized Object * *<span class="hljs-javadoctag"> @param</span> obj *<span class="hljs-javadoctag"> @param</span> path *<span class="hljs-javadoctag"> @return</span> </span> */</span><span class="hljs-keyword"><span class="hljs-keyword">synchronized</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">Saveobject</span></span>(Object obj, String Path) {<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(obj = =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">false</span></span>; } ObjectOutputStream Oos =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{oos =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>ObjectOutputStream (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>FileOutputStream (path)); Oos.writeobject (obj); Oos.close ();<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">true</span></span>; }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(ioexception E) {e.printstacktrace (); }<span class="hljs-keyword"><span class="hljs-keyword">finally</span></span>{<span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(oos! =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>) {<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{oos.close (); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(ioexception E) {e.printstacktrace (); } } }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">false</span></span>;}<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Deserialization Object * *<span class="hljs-javadoctag"> @param</span> path *<span class="hljs-javadoctag"> @param</span> <T> *<span class="hljs-javadoctag"> @return</span> </span> */</span><span class="hljs-annotation"><span class="hljs-annotation">@SuppressWarnings</span></span>(<span class="hljs-string"><span class="hljs-string">"unchecked"</span></span>)<span class="hljs-keyword"><span class="hljs-keyword">synchronized</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span><T> T<span class="hljs-title"><span class="hljs-title">ReadObject</span></span>(String Path) {objectinputstream OJS =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{ojs =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>ObjectInputStream (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>FileInputStream (path));<span class="hljs-keyword"><span class="hljs-keyword">return</span></span>(T) Ojs.readobject (); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(ioexception | ClassNotFoundException E) {e.printstacktrace (); }<span class="hljs-keyword"><span class="hljs-keyword">finally</span></span>{close (ojs); }<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;}</code></pre>Parcelable interface<p><p>Parcelable is an Android-specific serialization interface:</p></p><pre class="prettyprint"><code class=" hljs cs"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Interface</span></span>parcelable {<span class="hljs-comment"><span class="hljs-comment">a parameter in The//writetoparcel () method that identifies the current object as the return value returned</span></span> <span class="hljs-comment"><span class="hljs-comment">//some Implementation classes may release resources at this point</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>Final<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Parcelable_write_return_value =<span class="hljs-number"><span class="hljs-number">0x0001</span></span>;<span class="hljs-comment"><span class="hljs-comment">the second parameter in The//writetoparcel () method, which identifies the parent object that manages duplicate data in the internal state</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>Final<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Parcelable_elide_duplicates =<span class="hljs-number"><span class="hljs-number">0x0002</span></span>;<span class="hljs-comment"><span class="hljs-comment">//bitmask for the describecontents () method, each representing an object type</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span>Final<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Contents_file_descriptor =<span class="hljs-number"><span class="hljs-number">0x0001</span></span>;<span class="hljs-comment"><span class="hljs-comment">//describes The object type of the current parcelable instance</span></span> <span class="hljs-comment"><span class="hljs-comment">//for example, If the object has a file descriptor, This method returns the Contents_file_descriptor</span></span> <span class="hljs-comment"><span class="hljs-comment">//other Situations will return a</span> bitmask</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span> <span class="hljs-title"><span class="hljs-title">describecontents</span></span>();<span class="hljs-comment"><span class="hljs-comment">//convert object to a Parcel object</span></span> <span class="hljs-comment"><span class="hljs-comment">///parameter dest Indicates the Parcel object to write to</span></span> <span class="hljs-comment"><span class="hljs-comment">//flags Indicates how this object will be written</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Writetoparcel</span></span>(Parcel dest,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>flags);<span class="hljs-comment">the <span class="hljs-comment">//implementation class must have a Creator property that is used for deserialization to convert the Parcel object to Parcelable</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Interface</span></span>creator<t> {<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>T<span class="hljs-title"><span class="hljs-title">Createfromparcel</span></span>(Parcel source);<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>t[]<span class="hljs-title"><span class="hljs-title">NewArray</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>size); }<span class="hljs-comment"><span class="hljs-comment">//a Creator provided when the object is created</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Interface</span></span>Classloadercreator<t> extends Creator<t> {<span class="hljs-comment"><span class="hljs-comment">//Parcel an object with the class loader and the previously serialized object</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>T<span class="hljs-title"><span class="hljs-title">Createfromparcel</span></span>(Parcel source, ClassLoader loader); }}</code></pre><p><p>Classes that implement the Parcelable interface are converted to type data when serialized and deserialized <code>Parcel</code> .</p></p> <blockquote> <blockquote> <p>Parcel is a vector that can contain data or object references, and then pass through IBinder between Processes.</p> </blockquote> </blockquote><p><p>The class that implements the Parcelable interface must have a static variable of type CREATOR, and here is an instance:</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">Parcelablegroupbean</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">parcelable</span> {</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>String mname;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>List<string> mmembernamelist;<span class="hljs-keyword"><span class="hljs-keyword">Private</span></span>User muser;<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Requires a constructor that we created manually *<span class="hljs-javadoctag"> @param</span> name *<span class="hljs-javadoctag"> @param</span> membernamelist *<span class="hljs-javadoctag"> @param</span> US ER *</span> /</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-title"><span class="hljs-title">Parcelablegroupbean</span></span>(String name, list<string> membernamelist, user User) {mname = name; Mmembernamelist = membernamelist; Muser = user; }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * 1. Content description *<span class="hljs-javadoctag"> @return</span> </span> * *</span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">int</span></span> <span class="hljs-title"><span class="hljs-title">describecontents</span></span>() {<span class="hljs-comment"><span class="hljs-comment">//returns almost 0 unless the current object has a file descriptor of 1</span></span> <span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-number"><span class="hljs-number">0</span></span>; }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * 2. Serialization *<span class="hljs-javadoctag"> @param</span> dest *<span class="hljs-javadoctag"> @param</span> flags 0 or 1 */</span></span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">Writetoparcel</span></span>(Parcel dest,<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Flags) {dest.writestring (mname); Dest.writestringlist (mmembernamelist); Dest.writeparcelable (muser, flags); }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * 3. deserialization</span> /*</span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span>Creator<parcelablegroupbean> Creator =<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Creator<parcelablegroupbean> () {<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Reverse Sequence Create object *<span class="hljs-javadoctag"> @param</span> in *<span class="hljs-javadoctag"> @return</span> </span> */</span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>Parcelablegroupbean<span class="hljs-title"><span class="hljs-title">Createfromparcel</span></span>(Parcel In) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">New</span></span>Parcelablegroupbean (in); }<span class="hljs-javadoc"><span class="hljs-javadoc">/** * Reverse sequence Create an array of objects *<span class="hljs-javadoctag"> @param</span> size *<span class="hljs-javadoctag"> @return</span> </span> */</span> <span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span>parcelablegroupbean[]<span class="hljs-title"><span class="hljs-title">NewArray</span></span>(<span class="hljs-keyword"><span class="hljs-keyword">int</span></span>Size) {<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">New</span></span>parcelablegroupbean[size]; } };<span class="hljs-javadoc"><span class="hljs-javadoc">/** * 4. Automatically created constructor, using deserialization resulting Parcel constructed object *<span class="hljs-javadoctag"> @param</span> </span> in */</span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-title"><span class="hljs-title">Parcelablegroupbean</span></span>(Parcel In) {mname = in.readstring (); Mmembernamelist = In.createstringarraylist ();<span class="hljs-comment"><span class="hljs-comment">//deserialization, If you are familiar with a class that is also parcelable, you need to use its class loader as a parameter, otherwise the error cannot find the class</span></span>Muser = in.readparcelable (User.class.getClassLoader ()); }}</code></pre>Summarize<p><p>As you can see, the use of Serializable is simple, create a version number, and the parcelable is relatively complex, there will be four methods to Implement.</p></p><p><p>Generally in the storage of data to the SD card or network transmission is recommended to use Serializable, although the efficiency of some, Fortunately the use of convenience.</p></p><p><p>parcelable, such as intent,bundle, are recommended for runtime data transfer, and the Android bottom layer is optimized for high efficiency.</p></p>Thanks<p><p>Explore the art of Android development<br>Http://developer.android.com/reference/android/os/Parcelable.html</p></p> <p><p>Android Advanced 6: Two serialization modes Serializable and Parcelable</p></p></span>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.