[英]XStream java.util.Map field marshalling
I would like to use a custom converter for an object in xstream, but default to the default behavior for some fields. 我想为xstream中的对象使用自定义转换器,但某些字段的默认行为是默认行为。
unfortunately, when writing a field like map in TestObject, xstream does not write the surrounding <map>...</map>
tags. 不幸的是,在TestObject中编写诸如map之类的字段时,xstream不会编写周围的
<map>...</map>
标记。
public static class TestObject {
public Map map;
public Object custom;
}
protected static XStream stream = new XStream();
protected static Converter TestObjectConverter = new Converter() {
@Override
public boolean canConvert(Class type) {
return type == TestObject.class;
}
@Override
public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
return null;
}
@Override
public void marshal(Object source, HierarchicalStreamWriter writer, MarshallingContext context) {
TestObject obj = (TestObject)source;
context.convertAnother(obj.map);
}
};
@Override
protected void runApp() throws Exception {
stream.registerConverter(TestObjectConverter);
TestObject obj = new TestObject();
obj.map = new HashMap();
obj.map.put(1, "asdf");
obj.map.put(2, "qwerty");
String xml = stream.toXML(obj);
return;
};
essentially i am looking to replicate the default behavior for some fields in TestObject, but using custom converters for others. 本质上,我希望在TestObject中复制某些字段的默认行为,但对其他字段使用自定义转换器。 What would be the best way of doing this?
最好的方法是什么?
若要获取默认行为,请确保您的UnmarshallingContext
是AbstractReferenceMarshaller
,然后调用context.start(object, context)
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.