简体   繁体   English

Jackson YAML 解析父对象和对象数组

[英]Jackson YAML parsing with parent objects & array of objects

I have a yaml file with four parent objects.我有一个包含四个父对象的 yaml 文件。 The fourth object is an array of element of which I will want to create a class for and populate.第四个对象是一个元素数组,我将要为其创建一个类并填充该元素。 Is there a way to use the object mapper in jackson to ignore the first three objects and then parse my list of "InspectionModules"?有没有办法在 jackson 中使用对象映射器来忽略前三个对象,然后解析我的“InspectionModules”列表?

My yaml file looks like this:我的 yaml 文件如下所示:

# Don't care about these elements
InspectionGroups:
InspecitonModes:
ThresholdTypes:

# This array is of interest
InspectionModules:
 - feature : FC_Trig1
   name: "first inspection"
   Channels:
   - id: ICI_01
     category: Dia_MC_Config
 - feature : FC_Trig2
   name: "Diagonal Missing Cap"
   Channels:
   - id: ICI_02
     category: Dia_MC_Config

Basically I want to create a class called InspectionModule and have the mapper map the elements of the InspectionModules array into this class.基本上我想创建一个名为InspectionModule 的类,并让映射器将InspectionModules 数组的元素映射到这个类中。 Is there a simple way to do this in jackson?杰克逊有没有一种简单的方法可以做到这一点? If not would it be recommended to reorganize our YAML file so we can leverage the object mapper?如果不是,是否建议重新组织我们的 YAML 文件,以便我们可以利用对象映射器?

I assume you already have some familiarity with Jackson's ObjectMapper .我假设您已经对 Jackson 的ObjectMapper有所了解。

First you will need a Java class representing the entire contents of your YAML file.首先,您需要一个代表 YAML 文件全部内容的 Java 类。 Let's call it Root :我们称之为Root

public class Root {

    @JsonProperty("InspectionModules")
    private List<InspectionModule> inspectionModules;

    // getters and setters omitted here for brevity
}

Notice that you'll need to use @JsonProperty to tell Jackson that the YAML name InspectionModules corresponds to your Java property inspectionModules in spite of their different spellings.请注意,您需要使用@JsonProperty来告诉 Jackson YAML 名称InspectionModules对应于您的 Java 属性inspectionModules ,尽管它们的拼写不同。

Next you need a Java class representing one of the YAML sections below the InspectionModules: .接下来,您需要一个 Java 类来表示InspectionModules:下的 YAML 部分之一InspectionModules:

public class InspectionModule {

    private String feature;

    private String name;

    @JsonProperty("Channels")
    private List<Channel> channels;

    // getters and setters omitted here for brevity
}

And finally you need a Channel class representing one of the YAML sections below Channels: You should be able to write this Java class by yourself already.最后,您需要一个Channel类来表示Channels:下的 YAML 部分之一Channels:您应该已经能够自己编写这个 Java 类了。

Now you are ready to use Jackson's YAMLMapper for reading your YAML file into a Root object.现在您已准备好使用 Jackson 的YAMLMapper将您的 YAML 文件读入Root对象。

File file = new File("example.yaml");
ObjectMapper objectMapper = new YAMLMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Root root = objectMapper.readValue(file, Root.class);

Notice that you need to tell Jackson that it is OK to encounter unknown properties (like InspectionGroups ) during reading.请注意,您需要告诉 Jackson 在阅读过程中遇到未知属性(如InspectionGroups )是可以的。

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

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