简体   繁体   English

Jackson 序列化 - 动态更改元素名称

[英]Jackson serialization - dynamically change element name

I have the following XML that I have to convert to JSON:我有以下 XML 必须转换为 JSON:

<acme>
    <acme_name>1</acme_name>
    <acme_type>2</acme_type>
    <desc>desc0</desc>
</acme>

I'd want the following JSON:我想要以下 JSON:

{
   "acme":{
      "name":"1",
      "type":"2",
      "desc":"desc0"
   }
}

So what I want is所以我想要的是

  • if the element name contains an underscore then use the text as element name after the underscore如果元素名称包含下划线,则使用文本作为下划线后的元素名称
  • leave the other element name untouched保持其他元素名称不变

There are Java beans behind the XML with @XmlElement annotations.在 XML 后面有带有@XmlElement注释的 Java bean。
I cannot use jackson annotations in the Java beans as the classes come from a 3rd party source.我不能在 Java bean 中使用 jackson 注释,因为这些类来自第 3 方来源。

I thought I can use the JsonSerializer like我以为我可以像使用JsonSerializer

class GeneralSerializer extends JsonSerializer {

    @Override
    public void serialize(Object value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        ...
    }
}

but I have to specify what Java bean I want to serialize (see below) and I cannot use the same serializer for all the elements.但我必须指定我想要序列化的 Java bean(见下文),并且我不能对所有元素使用相同的序列化程序。

public class ItemSerializer extends JsonSerializer<Item>

The problem is that there are lots of different elements in the XML and specifying serializers them which do the same (ie searching for the underscore characters and just using the text after it) seems an overkill.问题是 XML 中有很多不同的元素,并指定它们执行相同操作的序列化程序(即搜索下划线字符并仅使用其后的文本)似乎有点矫枉过正。

Can I specify a common serializer that can be applied to every elements?我可以指定一个可以应用于每个元素的通用序列化程序吗?
Is there any other solution by any chance?有没有其他解决方案?

Thank you for the help!感谢您的帮助!

----------------- UPDATE 1 ----------------- ----------------- 更新 1 -----------------

I'd like to emphasize that I don't have the XML text but I have the Java beans (annotated with @Xml...) that represent the XML.我想强调一下,我没有 XML 文本,但我有代表 Z3501BB093D363810B671059B9CFED3FZ8 的 Java bean(用 @Xml 注释...)

----------------- UPDATE 2 ----------------- ----------------- 更新 2 -----------------

I don't insist on Jackson.我不坚持Jackson。 If there is a solution in another JSON library (eg GSON) then please don't spare me!如果另一个 JSON 库(例如 GSON)中有解决方案,请不要吝啬我!

some ideas:一些想法:

  1. serialize your beans to xml, then apply xslt transformations to rename the tags/change the structure to be more closer to your desired json将您的 bean 序列化为 xml,然后应用 xslt 转换以重命名标签/更改结构以更接近您想要的 json
  2. create pojo with jackson annotations and deserialize using jackson xml module使用 jackson 注释创建 pojo 并使用 jackson xml 模块反序列化
  3. serialize from pojo to json从 pojo 序列化到 json

or或者

  1. create pojo with jackson annotations according to your target json根据您的目标 json 使用 jackson 注释创建 pojo
  2. use a bean mapper like dozer to map bean to pojo使用像推土机这样的 bean 映射器到 map bean 到 pojo
  3. serialize from pojo to json从 pojo 序列化到 json

http://dozer.sourceforge.net/ http://dozer.sourceforge.net/

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

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