1. Introduction
1.绪论
In this short tutorial, we’re going to learn how to use Jackson to read and write YAML files.
在这个简短的教程中,我们将学习如何使用Jackson来读取和写入YAML文件。
After we go over our example structure, we’ll use the ObjectMapper to read a YAML file into a Java object and also write an object out to a file.
在我们看完我们的例子结构之后,我们将使用ObjectMapper来把YAML文件读成一个Java对象,同时把一个对象写到一个文件中。
2. Dependencies
2.依赖性
Let’s add the dependency for Jackson YAML data format:
让我们为杰克逊YAML数据格式添加依赖性。
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.0</version>
</dependency>
We can always find the most recent version of this dependency on Maven Central.
我们总能在Maven Central上找到该依赖的最新版本。
Our Java object uses a LocalDate, so let’s also add a dependency for the JSR-310 datatype:
我们的Java对象使用LocalDate,所以我们也要为JSR-310数据类型添加一个依赖。
<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>2.13.0</version>
</dependency>
Again, we can look up its most recent version on Maven Central.
同样,我们可以在Maven Central上查询其最新版本。
3. Data and Object Structure
3.数据和对象结构
With our dependencies squared away, we’ll now turn to our input file and the Java classes we’ll be using.
有了我们的依赖关系,我们现在将转向我们的输入文件和我们将要使用的Java类。
Let’s first look at the file we’ll be reading in:
让我们先看看我们要读进去的文件。
orderNo: A001
date: 2019-04-17
customerName: Customer, Joe
orderLines:
- item: No. 9 Sprockets
quantity: 12
unitPrice: 1.23
- item: Widget (10mm)
quantity: 4
unitPrice: 3.45
Then, let’s define the Order class:
然后,让我们定义Order类。
public class Order {
private String orderNo;
private LocalDate date;
private String customerName;
private List<OrderLine> orderLines;
// Constructors, Getters, Setters and toString
}
Finally, let’s create our OrderLine class:
最后,让我们创建我们的OrderLine类。
public class OrderLine {
private String item;
private int quantity;
private BigDecimal unitPrice;
// Constructors, Getters, Setters and toString
}
4. Reading YAML
4.读取YAML
We’re going to use Jackson’s ObjectMapper to read our YAML file into an Order object, so let’s set that up now:
我们将使用Jackson的ObjectMapper把我们的YAML文件读成一个Order对象,所以现在让我们来设置一下。
mapper = new ObjectMapper(new YAMLFactory());
We need to use the findAndRegisterModules method so that Jackson will handle our Date properly:
我们需要使用findAndRegisterModules方法,这样Jackson就能正确处理我们的Date。
mapper.findAndRegisterModules();
Once we have our ObjectMapper configured, we simply use readValue:
一旦我们配置了我们的ObjectMapper,我们只需使用readValue:。
Order order = mapper.readValue(new File("src/main/resources/orderInput.yaml"), Order.class);
We’ll find that our Order object is populated from the file, including the list of OrderLine.
我们会发现,我们的Order对象是从文件中填充的,包括OrderLine的列表。
5. Writing YAML
5.编写YAML
We’re also going to use ObjectMapper to write an Order out to a file. But first, let’s add some configuration to it:
我们还将使用ObjectMapper将一个Order写到文件中。但首先,让我们向它添加一些配置。
mapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
Adding that line tells Jackson to just write our date as a String instead of individual numeric parts.
添加这一行告诉Jackson把我们的日期写成String,而不是单个数字部分。
By default, our file will start with three dashes. That’s perfectly valid for the YAML format, but we can turn it off by disabling the feature on the YAMLFactory:
默认情况下,我们的文件将以三个破折号开始。这对YAML格式来说是完全有效的,但我们可以通过在YAMLFactory上禁用该功能来关闭它。
mapper = new ObjectMapper(new YAMLFactory().disable(Feature.WRITE_DOC_START_MARKER));
With that additional set up out of the way, let’s create an Order:
有了这些额外的设置,我们来创建一个Order。
List<OrderLine> lines = new ArrayList<>();
lines.add(new OrderLine("Copper Wire (200ft)", 1,
new BigDecimal(50.67).setScale(2, RoundingMode.HALF_UP)));
lines.add(new OrderLine("Washers (1/4\")", 24,
new BigDecimal(.15).setScale(2, RoundingMode.HALF_UP)));
Order order = new Order(
"B-9910",
LocalDate.parse("2019-04-18", DateTimeFormatter.ISO_DATE),
"Customer, Jane",
lines);
Let’s write our order using writeValue:
让我们使用writeValue来写我们的订单。
mapper.writeValue(new File("src/main/resources/orderOutput.yaml"), order);
When we look into the orderOutput.yaml, it should look similar to:
当我们查看orderOutput.yaml时,它应该看起来类似于。
orderNo: "B-9910"
date: "2019-04-18"
customerName: "Customer, Jane"
orderLines:
- item: "Copper Wire (200ft)"
quantity: 1
unitPrice: 50.67
- item: "Washers (1/4\")"
quantity: 24
unitPrice: 0.15
6. Conclusion
6.结语
In this quick tutorial, we learned how to read and write YAML to and from files using the Jackson library. We also looked at a few configuration items that will help us get our data looking the way we want.
在这个快速教程中,我们学习了如何使用Jackson库来读写YAML文件。我们还看了一些配置项,这些配置项将帮助我们使我们的数据看起来像我们想要的那样。
The full example code is over on GitHub.
完整的示例代码在GitHub上。