- Forward


Serialization
in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Serialization
Back SMYC Forward
  • Defined:
    • The process of creating a "frozen" representation of a "live" (i.e., in-memory) object
  • Uses:
    • Copy objects
    • Provide persistence
    • Transport objects over a communications channel
Overview of Serialization in Java
Back SMYC Forward
  • The Serializable Interface:
    • Implemented by classes to indicate that their Objects can be serialized
    • Has no methods (serves only to identify the semantics of being serializable)
  • Serialization:
    • Handled by the writeObject() method in the ObjectOutputStream class
  • Deserialization:
    • Handled by the readObject() method in the ObjectInputStream class
The Serialization Process
Back SMYC Forward
  • An ObjectOutputStream maintains a mapping from instances and classes to handles.
  • When writeObject( ) is passed an instance that has not yet been written to the stream the instance is assigned a reference handle, the handle is written to the stream, and the instance data is written to the stream.
  • When writeObject() is called with an argument that has already been written to the stream, the handle is written to the stream, and no further operations are necessary.
  • When an instance is being serialized and it contains another serializable instance, the serialization of the first instance is suspended and the second instance is serialized. After the second instance is fully serialized, the serialization of the first instance resumes.
Versioning
Back SMYC Forward
  • A Potential Problem:
    • The class changes between when it is serialized and when it is deserialized
  • Avoiding this Problem:
    • Provide a version number within the class and objects
  • A "Tricky" Implementation:
    • Use a static attribute within the class
Serialization
Back SMYC Forward

An Example

Date d; FileOutputStream out; ObjectOutputStream serializer; String s; out = new FileOutputStream("objects.dat"); serializer = new ObjectOutputStream(out); s = new String("Today"); d = new Date(); serializer.writeObject(s); serializer.writeObject(d); serializer.flush(); out.close();
Deserialization
Back SMYC Forward

An Example

Date d; FileInputStream in; ObjectInputStream deserializer; String s; in = new FileInputStream("objects.dat"); deserializer = new ObjectInputStream(in); s = (String)deserializer.readObject(); d = (Date)deserializer.readObject(); in.close();
A Complete Example
Back SMYC Forward
A Simple Class
javaexamples/serialization/Course.java
 
A Complete Example (cont.)
Back SMYC Forward
A Reasonably Complicated Collection
javaexamples/serialization/CourseDatabase.java
 
A Complete Example (cont.)
Back SMYC Forward
Serialization
javaexamples/serialization/SerializationDriver.java
 
A Complete Example (cont.)
Back SMYC Forward
Deserialization
javaexamples/serialization/DeserializationDriver.java
 
Advanced Serialization in Java
Back SMYC Forward
  • Transients:
    • Values that can be recalculated (and, hence, don't need to be serialized) can be declared transient
  • For Special Handling You Should Implement:
    • private void readObject(ObjectInputStream stream)
    • private void writeObject(ObjectOutputStream stream)
Advanced Serialization in Java (cont.)
Back SMYC Forward
  • Preventing Serialization/Deserialization:
    • Implement writeObject() and readObject() methods that always throw the NotSerializableException
    • The exception will be caught by the ObjectOutputStream which will abort the serialization process.
  • Keeping Complete Control:
    • Implement the Externalizable interface
  • Specifying an Alternative Object for Serialization:
    • Implement the writeReplace() and readReplace() methods
Formatting the Serialized Object
Back SMYC Forward
  • One Approach:
    • Use a binary representation
  • Another Approach:
    • Use XML
There's Always More to Learn
Back -