xstream - 在自定义转换器中重用默认转换器
我正在使用 xstream 处理 xml 字符串,但对象的某些字段在版本之间发生了变化,所以我正在实现 自定义转换器。下面列出了字段更改的摘要,只有前两个字段类型不同。
Field type1 type2
a short String
b String Object
c List List
d Object Object
.
.
.
x String String
我当前的转换器是为了专门处理每个字段而实现的,这导致 unmarshal() 方法中存在大量“else if”条件
package a.b.c.reports;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
public class MyConverter implements Converter {
..
@Override
public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
while (reader.hasMoreChildren()) {
reader.moveDown();
if(reader.getNodeName().equals("a"))
{
a = reader.getValue();
}
else if (reader.getNodeName().equals("b"))
{
b = (Object) context.convertAnother(reader, Object.class);
}
else if(reader.getNodeName().equals("c"))
{
a = reader.getValue();
}
..
..
}
}
是否有更智能的方法来委托处理类型未更改为默认值的字段xstream转换器?
I'm using xstream to process an xml string but some fields of the object have changed between versions, so i'm implementing
a custom converter. A summary of the field changes is listed below, and only the first two field types are different.
Field type1 type2
a short String
b String Object
c List List
d Object Object
.
.
.
x String String
My current converter is implemented to handle each of the fields specifically, which leads to a large number of 'else if' conditions within the unmarshal() method
package a.b.c.reports;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
public class MyConverter implements Converter {
..
@Override
public Object unmarshal(HierarchicalStreamReader reader,UnmarshallingContext context) {
while (reader.hasMoreChildren()) {
reader.moveDown();
if(reader.getNodeName().equals("a"))
{
a = reader.getValue();
}
else if (reader.getNodeName().equals("b"))
{
b = (Object) context.convertAnother(reader, Object.class);
}
else if(reader.getNodeName().equals("c"))
{
a = reader.getValue();
}
..
..
}
}
Is there a smarter way to delegate the processing of fields who's types have not changed to the default xstream converter?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题有点陈旧,但我还是花了一些时间来收集这些信息。
简单的解决方案是扩展
ReflectionConverter
而不是实现原始Converter
接口。ReflectionConverter
是 XStream 中的默认转换器,因此请覆盖所需的内容并super
其他所有内容。然后new XStream().register
你的新转换器就可以了。The question is a bit stale, but nevertheless took me some time to gather the bits.
Simple solution to that is to extend the
ReflectionConverter
instead of implementing the rawConverter
interface.ReflectionConverter
is the default converter in the XStream so override what's needed andsuper
everything else. Thennew XStream().register
your new converter and you're good.