Need help?

I'm available for remote short term contracting or consultancy work. Please check out my LinkedIn profile for more details on my experience.

Please feel free to use the form below to contact me.




Android Serialization Example (Java)

I'm going to go through the process of serializing and deserializing an object.

What this means is that we're going to convert an object into an array of bytes, which can easily be moved around or stored (for later use). And for deserialization we just take those bytes and convert them back into an Object.

The first one we have below is the serialization method, which just takes in a generic Object.


  public static byte[] serializeObject(Object o) {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();

    try {
      ObjectOutput out = new ObjectOutputStream(bos);
      out.writeObject(o);
      out.close();

      // Get the bytes of the serialized object
      byte[] buf = bos.toByteArray();

      return buf;
    } catch(IOException ioe) {
      Log.e("serializeObject", "error", ioe);

      return null;
    }
  }

The second one is the deserialization method, which takes in an array of bytes;


  public static Object deserializeObject(byte[] b) {
    try {
      ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(b));
      Object object = in.readObject();
      in.close();

      return object;
    } catch(ClassNotFoundException cnfe) {
      Log.e("deserializeObject", "class not found error", cnfe);

      return null;
    } catch(IOException ioe) {
      Log.e("deserializeObject", "io error", ioe);

      return null;
    }
  }

If you're wondering how to use these, well here is an example.


  public class Bla implements Serializable {
    public String blaDate;
    public String blaText;
  }
  
  Bla bla = new Bla();
  
  // assuming that both those serialize and deserialize methods are under the SerializerClass
  byte[] blaBytes = SerializerClass.serializeObject(bla);
  
  Bla deserializedBla = (Bla) SerializerClass.deserializeObject(blaBytes);

You've probably noticed that I implemented Serializable - you need this in order to serialize an object. Some of the built in objects already have this implemented - for example the ArrayList object.

When deserializing you need to cast it back to what the object was, since it's returning it as a generic Object - hence the (Bla).

Anyways, hope this helped someone, and if you noticed any mistakes, please let me know.


jon | December 02, 2010 | Comments (5)

Comments

Thanks a lot man!! I was searching on how to save ArrayList into db and most of the posts made it so complicated. I tried your code and converted an ArrayList to Bytes then save it to my db and pulled it back and it works as charm!
Comment by Murad Kablan - April 22, 2012 @ 4:56 pm
Thank you.. it helped me in understanding the concept
Comment by Raghu - December 28, 2011 @ 9:03 am
Is it possible to delete/remove an object which has already been serialized and written to the file?
Comment by Nisha - October 19, 2011 @ 3:12 am
Hi Jon,

thanks for your article! I was wondering if this method would work well, if the member variables of the serializable class (Bla) were more complex objects themselves instead of being just strings.

Is this possible or do these objects have to be serializable as well?

Thanks for any directions!

Peter
Comment by peter - August 25, 2011 @ 3:55 pm
Nice article , you have indeed covered topic in details with sample code and graphics, its indeed a topic which require a deeper understanding than many other java topics.

Javin
How Garbage collection works in Java
Comment by Javin @ Serialization in Java - April 21, 2011 @ 10:38 am

Name (required)
Email (will not be published) (required)
Website

captcha