0 votes
4k views
in Java by
How to serialize a non-serializable object in Java ?

1 Answer

0 votes
by (3.7k points)

If you want to serialise java object then you must be implement Serializable interface otherwise can't be serialise a class. If you want to do this, you should implement readObject and writeObject on your wrapper class so you can serialise its objects in a custom way.

First, make your non-serialisable field transient.
In writeObject method, first call defaultWriteObject on the stream to store all the non-transient fields, then call other methods to serialise the individual properties of your non-serialisable object.
In readObject method, first call defaultReadObject on the stream to read back all the non-transient fields, then call other methods (corresponding to the ones you added to writeObject) to deserialise your non-serialisable object.

If your class already implements the Serializable interface, you must to do declare the field you don't want to serialize with transient:

    public transient String desc;

To make a non-serializable class to serializable:-

I have a class which is not serializable. If i will try to serialize the object of it than i will get an exception "java.io.NotSerializableException"

For Example:-
class MyNonSerializableClass {
 String str = new String("Welcome");
 MyNonSerializableClass(){
 
 }

 MyNonSerializableClass(String s) {
  this.str = s;
 }

 public String getStr() {
  return str;
 }

 public void setStr(String str) {
  this.str = str;
 }

}

In order to serialize the object we can write a wrapper class over MyNonSerializableClass and which is implementing Serializable interface.
And we control the readObject and writeObject methods so that we can decide what need to be written with the wrapper class.

For Example:-
class MyNonSerializableClassWrapper extends MyNonSerializableClass implements Serializable {
 MyNonSerializableClassWrapper(){
  super();
 }
 MyNonSerializableClassWrapper(String s) {
  super(s);
 }

 private void writeObject(ObjectOutputStream out) throws IOException {
  // not required the default write object
  // ----> out.defaultWriteObject();
  out.writeObject(super.getStr());
 }

 private void readObject(ObjectInputStream in) throws IOException {
  // not required the default read object
  // ----> in.defaultReadObject();
  try {
   super.setStr((String) in.readObject());
  } catch (ClassNotFoundException e) {
   e.printStackTrace();
  }
 }
}

By getting the super class value and setting it during the write and read to serialized object we can achieve the serialization and deserialization even if a class is not implementing Serializable interface. Like:-

private void WriteObjectToFile() {
try {
  FileOutputStream fo = new FileOutputStream("c:\\test.ser");
  ObjectOutputStream os = new ObjectOutputStream(fo);
  os.writeObject(new MyNonSerializableClassWrapper("This is test of serialization"));
  fo.close();

  }
  catch (FileNotFoundException e) {
    e.printStackTrace();
  }
  catch (IOException e) {
    e.printStackTrace();
  }
}


private void ReadObjectFromFile() {
try {
  FileInputStream fi = new FileInputStream("c:\\test.ser");
  ObjectInputStream in = new ObjectInputStream(fi);
  MyNonSerializableClassWrapper mw = (MyNonSerializableClassWrapper)
  in.readObject();
  fi.close();
  System.out.println("serialized object value is: " + mw.getStr());
  }
  catch (FileNotFoundException e) {
      e.printStackTrace();
  }
  catch (IOException e) {
    e.printStackTrace();
  }
  catch (ClassNotFoundException e) {
    e.printStackTrace();
  }
}

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...