0
Serialization in Java Critical for Java network abilities Jeslie Chermak ( [email_address] )
What is “serialization”? <ul><li>Serialization is the (reversible) encoding of objects, and the objects reachable in them,...
Why Should I Use It? <ul><li>BASIS FOR ALL PERSISTENCE IN JAVA ! </li></ul><ul><li>Simple to add to most classes. </li></u...
Simple Class <ul><li>Create a new class in package  com.jcc.training.generics </li></ul><ul><li>Class will implement a tra...
Java 1.4 Implementation <ul><li>package  com.jcc.training.generics; </li></ul><ul><li>import  java.util.ArrayList; </li></...
WIBNIF <ul><li>Problem: How do we make this  class  serializable? </li></ul><ul><li>Solution: Use a special “marker”  inte...
Java 1.4 Implementation <ul><li>package  com.jcc.training.generics; </li></ul><ul><li>import  java.util.ArrayList; </li></...
WIBNIF <ul><li>Problem: Java has evolved since 1.4 </li></ul><ul><li>Solution: Serialization has been present since initia...
Java 5 with Generics <ul><li>package  com.jcc.training.generics; </li></ul><ul><li>import  java.io.Serializable; // missin...
WIBNIF <ul><li>Problem:  class es evolve over time </li></ul><ul><li>Solution: introduce a version UID to capture “real” c...
Improved   and Versioned <ul><li>package  com.jcc.training.generics; </li></ul><ul><li>import  java.util.ArrayList; </li><...
WIBNIF <ul><li>Problem: some code lacks the UID </li></ul><ul><li>Solution: define default UID to match that normally buil...
Good News <ul><li>Most (all?) IDEs provide a  serialver  hook </li></ul><ul><li>Writing simple objects is easy </li></ul><...
Object I/O <ul><li>Writing: </li></ul><ul><li>public   void  writeIt( OutputStream  os,  Foo  foo) { </li></ul><ul><li>Obj...
WIBNIF <ul><li>Problem: not every field should be serialized </li></ul><ul><li>Solution:  transient  fields </li></ul><ul>...
Bad News <ul><li>Default  readObject()  and  writeObject()  methods may not properly handle objects with  transient  and/o...
Good News <ul><li>Object.readObject()  and  Object.writeObject()  methods (used in default handling) are overloadable:  </...
Better News <ul><li>When default handling is insufficient, use the  Externalizable  interface instead of  Serializable  an...
Going Further … <ul><li>Java tutorials:  http://download.oracle.com/javase/tutorial/index.html </li></ul><ul><li>Serializa...
Upcoming SlideShare
Loading in...5
×

Java Serialization

2,728

Published on

Simple introduction to the core concepts of serialization in Java and how to add serialization to any Java class

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
2,728
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
85
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • GOOD AFTERNOON MY NAME IS …
  • WHY USE THIS? I’M PRODUCTIVE AND COMFORTABLE AS IS.
  • WE’RE GOING TO MAKE A SIMPLE CLASS TO ILLUSTRATE HOW EASY SERIALIZATION CAN BE.
  • WE START WITH A PRE-JAVA 5 VERSION. WE’LL USE JAVA 1.4 NOTE THAT OUR EXAMPLE LACKS JAVADOC COMMENTS. DON’T DO THIS IN ANY OF YOUR OWN CODE!
  • OUR CODE WAS NOT BUILT TO BE SAVEABLE – WHAT DO I CHANGE … ?
  • … WE CHANGED JUST ONE (1) LINE!
  • WHAT ABOUT LEGACY CODE? HOW HARD TO UPGRADE?
  • WE’LL REWIRTE OUR EXAMPLE FOR JAVA 5 BUT NOTICE THE CHANGE REQUIRED FOR SERIALIZATION (NONE!)
  • WHAT ABOUT NEW CODE EVOLUTION?
  • MUCH FANCIER JAVA GENERIC CODE WITH A “Unique ID”
  • OKAY, BUT WHAT ABOUT LEGACY CODE WITHOUT THE UID?
  • HOW “EASY” IS “EASY”?
  • IS EVERYTHING SAVED? CAN’T WE CONTROL WHAT WE KEEP?
  • THIS WAS JUST AN INTRODUCTION. THERE ARE SOME VERY USEFUL TOPICS WE HAVE NOT EVEN TOUCHED ON.
  • Transcript of "Java Serialization"

    1. 1. Serialization in Java Critical for Java network abilities Jeslie Chermak ( [email_address] )
    2. 2. What is “serialization”? <ul><li>Serialization is the (reversible) encoding of objects, and the objects reachable in them, into a stream of bytes. It is by no means unique to Java: http://en.wikipedia.org/wiki/Serialization </li></ul><ul><li>This talk is based on information from http://download.oracle.com/javase/tutorial/essential/io/objectstreams.html </li></ul>
    3. 3. Why Should I Use It? <ul><li>BASIS FOR ALL PERSISTENCE IN JAVA ! </li></ul><ul><li>Simple to add to most classes. </li></ul><ul><li>Allows for easy extension. </li></ul><ul><li>Handles versioning. </li></ul>
    4. 4. Simple Class <ul><li>Create a new class in package com.jcc.training.generics </li></ul><ul><li>Class will implement a traditional “stack” containing Integer s </li></ul><ul><li>Only methods are push , pop , top </li></ul><ul><li>Allow NULL s in stack </li></ul>
    5. 5. Java 1.4 Implementation <ul><li>package com.jcc.training.generics; </li></ul><ul><li>import java.util.ArrayList; </li></ul><ul><li>import java.util.List; </li></ul><ul><li>public class Stack { </li></ul><ul><li>private final List stack = new ArrayList(); </li></ul><ul><li>public void push( final Integer value) { </li></ul><ul><li>this . stack .add(value); // NULL allowed! </li></ul><ul><li>} </li></ul><ul><li>public Integer top() { </li></ul><ul><li>if ( this . stack .isEmpty()) throw new IllegalStateException(); </li></ul><ul><li>return (Integer) this . stack .get( this . stack .size() - 1); </li></ul><ul><li>} </li></ul><ul><li>public Integer pop() { </li></ul><ul><li>final Integer value = this .top(); </li></ul><ul><li>this . stack .remove( this . stack .size() - 1); </li></ul><ul><li>return value; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    6. 6. WIBNIF <ul><li>Problem: How do we make this class serializable? </li></ul><ul><li>Solution: Use a special “marker” interface -- Serializable </li></ul>
    7. 7. Java 1.4 Implementation <ul><li>package com.jcc.training.generics; </li></ul><ul><li>import java.util.ArrayList; </li></ul><ul><li>import java.util.List; </li></ul><ul><li>public class Stack implements java.io.Serializable { // marker interface </li></ul><ul><li>private final List stack = new ArrayList(); </li></ul><ul><li>public void push( final Integer value) { </li></ul><ul><li>this . stack .add(value); // NULL allowed! </li></ul><ul><li>} </li></ul><ul><li>public Integer top() { </li></ul><ul><li>if ( this . stack .isEmpty()) throw new IllegalStateException(); </li></ul><ul><li>return (Integer) this . stack .get( this . stack .size() - 1); </li></ul><ul><li>} </li></ul><ul><li>public Integer pop() { </li></ul><ul><li>final Integer value = this .top(); </li></ul><ul><li>this . stack .remove( this . stack .size() - 1); </li></ul><ul><li>return value; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    8. 8. WIBNIF <ul><li>Problem: Java has evolved since 1.4 </li></ul><ul><li>Solution: Serialization has been present since initial public release (1.0.2) -- it is a core feature of the language implementation </li></ul>
    9. 9. Java 5 with Generics <ul><li>package com.jcc.training.generics; </li></ul><ul><li>import java.io.Serializable; // missing earlier </li></ul><ul><li>import java.util.ArrayList; </li></ul><ul><li>import java.util.List; </li></ul><ul><li>public class Stack implements Serializable { // marker interface </li></ul><ul><li>private final List<Integer> stack = new ArrayList<Integer>(); // field typed! </li></ul><ul><li>public void push( final Integer value) { </li></ul><ul><li>this . stack .add(value); // NULL allowed! </li></ul><ul><li>} </li></ul><ul><li>public Integer top() { </li></ul><ul><li>if ( this . stack .isEmpty()) throw new IllegalStateException(); </li></ul><ul><li>return this . stack .get( this . stack .size() - 1); // no cast! </li></ul><ul><li>} </li></ul><ul><li>public Integer pop() { </li></ul><ul><li>final int value = this .top(); // OOPS! </li></ul><ul><li>this . stack .remove( this . stack .size() - 1); </li></ul><ul><li>return value; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    10. 10. WIBNIF <ul><li>Problem: class es evolve over time </li></ul><ul><li>Solution: introduce a version UID to capture “real” changes </li></ul>
    11. 11. Improved and Versioned <ul><li>package com.jcc.training.generics; </li></ul><ul><li>import java.util.ArrayList; </li></ul><ul><li>import java.util.List; </li></ul><ul><li>public class Stack<E> implements java.io.Serializable { // class typed! </li></ul><ul><li>private static final long serialVersionUID = 1L; // we specified private final List<E> stack = new ArrayList<E>(); // typed by class public void push( final E value) { // typed by class </li></ul><ul><li>this . stack .add(value); // NULL allowed! </li></ul><ul><li>} </li></ul><ul><li>public E top() { // typed by class </li></ul><ul><li>if ( this . stack .isEmpty()) throw new IllegalStateException(); </li></ul><ul><li>return this . stack .get( this . stack .size() - 1); </li></ul><ul><li>} </li></ul><ul><li>public E pop() { // typed by class </li></ul><ul><li>final E value = this .top(); // typed by class </li></ul><ul><li>this . stack .remove( this . stack .size() - 1); </li></ul><ul><li>return value; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>
    12. 12. WIBNIF <ul><li>Problem: some code lacks the UID </li></ul><ul><li>Solution: define default UID to match that normally built in a class -- serialver program </li></ul>
    13. 13. Good News <ul><li>Most (all?) IDEs provide a serialver hook </li></ul><ul><li>Writing simple objects is easy </li></ul><ul><li>Reading objects is comparable </li></ul>
    14. 14. Object I/O <ul><li>Writing: </li></ul><ul><li>public void writeIt( OutputStream os, Foo foo) { </li></ul><ul><li>ObjectOutputStream oos = new ObjectOutputStream (os); </li></ul><ul><li>oos.writeObject(foo); </li></ul><ul><li> oos.close(); </li></ul><ul><li>} </li></ul><ul><li>Reading: </li></ul><ul><li>public Foo readIt( InputStream is) { </li></ul><ul><li>ObjectInputStream ois = new ObjectInputStream (is); </li></ul><ul><li>Foo foo = ( Foo ) ois.readObject(); </li></ul><ul><li> ois.close(); </li></ul><ul><li>return foo; </li></ul><ul><li>} </li></ul>
    15. 15. WIBNIF <ul><li>Problem: not every field should be serialized </li></ul><ul><li>Solution: transient fields </li></ul><ul><li>Problem: static fields are the same for each object instance </li></ul><ul><li>Solution: they are not written by default </li></ul>
    16. 16. Bad News <ul><li>Default readObject() and writeObject() methods may not properly handle objects with transient and/or static fields </li></ul>
    17. 17. Good News <ul><li>Object.readObject() and Object.writeObject() methods (used in default handling) are overloadable: </li></ul><ul><li>private void readObject( ObjectInputStream ) </li></ul><ul><li>throws IOException, ClassNotFoundException; </li></ul><ul><li>private void writeObject( ObjectOutputStream ) </li></ul><ul><li>throws IOException; </li></ul>
    18. 18. Better News <ul><li>When default handling is insufficient, use the Externalizable interface instead of Serializable and implement readExternal() and writeExternal() -- a lot more work for total control of serialization </li></ul>
    19. 19. Going Further … <ul><li>Java tutorials: http://download.oracle.com/javase/tutorial/index.html </li></ul><ul><li>Serialization in depth w/examples: http://download.oracle.com/javase/7/docs/technotes/guides/serialization/index.html </li></ul><ul><li>Object streams: http://download.oracle.com/javase/tutorial/essential/io/objectstreams.html </li></ul><ul><li>Google “java serialization” </li></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×