Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/394.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用xstream定制转换器_Java_Converter_Xstream - Fatal编程技术网

Java 使用xstream定制转换器

Java 使用xstream定制转换器,java,converter,xstream,Java,Converter,Xstream,我的类作用域需要一个自定义转换器: class Scope { private final String name; private final SomeProp prop; private final Item[] items; //... } 我为某个道具注册了转换器。但是我想为Item(和所有子类)使用默认的转换器 我怎么能做到 我试图覆盖封送/解封: public void marshal(Object val, HierarchicalStreamWr

我的类作用域需要一个自定义转换器:

class Scope {
    private final String name;
    private final SomeProp prop;
    private final Item[] items;
    //...
}
我为某个道具注册了转换器。但是我想为Item(和所有子类)使用默认的转换器

我怎么能做到

我试图覆盖封送/解封:

public void marshal(Object val, HierarchicalStreamWriter writer, 
          MarshallingContext context) {
    //... saving name and prop
    writer.startNode("items");

    ArrayConverter conv = new ArrayConverter(mapper);
    assert(conv.canConvert(items.members.getClass()));
    conv.marshal(items.members, writer, context);

    writer.endNode);
}

public Object unmarshal(HierarchicalStreamReader reader,
    UnmarshallingContext context) {
    //... reading name and prop
    reader.moveDown();

    assert("items".equals(reader.getNodeName()));

    ArrayConverter conv = new ArrayConverter(mapper);
    Item[] items = (Item[])conv.unmarshal(reader, context);
    //...
}

但由于某种原因,解组器不起作用。

您的描述不太清楚,但我认为您正在尝试做远远超出需要的工作。如果你想为某个道具定制一个转换器,而为其他所有道具设置一个默认值,那么你所要做的就是

Scope scope = ...;
XStream xstream = new XStream();
xstream.registerConverter(new SomePropConverter());
String xml = xstream.toXML(scope);

如果我在你的问题中遗漏了什么,请澄清。

你的描述不是很清楚,但我认为你正在努力做的工作远远超出了需要。如果你想为某个道具定制一个转换器,而为其他所有道具设置一个默认值,那么你所要做的就是

Scope scope = ...;
XStream xstream = new XStream();
xstream.registerConverter(new SomePropConverter());
String xml = xstream.toXML(scope);

如果我在您的问题中遗漏了什么,请澄清。

我阅读了ArrayConverter和AbstractCollectionConverter的源代码,并重写了如下解组:

while(reader.hasMoreChildren()) {    
    reader.moveDown(); 
    Class type = HierarchicalStreams.readClassType(reader, mapper); 
    Object item = context.convertAnother(context.currentObject(), type);  
    itemList.add((Item)item); 
    reader.moveUp(); 
}
但在循环引用的情况下,我得到了一个错误。
因此,Ryan Stewart尝试了最好的方法。

我阅读了ArrayConverter和AbstractCollectionConverter的源代码,并重新编写了解组器,如下所示:

while(reader.hasMoreChildren()) {    
    reader.moveDown(); 
    Class type = HierarchicalStreams.readClassType(reader, mapper); 
    Object item = context.convertAnother(context.currentObject(), type);  
    itemList.add((Item)item); 
    reader.moveUp(); 
}
但在循环引用的情况下,我得到了一个错误。
因此,最好的办法是由Ryan Stewart提出。

可能是你在写。我简化了我的代码。简单的方法是将类范围划分为两个部分:AllCustomProps和items。我不确定我们是否相互理解。没有必要更改您的Scope类。XStream会正常工作。可能您正在编写。我简化了我的代码。简单的方法是将类范围划分为两个部分:AllCustomProps和items。我不确定我们是否相互理解。没有必要更改您的Scope类。XStream就行了。