How to use the Jackson DataFormat Yaml in the Java project for data serialization and desertileization
How to use the Jackson DataFormat Yaml in the Java project for data serialization and desertileization
Jackson is a popular Java library for conversion between Java objects and JSON data.But in some cases, we may prefer to use the YAML format to store and transmit data, because it is easier to read and can process multiple data types.In order to use YAML in the Java project, we can use Jackson DataFormat Yaml to extend library.
The following is the steps of using the Jackson DataFormat Yaml in the Java project for the steps of serialization and derivativeization of data:
1. Import dependencies
First of all, we need to add Jackson DataFormat Yaml to the project construction file.Using Maven projects can add the following dependencies to the pom.xml file:
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.12.4</version>
</dependency>
If you use Gradle, add the following code to the built.gradle file:
gradle
implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:2.12.4'
2. Create a java class
Next, we need to create an ordinary Java class, which will be serialized to YAML or serialize from YAML.
public class Person {
private String name;
private int age;
// Construction function, getter and setter method
// omit other code
}
3. Sequence to YAML
To turn the Java object sequence to YAML, we need to create an ObjectMapper instance and configure it to use yaml format.
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class Main {
public static void main(String[] args) throws IOException {
// Create ObjectMapper and use yamlFactory for configuration
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
// Create an object
Person Person = New Person ("Zhang San", 25);
// Turn the object sequence to YAML string
String yamlString = objectMapper.writeValueAsString(person);
// Print yaml string
System.out.println(yamlString);
}
}
The output will be similar to:
yaml
---
name: "Zhang San"
age: 25
4. From YAML's back serialization
To be transformed from YAML to Java objects, we can use ObjectMapper's `Readvalue () method.
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
public class Main {
public static void main(String[] args) throws IOException {
// Create ObjectMapper and use yamlFactory for configuration
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
// yaml string
String yamlString = "---
name: \ "Zhang San \"
age: 25";
// From YAML's back -sequentialization to object
Person person = objectMapper.readValue(yamlString, Person.class);
// Print the attribute of the object
System.out.println(person.getName());
System.out.println(person.getAge());
}
}
The output will be:
Zhang San
25
Through the above steps, we successfully used the Jackson DataFormat Yaml in the Java project for data serialization and derivativeization.This enables us to easily convert Java objects into YAML string and re -create the Java object from the YAML string.