Custom serialization - could you do this?
807588Jan 29 2009 — edited Jan 30 2009
Hi
I'll describe a partially hypothetical situation to allow me to ask the questions I want with some context.
Let's say someone gave you the responsibility of implementing the following API with the specified contract:
/**
* Convert <code>object</code> to a serialized format and save in the file
* <code>saveDestination</code>. You are free to choose the serialization format.
**/
public void serializeAndSave(java.io.Serializable object, java.io.File saveDestination);
}
/**
* Deserialize objects serialized by the <code>serializeAndSave()</code> method.
*/
public java.io.Serializable deserialize(java.io.File file);
Additionally, let's say the following were conditions and restrictions were specified by the person asking you to implement these methods:
* The version of the class for <code>object</code> or any of the super-classses in its class hierarchy or the type of any fields will never change.
* The fields of <code>object</code> will probably be private. Fields consistent with the java.io.Serializable contract, must be serialied (for instance, transient fields do not need to be serialized).
* You will never have any information about how any declared methods in <code>object</code> relate to declared fields, so you cannot rely on declared methods for getting field values when serializing or setting field values when deserializing.
* You will never have information about how parameters in declared constructors relate to declared fields, so you cannot rely on declared constructors for setting field values when deserializing.
* Besides the java.io.Serializable interface and the contract specified by Java when implementing the Serializable interface (eg. empty constructor), <code>object</code> will not necessarily conform to any interface or API that you will have prior knowledge of.
* <code>object</code> may have an inheritance hierarchy to any depth. Super-classes in the class hierarchy may have fields too.
* Super-classes in the object hierarchy, and their fields, are subject to the same conditions and constraints described here for <code>object</code>
OK, there's a description of the hypothetical task someone has asked you to complete.
Luckily, this is very easy to do. You can simply rely on Java's inbuilt serialization mechanisms. You can use java.io.ObjectOutputStream and java.io.ObjectInputStream - it's all very easy. Java's inbuilt serizlization/deserialization has no problem with private fields and no problems with super-classe fields even if private.
Now let's say I changed your task by changing the contract for the serializeAndSave() method as follows:
/**
* Convert <code>object</code> to an XML serialization format and save in the file
* <code>saveDestination</code>.
*/
public void serializeAndSave(java.io.Serializable object, java.io.File saveDestination);
Notice that the class must now be serialized in XML format*.
How would you implement the serializeAndSave() and deserialize() methods now? In particular:
* In serialzeAndSave(), how would you obtain the values of private fields in any of the super-classes in the object hierarchy?
* In deserialize(), assuming you succussfully implemented the serializeAndSave() method, how would you set the value of any of the private fields in <code>object</code> or any of the super-classes in <code>object</codes>'s class hierarchy?
Cheers.