[英]simpleframework, deserializing an empty element to an empty string instead of 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.我在一个项目中使用 simpleframework ( http://simple.sourceforge.net/ ) 来满足我的序列化/反序列化需求,但是在处理空/空字符串值时它没有按预期工作(好吧,至少不是我期望的那样) .
If I serialize an object with an empty String value, it will show as an empty xml element.如果我使用空字符串值序列化一个对象,它将显示为一个空的 xml 元素。
So this:所以这:
MyObject object = new MyObject();
object.setAttribute(""); // attribute is String
would serialize as:将序列化为:
<object>
<attribute></attribute>
</object>
But deserializing that empty attribute will end up as null, instead of an empty String.但是反序列化该空属性最终会为空,而不是空字符串。
Am I completely bonkers for thinking that it should be an empty String instead of null?我是不是完全疯了,认为它应该是一个空字符串而不是 null? And how on earth can I make it to work in the way I wan't?我到底如何才能让它以我不想要的方式工作?
Oh, and if I serialize the object with a null attribute it will end up showing <object/>
as one might expect.哦,如果我用 null 属性序列化对象,它最终会像人们预期的那样显示<object/>
。
Edit:编辑:
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;
}
}
Edit, fixed:编辑,修复:
For some reason the InputNode
value is null when an empty string is passed to it.出于某种原因,当向InputNode
传递空字符串时,它的值为 null。 I resolved the problem by creating a custom Converter for String
.我通过为String
创建自定义 Converter 解决了这个问题。
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);
}
});
Answering for completeness回答完整性
Annotate your element with the convert annotation and give it a converter class as a parameter @Convert(SimpleXMLStringConverter.class)
使用 convert 注释来注释你的元素,并给它一个转换器类作为参数@Convert(SimpleXMLStringConverter.class)
Create the converter class that does string conversion from null to empty string创建将字符串从 null 转换为空字符串的转换器类
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 don't for get to add new AnnotationStrategy()
to your persister.并且不要将new AnnotationStrategy()
添加到您的持久器中。
Use the Attribute annotation.使用属性注释。 It has a property named empty used to provide a default value.它有一个名为 empty 的属性,用于提供默认值。
Am I completely bonkers for thinking that it should be an empty String instead of null?我是不是完全疯了,认为它应该是一个空字符串而不是 null?
Not as far as I know... Normally this indicates some problem with the Serialization along the way, it's supposed to return the Object and all it's non transient instance variable with the values set on Serialization.据我所知......通常这表明序列化过程中存在一些问题,它应该返回对象及其所有非瞬态实例变量,其值设置在序列化上。
BTW you didn't post all your code, it's also possible that the order in which the Serialization starts means it skips the String data this can sometime be a problem.顺便说一句,您没有发布所有代码,也可能是序列化开始的顺序意味着它跳过了字符串数据,这有时可能是一个问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.