使用xStream
反序列化隐式集合时遇到一些麻烦我有两个班级:
一:
@XStreamAlias("instances")
public class ServerInstances {
public ServerInstances(){
instances = new ArrayList<>();
}
@XStreamImplicit(itemFieldName = "instance")
private List<ServerInstance> instances;
...add, get, size, etc.
}
二:
@XStreamAlias("instance")
public class ServerInstance {
@XStreamAlias("version")
private String version;
@XStreamAlias("environment")
private String environment;
@XStreamAlias("user")
private String user;
@XStreamAlias("timestamp")
private DateTime timeStamp;
getter, setter, etc.
}
当我序列化ServerInstances类
时 xStream = new XStream2();
xStream.registerConverter(new DateTimeConverter());
xStream.processAnnotations(new Class[] {ServerInstances.class, ServerInstance.class});
xStream.toXMLUTF8(object, outputStream);
我得到了很好的xml:
<?xml version="1.0" encoding="UTF-8"?>
<instances plugin="jenkins-deployment-dashboard@0.0.1">
<instance>
<version>1</version>
<environment>i1</environment>
<user>u1</user>
<timestamp>2014-10-10T11:42:30.957+02:00</timestamp>
</instance>
<instance>
<version>2</version>
<environment>i2</environment>
<user>u2</user>
<timestamp>2014-10-10T11:42:30.991+02:00</timestamp>
</instance>
</instances>
但是当我尝试反序列化时
xStream = new XStream2();
xStream.registerConverter(new DateTimeConverter());
xStream.processAnnotations(new Class[] {ServerInstances.class, ServerInstance.class});
ServerInstances instances = (ServerInstances)xStream.fromXml(inputStream);
我得到一个ServerInstances的实例,其中List为null。 我还试图通过使用xStream.alias(...)和xStream.addImplicitCollection(...)来声明别名,这会产生相同的结果。我不知道该怎么做了。