protostuff icon indicating copy to clipboard operation
protostuff copied to clipboard

How to deserialize e.g. a Singleton as part of the graph

Open mikaelgrev opened this issue 6 years ago • 3 comments

I'm using runtime schema and @Tag-ging everything. Is there a simple way to control the re-creation of objects in the graph, e.g. if I have a singleton as the object on a field in a message?

The message itself is easy enough to handle, but I have not found a way to control the creation of the parts of the message.

What I'm looking for is basically the equivalent of Serializable´s readResolve().

mikaelgrev avatar Mar 21 '18 14:03 mikaelgrev

Have you checked out delegates? It should be able to solve your problem.

dyu avatar Mar 21 '18 14:03 dyu

I have tried, but they seem AFAIK to work in message level, right? I can't find a way to register something that can handle fields of a message, or any message.

I have lots and lots of message (root) types (hundreds) and want a way to make all Singleton objects of a specific type, where ever in the graph under the message, to be deserialized the same way. With Java serializable I just override readResolve() in the singleton and return the single instance.

The simplest way would be to have an annotation on the singleton class, like @ReadResolve(..) where one could specify the method name (would default to "readResolve") that is called to return a possibly modified version, or another object, just like readResolve() does.

Even better (more performant) would be if Delegates could be registered for a specific type, then the object wouldn't have to be created in the first place.

mikaelgrev avatar Mar 21 '18 15:03 mikaelgrev

that is called to return a possibly modified version, or another object, just like readResolve() does.

You write and register a delegate and return the version (depending on the content) in the method readFrom. That is equivalent to the readResolve you're describing.

If you have inheritance, you register the concrete class, not the base/parent class.

dyu avatar Mar 21 '18 23:03 dyu