Simpleframework, deserializing an empty element to an empty string instead of a null

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); } }); 
+7
source share
3 answers

Answer to completeness

Annotate your element using transform annotation and give it a converter class as the @Convert(SimpleXMLStringConverter.class) parameter @Convert(SimpleXMLStringConverter.class)

Create a converter class that converts a string from zero to an empty string

 public class SimpleXMLStringConverter implements Converter<String> { @Override public String read(InputNode node) throws Exception { String value = node.getValue(); if(value == null) { value = ""; } return value; } @Override public void write(OutputNode node, String value) throws Exception { node.setValue(value); } } 

And do not add new AnnotationStrategy() to your persister.

+10
source

Use attribute annotation. It has a property called empty, used to provide a default value.

See Simple Javadocs Framework.

+1
source

Am I completely mistaken in thinking that this should be an empty string instead of a null?

Not as far as I know ... Usually this indicates some serialization problem along the way, it should return an Object and all its instance variable without interruption with the values ​​set in Serialization.

By the way, you have not published all your code, it is also possible that the order in which Serialization is started means that it skips String data, this can be a problem.

-one
source

All Articles