问题描述
我想知道是否可以在 XStream 中自动添加包装元素而无需创建虚拟对象
例如
class Patient {
private HumanPatient humanPatient;
//getters setters
}
class HumanPatient {
private String firstName;
private String lastName;
}
这个需要序列化成xml为
<Patient>
<HumanPatient>
<FirstName></FirstName>
</HumanPatient>
</Patient>
理想情况下,我只需要 Patient
并跳过 HumanPatient
class Patient {
private String firstName;
private String lastName;
//getters setters
}
除了 HumanPatient 之外,我们不需要关心 Patient 的其他子元素,如果我不需要,我不想创建层次结构,所以理想情况下,我希望 xstream 将 Patient 序列化并反序列化为如果它不在那里。
我已经创建了一个 CustomConverter,我可以做到
public class HumanPatientConverter implements Converter {
@Override
public void marshal(Object source,HierarchicalStreamWriter writer,MarshallingContext context) {
writer.startNode("HumanPatient");
writer.endNode();
}
@Override
public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
return null;
}
@Override
public boolean canConvert(Class type) {
return Patient.class.isAssignableFrom(type);
}
}
但是,然后我必须在其下控制一堆序列化,它在 Patient
下有很多嵌套的东西,我不想手动控制,我只是希望它用 HumanPatient 将 Patient 下的所有内容包装起来
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)