Question

Is there an equivalent to the C# [OnSerialized] and [OnDeserialized] attributes in Java?

Was it helpful?

Solution

If you're using default Java serialization, you can override the methods used to serialize and deserialize. These methods are:

private void writeObject(ObjectOutputStream out) throws IOException
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException

Just call the "default" method for each of these: out.defaultWriteObject(out) and in.defaultReadObject(in).

There's a very detailed article on Java serializtion at http://java.sun.com/developer/technicalArticles/Programming/serialization/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top