I use simpleframework (http://simple.sourceforge.net/) in the project for my serialization / deserialization tasks, but it does not work as expected (well, at least not the way I expect) when working with an empty / null String values.
If I serialize an object with an empty String value, it will be displayed as an empty xml element.
So this is MyObject object = new MyObject();
object.setAttribute(""); // attribute is String MyObject object = new MyObject();
object.setAttribute(""); // attribute is String
will serialize as <object>
<attribute></attribute>
</object> <object>
<attribute></attribute>
</object>
But deserializing this empty attribute will be zero, not an empty String.
Am I completely mistaken in thinking that this should be an empty string instead of zero? And how can I make it work like I canβt?
Oh, and if I serialize an object with a null attribute, it will eventually show <object/> as you might expect.
Edit:
Added a simple test script in which I am doing the current work
@Test public void testDeserialization() throws Exception { StringWriter writer = new StringWriter(); MyDTO dto = new MyDTO(); dto.setAttribute(""); Serializer serializer = new Persister(); serializer.write(dto, writer); System.out.println(writer.getBuffer().toString()); MyDTO read = serializer.read(MyDTO.class, writer.getBuffer().toString(),true); assertNotNull(read.getAttribute()); } @Root public class MyDTO { @Element(required = false) private String attribute; public String getAttribute() { return attribute; } public void setAttribute(String attribute) { this.attribute = attribute; } }
Edit, fixed:
For some reason, the value of InputNode is null when an empty string is passed to it. I solved the problem by creating my own converter for the string.
new Converter<String>() { @Override public String read(InputNode node) throws Exception { if(node.getValue() == null) { return ""; } return node.getValue(); } @Override public void write(OutputNode node, String value) throws Exception { node.setValue(value); } });