gpt4 book ai didi

java - 使用简单框架时不调用转换器

转载 作者:行者123 更新时间:2023-11-30 08:17:05 28 4
gpt4 key购买 nike

这是一个简单的类

@Root(strict = false)
public class Foo{
@ElementList(entry = "Test", required = false, inline=true)
@Convert(TestConverter.class)
public List<MyClass> test = new ArrayList<MyClass>();

public static class TestConverter implements Converter<List<MyClass>>
{
public List<MyClass> read(InputNode node) {

return new ArrayList<MyClass>();
}

public void write(OutputNode node, List<MyClass> myclass) {
node.setAttribute("debug", "debug");
}
}
}

永远不会调用转换器。任何人都可以阐明这一点吗?

最佳答案

The converter is never called. Could anyone shed some light on this?

这里的原因:序列化器不知道 @Convert。您必须指定一个策略,告知要使用一个转换器。

使用其中之一:

使用AnnotationStrategy:

只需替换...

Serializer ser = new Persister();

Serializer ser = new Persister(new AnnotationStrategy());
// |----------------------|

当然每个here都有一个例子.


一些其他问题:

@ElementList(entry = "Test", required = false, inline=true)
@Convert(TestConverter.class)

这可能会失败:

Note that for the above field the Element annotation is required. If this is used with any other XML annotation such as the ElementList or Text annotation then an exception will be thrown.

http://simple.sourceforge.net/download/stream/doc/javadoc/org/simpleframework/xml/convert/Convert.html

只是改用@Element

使用@Override

@Override /* Better use this */
public List<MyClass> read(InputNode node) {
// ...
}

@Override /* Better use this */
public void write(OutputNode node, List<MyClass> myclass) {
// ...
}

关于java - 使用简单框架时不调用转换器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28060873/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com