Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI...

12
Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP://WWW-SCF.USC.EDU/ ~CSCI201 USC CSCI 201L

Transcript of Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI...

Page 1: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

SerializationCSCI 201L

Jeffrey Miller, Ph.D.

HTTP://WWW-SCF.USC.EDU/~CSCI201

USC CSCI 201L

Page 2: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Outline

USC CSCI 201L 2/12

▪ Serialization and File I/O

Page 3: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Saving Objects

▪ When writing objects out to an output stream, you may be tempted to save all of the data that is within the object individually

▪ Although this may work, you need to make sure to save all of the data from parent classes up the hierarchy as well› This is necessary if you want to save the exact state of a

program or copy that state to another program

▪ Reading all of this data back into the exact variables may be challenging if you do not have access to set the values of all of the variables

USC CSCI 201L 3/12Serialization

Page 4: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Saving Object Example1 class Employee {2 private String fname, lname;3 private long salary;4 public Employee(String fname, String lname, long salary) {5 this.fname = fname;6 this.lname = lname;7 this.salary = salary;8 }9 public void saveData(PrintWriter pw) {10 pw.println(fname + “,” + lname + “,” + salary);11 }12 }1314 public class Test {15 public static void main(String [] args) {16 Employee emp = new Employee(“donald”, “trump”, “billionaire”);17 PrintWriter pw = new PrintWriter(new FileWriter(“employees.txt”));18 emp.saveData(pw);19 }20 }

USC CSCI 201L 4/12Serialization

Page 5: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Saving Object Example1 class Person {2 protected String fname, lname;3 public Person(String fname, String lname) {4 this.fname = fname;5 this.lname = lname;6 }7 }8 class Employee extends Person {9 private long salary;10 public Employee(String fname, String lname, long salary) {11 super(fname, lname);12 this.salary = salary;13 }14 public void saveData(PrintWriter pw) {15 pw.println(fname + “,” + lname + “,” + salary);16 }17 }1819 public class Test {20 public static void main(String [] args) {21 Employee emp = new Employee(“donald”, “trump”, “billionaire”);22 PrintWriter pw = new PrintWriter(new FileWriter(“employees.txt”));23 emp.saveData(pw);24 }25 }

USC CSCI 201L 5/12Serialization

Page 6: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Saving Object Example1 class Person {2 private String fname, lname;3 public Person(String fname, String lname) {4 this.fname = fname;5 this.lname = lname;6 }7 }8 class Employee extends Person {9 private long salary;10 public Employee(String fname, String lname, long salary) {11 super(fname, lname);12 this.salary = salary;13 }14 public void saveData(PrintWriter pw) {15 pw.println(fname + “,” + lname + “,” + salary); // will this compile?16 }17 }1819 public class Test {20 public static void main(String [] args) {21 Employee emp = new Employee(“donald”, “trump”, “billionaire”);22 PrintWriter pw = new PrintWriter(new FileWriter(“employees.txt”));23 emp.saveData(pw);24 }25 }

USC CSCI 201L 6/12Serialization

Page 7: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Serialization Overview

▪ The java.io.Serializable interface allows us to specify to the JVM that the current object can be stored in an ObjectOutputStream and then retrieved from ObjectInputStream

▪ The object’s state will be restored exactly when retrieved from the ObjectInputStream› This includes all of the private variables from inherited classes as well

▪ The java.io.Serializable interface does not contain any methods▪ If an object does not implement the java.io.Serializable interface

and tries to be serialized, a NotSerializableException will be thrown

▪ static and transient variables of the class are not serialized› If a variable in the serializable class is another object, that object must be

serializable or a NotSerializableException will be thrown› You can make the variable transient instead, which means the variable

will not be serialized

USC CSCI 201L 7/12Serialization

Page 8: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

serialVersionUID

▪ The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.

▪ If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException.

▪ A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long› public static final long serialVersionUID = 1;

From http://docs.oracle.com/javase/7/docs/api/java/io/Serializable.html

USC CSCI 201L 8/12Serialization

Page 9: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Array Serialization Example1 import java.io.*; // Note that I imported * to save space23 public class Test {4 public static void printArray(String [] arr) {5 for (int i=0; i < arr.length; i++) {6 System.out.println("[" + i + "] = " + arr[i]);7 }8 }9 10 public static void main(String [] args) {11 try {12 String [] strarr = {"CSCI", "201", "Spring", "Summer", "Fall"};13 printArray(strarr);14 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("o.txt"));15 oos.writeObject(strarr);16 oos.close();17 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("o.txt"));18 String [] sarr = (String[])ois.readObject();19 printArray(sarr);20 ois.close();21 } catch (FileNotFoundException fnfe) {22 System.out.println("FileNotFoundException: " + fnfe.getMessage());23 } catch (IOException ioe) {24 System.out.println("IOException: " + ioe.getMessage());25 } catch (ClassNotFoundException cnfe) {26 System.out.println("ClassNotFoundException: " + cnfe.getMessage());27 }28 }29 }

USC CSCI 201L 9/12Serialization

Page 10: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Custom Serialization Example1 import java.io.*; 2 class Employee implements Serializable { 3 public static final long serialVersionUID = 1; 4 private String fname, lname; 5 private transient String pass; 6 public Employee(String fname, String lname, String pass) { 7 this.fname = fname; 8 this.lname = lname; 9 this.pass = pass; 10 } 11 public void printEmployee() { 12 System.out.println(fname + " " + lname + ": " + pass); 13 } 14 } 15 public class EmployeeMain {16 public static void main(String [] args) {17 Employee emp = new Employee("donald", "trump", "billionaire");18 try {19 ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("out.txt"));20 oos.writeObject(emp);21 oos.flush();22 oos.close();23 ObjectInputStream ois = new ObjectInputStream(new FileInputStream("out.txt"));24 Employee emp1 = (Employee)ois.readObject();25 ois.close();26 emp.printEmployee();27 emp1.printEmployee();28 } catch (FileNotFoundException fnfe) {29 System.out.println("fnfe: " + fnfe.getMessage());30 } catch (IOException ioe) {31 System.out.println("ioe: " + ioe.getMessage());32 } catch (ClassNotFoundException cnfe) {33 System.out.println("cnfe: " + cnfe.getMessage());34 }35 }36 }

USC CSCI 201L 10/12Serialization

Page 11: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Networking Serialization

▪ Just as we have written out to a file and read it back into objects, we can do the same with transmitting data over the line to another program

▪ We will see this when we talk about networking later in the semester

USC CSCI 201L 11/12Serialization and Networking

Page 12: Serialization CSCI 201L Jeffrey Miller, Ph.D. HTTP :// WWW - SCF. USC. EDU /~ CSCI 201 USC CSCI 201L.

Program

▪ Create a class that contains information about an employee, including name, address, and social security number. For the address, create another class with number, street, city, state, and zip in it.

▪ All of the data except social security number should be saved to a file and be read back into the same objects.

USC CSCI 201L 12/12Serialization