Jackson
- 使用
Jackson
读取 -
JUnit5
框架验证
pom
<properties>
<jackson.version>2.13.1</jackson.version>
</properties>
<!--yaml文件解析-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.0</version>
</dependency>
解析List
文件
List
类型yaml
orderlist.yaml
- item: No. 9 Sprockets
quantity: 12
unitPrice: 1.23
orderDate: 2019-04-17
- item: No. Widget (10mm)
quantity: 10
unitPrice: 3.45
orderDate: 2022-01-16
List类型解析
- 直接声明类型{style=width:500px}
@Test
void listMapTest() throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
TypeReference<List<HashMap<String, Object>>> typeReference =
new TypeReference<List<HashMap<String, Object>>>() {};
List<HashMap<String, Object>> hashMaps = mapper.readValue(
new File("src/test/resources/yaml/orderlist.yaml"), typeReference);
System.out.println(hashMaps);
hashMaps.forEach(hashMap -> {
assertAll(
() -> assertThat(hashMap.get("item").toString(), startsWith("No")),
() -> assertThat(Integer.parseInt(hashMap.get("quantity").toString()),
is(greaterThan(9))),
() -> assertThat(new BigDecimal(hashMap.get("unitPrice").toString()),
is(closeTo(new BigDecimal(1.0),new BigDecimal(4.00))))
);
});
}
实体类对应解析
实体类
成员变量与yaml的key不一致
public class OrderList {
@JsonProperty("item")
private String otherItem;
@JsonProperty("quantity")
private int qua;
@JsonProperty("unitPrice")
private BigDecimal price;
@JsonProperty("orderDate")
private LocalDate date;
// Constructors, Getters, Setters and toString
}
测试方法
对应的实体类解析
@Test
public void orderListTest() throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
//功能上等价的便捷方法: mapper.registerModules(mapper.findModules());
//我们需要使用 findAndRegisterModules方法,以便 Jackson正确处理我们的日期
//Jackson也可以自动搜索所有模块,不需要我们手动注册
mapper.findAndRegisterModules();
TypeReference<List<OrderList>> typeReference = new TypeReference<List<OrderList>>() {
};
List<OrderList> orderLists = mapper.readValue(new File("src/test/resources/yaml/orderlist.yaml"), typeReference);
System.out.println(orderLists);
orderLists.forEach(orderList -> {
assertAll(
() -> assertThat(orderList.getOtherItem(), startsWith("No")),
() -> assertThat(orderList.getQua(), is(greaterThan(9))),
() -> assertThat(orderList.getPrice(), is(closeTo(new BigDecimal(1.0),new BigDecimal(4.00))))
);
});
}
成员变量与yaml的key一致
public class OrderLine{
private String item;
private int quantity;
private BigDecimal unitPrice;
private LocalDate orderDate;
// Constructors, Getters, Setters and toString
}
测试方法
对应的实体类解析
@Test
public void orderLineTest() throws IOException {
ObjectMapper mapper = new ObjectMapper(new YAMLFactory());
mapper.findAndRegisterModules();
TypeReference<List<OrderLine>> typeReference = new TypeReference<List<OrderLine>>() {
};
List<OrderLine> orderLines = mapper.readValue(new File("src/test/resources/yaml/orderlist.yaml"), typeReference);
System.out.println(orderLines);
}