JMU
Serialization
in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu


Serialization
Overview of Serialization in Java
The Serialization Process
Versioning
Serialization

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

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
A Simple Class
javaexamples/serialization/Course.java
import java.io.Serializable;


/**
 * A Course 
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class Course implements Serializable
{
    private static final long serialVersionUID = 1L;

    private int    number;
    private String department, title;

    /**
     * Explicit Value Constructor
     *
     * @param department  The department code
     * @param number      The course number
     * @param title       The course title
     */
    public Course(String department, int number, String title)
    {
       this.department = department;
       this.number     = number;
       this.title      = title;
    }

    /**
     * Get the course designation
     *
     * @return  The course designation
     */
    public String getDesignation()
    {
       return department+number;
    }

    /**
     * Get the course title
     *
     * @return  The course title
     */
    public String getTitle()
    {
       return title;
    }
}
        
A Complete Example (cont.)
A Reasonably Complicated Collection
javaexamples/serialization/CourseDatabase.java
import java.io.Serializable;
import java.util.Iterator;
import java.util.HashMap;


/**
 * A database of Course objects
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class CourseDatabase implements Serializable
{
    private static final long serialVersionUID = 1L;

    private HashMap<String, Course>     db;

    /**
     * Constructor
     */
    public CourseDatabase()
    {
       db = new HashMap<String,Course>();
    }

    /**
     * Add a Course
     *
     * @param id       The identifier
     * @param course   The Course to add
     */
    public void add(String id, Course course)
    {
       db.put(id, course);
    }

    /**
     * Get the values (i.e., Course objects) in the database
     *
     * @return  The values
     */
    public Iterator<Course> iterator()
    {
       return db.values().iterator();
    }

    /**
     * Get a Course
     *
     * @param  department  The department identifier
     * @param  number      The course number
     * @return             The Course to get
     */
    public Course get(String department, int number)
    {
       return db.get(department+number);
    }

    /**
     * Remove a Course
     *
     * @param department   The department identifier
     * @param number       The course number
     */
    public void remove(String department, int number)
    {
       db.remove(department+number);
    }
}
        
A Complete Example (cont.)
Serialization
javaexamples/serialization/SerializationDriver.java
import java.io.*;

/**
 * An example of serialization
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class SerializationDriver
{
    public static void main(String[] args)
    {
        Course               cross;
        CourseDatabase       db;
        FileOutputStream     out;
        ObjectOutputStream   serializer;

        cross = new Course("CS/MATH",227,"Discrete Mathematics");
       
        db = new CourseDatabase();
        db.add("CS139",  new Course("CS",139,"Algorithm Development"));
        db.add("CS227",  cross);
        db.add("CS240",  new Course("CS",240,"Data Structures and Algorithms"));
        db.add("MATH227", cross);


        try 
        {
            out = new FileOutputStream("objects.dat");
            serializer = new ObjectOutputStream(out);
            
            serializer.writeObject(db);
            serializer.flush();
            out.close();
        } 
        catch (Exception e) 
        {
            e.printStackTrace();
        }
    }

}
        
A Complete Example (cont.)
Deserialization
javaexamples/serialization/DeserializationDriver.java
import java.io.*;
import java.util.*;

/**
 * An example of deserialization
 *
 * @author  Prof. David Bernstein, James Madison University
 * @version 1.0
 */
public class DeserializationDriver
{
    public static void main(String[] args)
    {
       Course               course;
       CourseDatabase       db;
       Iterator<Course>     i;
       FileInputStream      in;
       ObjectInputStream    deserializer;

       try 
       {
          in = new FileInputStream("objects.dat");
          deserializer = new ObjectInputStream(in);
            
          db = (CourseDatabase)deserializer.readObject();
          in.close();

          i = db.iterator();
          while (i.hasNext()) 
          {
             course = i.next();
             System.out.println(course.getDesignation()+"\t"+
                                course.getTitle());
          }
       } 
       catch (Exception ex) 
       {
          ex.printStackTrace();
       }
    }

}
        
Advanced Serialization in Java
Advanced Serialization in Java (cont.)
Formatting the Serialized Object