1

I'm using Protostuff in an attempt to serialize/deserialize objects of several different types for which no protobuf sources are available (it's a server-server RPC scenario). Serialization goes OK because I know the type of the object to serialize and can create the schema:

Schema schema = RuntimeSchema.getSchema(object.getClass());

Now, I use ProtobufIOUtil.toByteArray and get a byte array which I then pass to a remote server. However, I can't seem to deserialize this byte array in the remote server because I have no way to create a schema for an object of "unknown" type. Is there any way I can get past this and use Protostuff in the same way I would use Java's native serialization?

Roy Stark
  • 463
  • 2
  • 15

1 Answers1

1

There are few solutions with common idea - serialize name of the class together with the data.

First one requires protostuff-runtime. You should create wrapper class with one field of type Object:

public class Wrapper {
    public Object data;
}

Then you put your object to data field and serialize wrapper, protostuff-runtime will append class name to serialized form automatically, and later use it for deserialization.

If you want more control, then you can do similar thing without protistuff-runtime.

First, you need a wrapper class:

public class Wrapper {
    public String clazz;
    public byte[] data;
}

Then you should serialize your data to byte array, store it to wrapper, and then serialize wrapper instance.

On remote side, you deserialize Wrapper first, then get clazz field - it is the class you should use to deserialize data.

Kostiantyn
  • 935
  • 8
  • 13