Convert Byte[] to xml

Viewed 569

I'm using Axon 2.4.6. I have a Saga the payload of which was serialized in binary using XStreamSerializer.

The saga looks like this:

public class MySaga extends AbstractAnnotatedSaga {
    ...
    private MyEvent myEvent;
    ...
}

It contains one event which is the initialize event of the related aggregate object.

Right now I'm having is a deserialization problem because I changed MyEvent by adding one property to it. I figured out a workaround to this by adding the serialize id that the deserializer is expecting, however this solution might not be the best since I'm on production data right now and would be nice if I was able to somehow upcast the sagas.

So what I intend to do is create a custom serializer that extends JavaSerializer and tweak the SerializedObject<S> that is coming in. The problem is that the SerializedObject is in hex/binary so I need a way to convert it in to an org.dom4j object for instance so I could add the missing property and then be able to deserialize it in to MySaga.

I tried several approaches like

ByteArrayInputStream bos = new ByteArrayInputStream((byte [])serializedObject.getData());

or new XStream();

but they all go from the binary representation straight to the object deserialization, what I need is to get the dom4j or even xml conversion first. I can't figure out how to do it.

1 Answers

I have to say that Axon 2 is not something I have experience with but let me try to help you nonetheless.

As I can find on the docs, Axon provide an example of how to write an upcaster here using the correct 2.4 documentation link.

What is not clear for me, based on your question, is if you are using the JavaSerializer or an XStreamSerializer (or JacksonSerializer to make it complete).

In the case you are using XML, the docs will provide you an example of an upcaster. What is good to mention (and check) is that you can also look into xStream.ignoreUnknownElements() which will make your Serializer lenient meaning it won't fail when trying to deserialize something which contains an attribute it does not know (very useful I would say).

If you are using JSON, you also have the FAIL_ON_UNKNOWN_PROPERTIES "feature" that can be disabled in this case to make it lenient.

Making your serializers lenient seems to be the correct route if you ask me. If you really need to add a default/derived value to the new field, than the upcaster route is the one you should pick.

KR,


Edit 1: triggered by the comment given by Steven, this led me to add this edit and ask you how long do you expect this Saga to live. Now that I noticed the Event is part of your Saga, I would rather write a new Saga that does not contains the Event as part of it but just fields not coupling it to any specific Event.

Related