0

We are using XStream to serialize and deserialize entities. If the entities are persisted, they have an ID and the serialized form of the object is the ID only which could look like:

<Person>4</Person>

"Deserialization" is simply the read operation from the database.

This works pretty good implementing a SingleValueConverter which uses a DAO to load the entity:

public class TestSerializer implements SingleValueConverter {
    public Object fromString(String str) {
        return dao.readById(str);
    }
    public String toString(Object obj) {
        return ((Entity) obj).getId();
    }
}

But in some cases the entities which have to be serialized are not persisted. So they do not have an ID and I have to do a "real" serialization. Since XStream knows how to serialize many objects on it's own and I do not care about the structure of the XML outcome, it would be fine if i could get something like this:

<Person>
  <name>Jon Doe</name>
</Person>

So is it possible in a custom Converter-implementation to do this serialization and deserialization?

2 Answers 2

0

In a converter you are free to do conversion in any way you like. So basically you can decide in your custom converter how you want to (de-)serialize the object. However, AFAIK you cannot switch between single-value-conversion/normal conversion. But your example XML implies this.

If you just want to delegate to another converter like XStream's ReflectionConverter, you could do this with the convertAnother(Object, Converter)-method (see also this question/answer). For this, you have to implement the Converter interface instead of SingleValueConverter, to get access to the (un-)marshalling context.

Does this answer your question?

hth,
- martin

1
  • The problem with this approach is, that I have to tell XStream exactly which converter to use for further serialization. But I don't want to do so, since I do not want to influence the well defined and fine-tuned serialization-process. Basically in my custom converter all I want to do is to ask XStream to serialize my entity without using my custom converter (but everything else as configured).
    – MrD
    Jun 23, 2014 at 6:50
0

Underscore-java can read and write xml files. I am the maintainer of the project.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.