本文介绍了simpleframework,将空元素反序列化为空字符串而不是null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在项目中使用simpleframework(http://simple.sourceforge.net/)来进行序列化/反序列化需求,但是在处理空/时它没有按预期工作(好吧,至少不是我期望的) null字符串值。

I use simpleframework (http://simple.sourceforge.net/) in a project for my serializing / deserializing needs, but it doesn't work as expected (well, atleast not how I expect) when dealing with empty / null String values.

如果我使用空字符串值序列化对象,它将显示为空的xml元素。

If I serialize an object with an empty String value, it will show as an empty xml element.

所以这个

MyObject object = new MyObject();

object.setAttribute(); // attribute is String

将序列化为

< object>

< attribute>< / attribute>

< / object>

但反序列化empty属性将以null结尾,而不是空字符串。

But deserializing that empty attribute will end up as null, instead of an empty String.

我认为它应该是一个空字符串而不是null,我完全疯了吗?我怎么能以我想要的方式使它工作?

Am I completely bonkers for thinking that it should be an empty String instead of null? And how on earth can I make it to work in the way I wan't?

哦,如果我使用null属性序列化对象,它将最终显示
< object />
正如人们所料。

Oh, and if I serialize the object with a null attribute it will end up showing <object/>as one might expect.

编辑:

添加了一个我目前正在运行的简单测试用程序

Added a simple testcase I'm currenty running

@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;
    }
}

编辑,修正:

由于某种原因,当一个空字符串传递给它时,InputNode值为null。我通过创建自定义Converter for String解决了这个问题。

For some reason the InputNode value is null when an empty string is passed to it. I resolved the problem by creating a custom Converter for 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);
    }

});


推荐答案

回答完整性

使用转换注释注释元素,并将转换器类作为参数
@Convert(SimpleXMLStringConverter.class)

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

创建转换器类,将字符串从null转换为空字符串

Create the converter class that does string conversion from null to 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);
    }

}

不要去将 new AnnotationStrategy()添加到您的信息中。

And don't for get to add new AnnotationStrategy() to your persister.

这篇关于simpleframework,将空元素反序列化为空字符串而不是null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 11:21