Smart IT Group

A blog for IT Students and Professionals

Serialisation & Serializable Object

What is Serialisation?

In computer science, in the context of data storage and transmission, serialisation is the process of converting an object into a sequence of bits so that it can be stored on a storage medium (such as a file, or a memory buffer) or transmitted across a network connection lnk. When the resulting series of bits is reread according to the serialization format, it can b used to create a semantically identical clone of the original object. For many complex objects, such as those that make extensive use of references, this process is not straightforward.

Advantages of Serialisation

  • A method of persisting objects which is more convenient than writing their properties to a text file on disk, and re-assembling them by reading this backin.
  • A method of issuing remote procedure calls such as SOAP
  • A method for distributing objects, especially in software componentry such as COM, CORBA etc
  • A method for detecting changes in time-varying data.

Serializable Object:

To serialize an object means to convert its state to a bite stream so way that the byte stream can be reverted back into a copy of the object. A Java object is serializable if its class or any of its superclasses implements eithe the java.io.Serializable interface or its subinterface, java.io.Externalizable.

When an object is serialized, information that identifies its class is recorded in the serialized stream. However the class’s definition (“class file”) itself is not recorded. It is the responsibility of the system that is deserializing the object to determine how to locate and load necessary class files. For example, a Java application might include in its classpath a JAR file that contains the class files of the serialized object(s) or load the class definitions by using information stored in the directory, as explained later in this lesson.

The following example invokes Context.bind() to bind an AWT button to the name "cn=Button". To associate attributes with the new binding, you use DirContext.bind() . To overwrite an existing binding, use Context.rebind() and DirContext.rebind() .

// Create the object to be bound
Button b = new Button("Push me");
 
// Perform the bind
ctx.bind("cn=Button", b);

You can then read the object back using Context.lookup() , as follows.

// Check that it is bound
Button b2 = (Button)ctx.lookup("cn=Button");
System.out.println(b2);

Running this example produces the following output.

# java SerObj
java.awt.Button[button0,0,0,0x0,invalid,label=Push me]

When a serialized object is bound in the directory as shown in the previous example, applications that read the serialized object from the directory must have access to the class definitions necessary to deserialize the object. Alternatively, you can record a codebase with the serialized object in the directory, either when you bind the object or subsequently by adding an attribute by using DirContext.modifyAttributes() . You can use any attribute to record this codebase and have your application read that attribute from the directory and use it appropriately. Or you can use the "javaCodebase" attribute specified in RFC 2713. In the latter case, Sun’s LDAP service provider will automatically use the attribute to load the class definitions as needed. "javaCodebase" should contain the URL of a codebase directory or a JAR file. (Note that JAR files work only with the Java 2 platform). If the codebase contains more than one URL, then each URL must be separated by a space character. (“Serializable Object”, n.d.)

Reference:

Wikipedia. (2008). Serialization. Retrieved June 07, 2009, from http://en.wikipedia.org/wiki/Serialization

Serializable. (n.d.). Retrieved June 07, 2009, from http://java.sun.com/products/jndi/tutorial/objects/storing/serial.html

June 7, 2009 - Posted by xshi08 | Articles | | 4 Comments

4 Comments »

  1. Anna
    Yes, your explanations are very good and clear. It makes sense to every one.

    Comment by dilini7 | June 7, 2009 |

    • Hello Delini,
      Vu has suggest that i cannot put in text reference for wikipedia but can leave it in the reference list. What do you think? I have just removed the in text reference of wikipedia.

      Comment by xshi08 | June 7, 2009 |

  2. Thanks for this post which explains your previous post in more details.
    Vu.

    Comment by PHAM VU DUNG | June 7, 2009 |

  3. thanks for your explaination. This helps me to understand the content very much.

    Comment by sunny | June 7, 2009 |


Leave a comment

You must be logged in to post a comment.