Serialization
Object Serialization Explanation +
Example of file + network
You want to send an object to a stream
Motivation
A lot of code involves boring conversion from a file to
memory
AddressBook program reads data from file and then parses it to
construct objects
This is a common problem!
ali, defence, 9201342
usman, gulberg, 5162346
[Link]
25-Oct-05
Serialization
Java: Automatic Serialization
Javas answer:
Serializable Interface
Serialization
Problem- Objects have state in memory
Serialization is also called
Object know how to read/write themselves to streams
Flattening, Streaming, Dehydrate (rehydrate = read),
Archiving
Found in [Link] package
Tagging interface has no methods and serves only to identify the
semantics of being serializable
Automatic Writing
System knows how to recursively write out the state of an object to stream
Recursively follows references and writes out those objects too!
Automatic Reading
By implementing this interface a class declares that it is willing to be
read/written by automatic serialization machinery
System knows how to read the data from Stream and re-create object in
memory
Downcasting is required
How it works?
To write out an object of PersonInfo
Example Code: Serialization
To read that object back in
PersonInfo p = new PersonInfo();
ObjectOutputStream out;
[Link](p)
Reading/Writing PersonInfo objects
ObjectInputStream in;
PersonInfo obj = (PersonInfo) [Link]();
Must be of the same type
class and version issue
5
25-Oct-05
CS391 Umair Javed
1
Example Code: Serialization
Example Code: Serialization (cont.)
import [Link]*;
import [Link].*;
import [Link].*;
public class WriteEx{
public static void main(String args[ ]){
public class PersonInfo implements Serializable{
PersonInfo pWrite = new PersonInfo("ali", "defence", "9201211");
String name;
String address;
String phoneNum;
try {
FileOutputStream fos = new FileOutputStream("[Link]");
ObjectOutputStream out = new ObjectOutputStream(fos);
//serialization
[Link](pWrite);
public void printPersonInfo( ){
[Link]( name: + name + address: + address +
phoneNum: + phoneNum);
[Link]();
[Link]();
} catch (Exception ex){
[Link](ex)
}
}
7
Example Code: Serialization (cont.)
Object Serialization and Network
import [Link]*;
public class ReadEx{
public static void main(String args[ ]){
You can read/write objects to network using sockets
try {
FileInputStream fis = new FileInputStream("[Link]");
ObjectInputStream in = new ObjectInputStream(fis);
//de - serialization
PersonInfo pRead = (PersonInfo) [Link]( );
The class version should be same on both sides (client
& Server) of the network
[Link]();
[Link]();
[Link]();
} catch (Exception ex){
[Link](ex)
}
}
9
10
Sending Objects over Network
Reading Objects from Network
..
..
PersonInfo p = new PersonInfo (ali, defence, 9201211);
Socket s = new Socket(localhost, 4444);
OutputStream os = [Link]();
ObjectOutputStream oos = new ObjectOutputStream(os);
[Link](p);
11
Socket s = [Link]();
InputStream in = [Link]();
ObjectInputStream ois = new ObjectInputStream(is);
PersonInfo p = (PersonInfo) [Link]( );
12
CS391 Umair Javed
2
Preventing Serailization
transient keyword is used to mark a field that should not be
serialized
import [Link].*;
import [Link].*;
public class PersonInfo implements Serializable{
Often there is no need to serialize sockets, streams & DB
connections etc (they do not represent the state of object, rather
connections to external resources)
String name;
String address;
transient String phoneNum;
So, we can mark them as
Example Code: transient
public void printPersonInfo( ){
public transient Socket s;
public transient OutputStream os;
public transient Connection con;
[Link]( name: + name + address: + address +
phoneNum: + phoneNum);
}
Transient fields are returned as null on reading
}
13
14
Circularity: not an issue
Serialization machinery will take circular references
into account and do the right thing!
15
CS391 Umair Javed
3