I have a object -> XML -> object process in one project I have to support. The object is containing List and if it gets serialized, all the null values which where present in list are omitted. My question is, can it be done with Simpleframework or should I use something else? What? Here is what I do:
import java.io.StringWriter;
import java.util.Arrays;
import java.util.List;
import org.simpleframework.xml.Attribute;
import org.simpleframework.xml.ElementList;
import org.simpleframework.xml.Root;
import org.simpleframework.xml.core.Persister;
import org.testng.annotations.Test;
public class SimpleframeworkTest {
@Test
public void testNullsInParams() throws Exception {
Container container = new Container();
container.setId(4000);
container.setParams(Arrays.asList(new Object[] { "foo", null, "bar" }));
String xml = container.toXml(); // omits null value in output
}
@Test
public void testDeserializeNull() throws Exception {
String xml = "<container id=\"4000\">"+
" <object class=\"java.lang.String\">foo</object>"+
// " <object class=\"java.lang.String\"></object>"+ // gets NullPointerException here
" <object class=\"java.lang.String\">bar</object>"+
"</container>";
Container object = Container.toObject(xml);
}
@Root(name = "container", strict = false)
public static class Container {
@Attribute
private Integer id;
@ElementList(inline = true, required = false)
private List<Object> params;
public String toXml() throws Exception {
StringWriter sw = new StringWriter();
new Persister().write(this, sw);
return sw.toString();
}
public static Container toObject(String xml) throws Exception {
return new Persister().read(Container.class, xml);
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public List<Object> getParams() {
return params;
}
public void setParams(List<Object> params) {
this.params = params;
}
@Override
public String toString() {
return "Container [id=" + id + ", params=" + params + "]";
}
}
}
First, your list annotation is missing the the entries name:
Otherwise
<string>...</string>
is used, not<object>...</object>
.You can prevent that nullpointer excpetion by adding
type = String.class
to your list's annotation. However, this doesn't fix the main problem.In general empty tags /
null
-elements will not be added to the result.Here's an example how to solve this problem with a
Converter
.As written in the comments, you have to do some further work.
Results:
testNullsInParams()
testDeserializeNull()