现在真正的问题…
// TODO: What annotations to put here?private List<BarEntity> barEntities;
我的回答: 没有!或至少,没关系!
像
type这里这样的属性只是字符串,不能做出任何决定。但是还有另一种好方法:
- 实施要
RootNode
为其做出决定的转换器 - 使用a
Serializer
做反序列化每个实体的实际工作。
我对您的课程进行了一些修改,但没有任何改变。的
toString()-方法仅用于测试-实现,因为你需要它。
类 FooEntity
@Root(name = "Entity")public class FooEntity{ @Attribute(name = "type") private String type; @Element(name = "Price") private int price; @Override public String toString() { return "FooEntity{" + "price=" + price + '}'; }}类 BarEntity
@Root(name = "Entity")public class BarEntity{ @Attribute(name = "type") private String type; @Element(name = "URL") private String url; @Override public String toString() { return "BarEntity{" + "url=" + url + '}'; } }类 RootNode
@Root(name = "RootNode")@Convert(RootNodeConverter.class) // <--- important!class RootNode{ private List<FooEntity> fooEntities; private List<BarEntity> barEntities; public RootNode() { // This has to be done somewhere ... this.fooEntities = new ArrayList<>(); this.barEntities = new ArrayList<>(); } public List<FooEntity> getFooEntities() { return fooEntities; } public List<BarEntity> getBarEntities() { return barEntities; } @Override public String toString() { return "RootNode{" + "fooEntities=" + fooEntities + ", barEntities=" + barEntities + '}'; }}最后是-
Converter实现:
类 RootNodeConverter
public class RootNodeConverter implements Converter<RootNode>{ @Override public RootNode read(InputNode node) throws Exception { RootNode root = new RootNode(); final InputNode entities = node.getNext("Entities"); InputNode child; while( ( child = entities.getNext() ) != null ) { if( child.getName().equals("Entity") == false ) { continue; // Not an Entity } final Serializer ser = new Persister(); switch(child.getAttribute("type").getValue()) { case "foo": root.getFooEntities().add(ser.read(FooEntity.class, child)); break; case "bar": root.getBarEntities().add(ser.read(BarEntity.class, child)); break; default: // Not a Foo nor a Bar - what now!? break; } } return root; } @Override public void write(OutputNode node, RootNode value) throws Exception { throw new UnsupportedOperationException("Not implemented yet!"); }}有一些东西需要优化,例如。
root.addBar(ser.read(BarEntity.class, child))通常添加或错误处理。
顺便说一句。而不是 2个 列表,您可以维护一个 单一的 一个(如果相关)。只需为实体创建超类。您也可以将
type-attribute移到那里。
用法
这是使用示例:
final String input = "<RootNode>n" + " <Entities>n" + " <Entity type="foo">n" + " <Price>1</Price>n" + " </Entity>n" + "n" + " <Entity type="bar">n" + " <URL>www.google.co.uk</URL>n" + " </Entity>n" + "n" + " <Entity type="foo">n" + " <Price>77</Price>n" + " </Entity>n" + " </Entities>n" + "</RootNode>";final Serializer ser = new Persister(new AnnotationStrategy()); // <-- Note the strategy!RootNode root = ser.read(RootNode.class, input);System.out.println(root);
这里也没有什么特别壮观的…
输出:
RootNode{fooEntities=[FooEntity{price=1}, FooEntity{price=77}], barEntities=[BarEntity{url=www.google.co.uk}]}


