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?