[Simple-support] carriage return
Brought to you by:
niallg
|
From: Hans <fis...@ya...> - 2012-05-03 06:26:22
|
Hi,
The following example serializes a simple class
with one String property. This property contains
a carriage return ("\r").
After serializing the class to a String the carriage
return is preserved in the String.
After deserializing the String back to the class,
the carriage return is replaced by a line feed!
This behaviour surprised me, but after googling
around, this seems to be the normal behaviour.
So my question is what is best practice to
preserve the carriage return?
Setting the data attribute inside the annotation
of the element is not enough, the carriage
return is still replaced by a line feed.
package carriage_return;
import java.io.StringWriter;
import org.simpleframework.xml.Element;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;
public class CarriageReturn {
@Root
public static class Xml {
@Element
private String content;
public Xml() {
}
public Xml(String content) {
this.content = content;
}
public String getContent() {
return content;
}
}
public static void main(String[] args) throws Exception {
Xml xml = new Xml("Hello\rSimpleXML");
Serializer serializer = new Persister();
StringWriter stringWriter = new StringWriter();
serializer.write(xml,stringWriter);
String serialzedXml = stringWriter.toString();
System.out.println(serialzedXml.contains("\r"));
xml = serializer.read(Xml.class,serialzedXml);
System.out.println(xml.getContent().contains("\r"));
}
}
|