Search code examples
javapersistencexstream

Replacement for ToAttributedValueConverter in XStream Object


I'am using the ToAttributedValueConverter via class annotation:

@XStreamConverter(value = ToAttributedValueConverter.class, strings = { "text" })
public final class Message {
    private final String text;

    // Needed for XStream serialization
    @SuppressWarnings("unused")
    private Message() {
        text = null;
    }

    public Message(final String text) {
        if (text == null) {
            throw new NullPointerException();
        }
        this.text = text;
    }

    public String getText() {
        return this.text;
    }
}

Example:

final XStream xstream = new XStream();
xstream.alias("message", Message.class);
xstream.processAnnotations(Message.class);

final Message message = new Message("Lorem Ipsum");
final String xml = xstream.toXML(message);
System.out.println(xml);

The output is:

<message>Lorem Ipsum</message>

In order to separate data model (class Message) from the persistence (XStream) I would remove all XStream annotations from the data model.

For example XStreamAlias("message") can simply replaced with xstream.alias("message", Message.class). But whats the replacement for ToAttributedValueConverter in a xstream object?


Solution

  • Sometimes the answer is quite simple. The short answer is: use xstream.registerConverter(converter)

    The long answer is: ToAttributeValueConverter is a converter. That is, you can register that one with the XStream instance to influence the serialization.

    The following code will do what you want in your question without using a XStream annotation (I used your Message class for that sample). All relevant code is in the main method:

    import com.thoughtworks.xstream.XStream;
    import com.thoughtworks.xstream.converters.Converter;
    import com.thoughtworks.xstream.converters.extended.ToAttributedValueConverter;
    
    public class XmlSample {
    
        public static void main(String[] args) {
            XStream xStream = new XStream();
            Converter converter = new ToAttributedValueConverter(Message.class,
                    xStream.getMapper(), xStream.getReflectionProvider(), xStream.getConverterLookup(), "text");
            xStream.registerConverter(converter);
            xStream.alias("message", Message.class);
            System.out.println(xStream.toXML(new Message("testtest")));
        }
    
        public static final class Message {
            private final String text;
    
            // Needed for XStream serialization
            @SuppressWarnings("unused")
            private Message() {
                text = null;
            }
    
            public Message(final String text) {
                if (text == null) {
                    throw new NullPointerException();
                }
                this.text = text;
            }
    
            public String getText() {
                return this.text;
            }
        }
    }