简体   繁体   English

Jackson——替代 SimpleXML 的 @ElementListUnion

[英]Jackson-alternative to SimpleXML's @ElementListUnion

Serializing a list that may contain elements with different types in Java using SimpleXML, you can you something such as使用 SimpleXML 序列化可能包含 Java 中不同类型元素的列表,您可以使用诸如

class Root {
  @ElementListUnion({
    @ElementList(entry = "Foo", type = Foo.class, inline = true),
    @ElementList(entry = "Bar", type = Bar.class, inline = true)
  })
  protected List<Object> fooOrBar;
}

This will end up being serialized to这最终将被序列化为

<Root>
  <Foo>
    {correct Foo serialization}
  </Foo>
  <Bar>
    {correct Bar serialization}
  </Bar>
</Root>

Is there any Jackson-alterantive to @ElementListUnion , or a workaround? @ElementListUnion是否有 Jackson-alterantive 或解决方法? I could create a custom serializer - how would I go about creating a custom serializer that will only alter the name based on type?我可以创建一个自定义序列化程序 - 我 go 如何创建一个只会根据类型更改名称的自定义序列化程序?

When using Jackson, I cannot seem to find a way that achieves the same thing.使用 Jackson 时,我似乎无法找到实现相同目的的方法。 The closest I can get with using @JacksonXmlElementWrapper and/or JacksonXmlProperty , is a result similar to使用@JacksonXmlElementWrapper和/或JacksonXmlProperty我能得到的最接近结果是类似于

<Root>
  <fooOrBar>
    {correct Foo serialization}
  </fooOrBar>
  <fooOrBar>
    {correct Bar serialization}
  </fooOrBar>
</Root>

There is no Jackson-alternative to @ElementListUnion, or workaround at the moment.目前没有@ElementListUnion 的 Jackson 替代方案或解决方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM