2

I'm currently experimenting with Msgpack to de-serialize an object graph using Msgpack but I am hitting problems.

At the moment I have a simple hierachy consisting of two classes: BaseMessage and PersonNew. BaseMessage has a single Long field id with getters and setters. PersonNew has String fields name and email and inherits from BaseMessage.

Originally PersonOld class had the id field and it serialized/de-serialized fine. However when tring to de-serialize from PersonOld (which had the id) to PersonNew class (which inherits the id from BaseMessage) I'm being hit with the error:

org.msgpack.MessageTypeException: Unexpected raw value

All the fields I'm using are private.

Below is the sample code:

    PersonOld personOld = new PersonOld();
    personOld.setName("person");
    personOld.setEmail("person@email.com");

    MessagePack msgpack = new MessagePack();
    msgpack.register(PersonNew.class);
    msgpack.register(PersonOld.class);

    // Serialize using old person class
    byte[] personBytes = msgpack.write(personOld);

    ByteArrayInputStream in = new ByteArrayInputStream(personBytes);
    Unpacker unpacker = msgpack.createUnpacker(in);

    // Deserialize using new person class
    PersonNew deserializePerson = unpacker.read(PersonNew.class);

The reason I'd like to de-serialize to a newer class is because I want to see if it's possible to have older serialized data (e.g. in a persisted message queue) be compatible with newer classes (e.g. in case of class updates). I have attempted to even use the @Optional annotation on the id of the BaseMessage class but still doesn't work.

Is it possible at all to de-serialize with a different class despite having the same fields?

Regards,

SNK

Florian Neumann
  • 5,587
  • 1
  • 39
  • 48
SNK
  • 51
  • 1
  • 4

0 Answers0