Simpleframework. Can nulls be retained in collections?

狂风中的少年 提交于 2019-12-02 05:33:09

问题


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 + "]";
        }
    }
}

回答1:


First, your list annotation is missing the the entries name:

@ElementList(inline = true, required = false, entry = "object")
private List<Object> params;

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.

public class SimpleframeworkTest
{
    // ...

    @Root(name = "container", strict = false)
    @Convert(NullawareContainerConverter.class)
    public static class Container
    {
        static final Serializer ser = new Persister(new AnnotationStrategy());

        // ...

        public String toXml() throws Exception
        {
            StringWriter sw = new StringWriter();
            ser.write(this, sw);
            return sw.toString();
        }

        public static Container toObject(String xml) throws Exception
        {
            return ser.read(Container.class, xml);
        }

        // ...
    }


    static class NullawareContainerConverter implements Converter<Container>
    {
        final Serializer ser = new Persister();

        @Override
        public Container read(InputNode node) throws Exception
        {
            final Container c = new Container();
            c.id = Integer.valueOf(node.getAttribute("id").getValue());
            c.params = new ArrayList<>();
            InputNode n;

            while( ( n = node.getNext("object")) != null )
            {
                /* 
                 * If the value is null it's added too. You also can add some
                 * kind of null-replacement element here too.
                 */
                c.params.add(n.getValue());
            }

            return c;
        }

        @Override
        public void write(OutputNode node, Container value) throws Exception
        {
            ser.write(value.id, node);

            for( Object obj : value.params )
            {
                if( obj == null )
                {
                    obj = ""; // Set a valid value if null
                }
                // Possible you have to tweak this by hand
                ser.write(obj, node);
            }
        }

    }

}

As written in the comments, you have to do some further work.

Results:

testNullsInParams()

<container>
   <integer>4000</integer>
   <string>foo</string>
   <string></string>
   <string>bar</string>
</container>

testDeserializeNull()

Container [id=4000, params=[foo, null, bar]]


来源:https://stackoverflow.com/questions/26386175/simpleframework-can-nulls-be-retained-in-collections

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!