protostuff/protostuff

Support for `readObject` and `writeObject` methods on Serializable classes

parasjain1 opened this issue · 0 comments

What is the proposal?

In Java there's a practice to implement readObject and writeObject methods on Serializable classes. This is a way in which the class conveys as to how it wants to be serialized and deserialized. We propose to have a configurable way to enable the use of these methods during serialization/deserialization.

Additional Context

To give an example, java.net.InetSocketAddress class, implements these methods. The only property it has is transient private final transient InetSocketAddressHolder holder. Now when we serialize InetSocketAddress object with protostuff, holder being a transient field will be ignored and no actual data is serialized.

Potential Solution

Add an overloaded method RuntimeSchema.getSchema(Class<?> clazz, bool enableCustom). Iff writeObject and readObject methods are present for the clazz -

  • generate schema with a single string attribute
  • The RuntimeSchema can hold a boolean flag custom = true

During serialization/deserialization check for custom flag on the RuntimeSchema object, if true rely on readObject of clazz to deserialize the string attribute.