
In this tutorial, we will see how to convert a Java Object into a YAML String/File using Jackson Library,
Step 1: Add Jackson YAML Dependency
Maven:<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
<version>2.13.4</version>
</dependency>
Gradle:
implementation group: 'com.fasterxml.jackson.dataformat', name: 'jackson-dataformat-yaml', version: '2.13.4'
Step 2: Java Object to YAML
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import java.io.IOException;
public class JavaObjectToYaml {
public static void main(String[] args) throws IOException {
String[] subjects = {"Java","PHP","Python","C#"};
Student student = new Student("Andrew",24,"Computers", subjects);
ObjectMapper objectMapper = new ObjectMapper(new YAMLFactory());
String yamlString = objectMapper.writeValueAsString(student);
System.out.println(yamlString);
}
}
Output:
name: "Andrew"
age: 24
course: "Computers"
subjects:
- "Java"
- "PHP"
- "Python"
- "C#"
Have Questions? Post them here!
- Error: Can not find the tag library descriptor for
- Create a Database Table using JDBC PreparedStatement
- Java JDBC Select Multiple Records from table as List using PreparedStatement
- Java Jackson with Maven Example
- [fix] Java JDBC ConnectException: Connection refused
- Spring Boot: Transactions Management with JDBCTemplate Example
- Java Get Current Date for a Specific Time Zone
- What are the 8 Primitive Data Types in Java
- Java JDBC: Insert Java 8 LocalDate and Time using PreparedStatement
- Maven Eclipse (M2e) No archetypes currently available
- How to Sort a LinkedList in Java
- [Fatal Error] XML The markup in the document following the root element must be well-formed.
- Split a String in Java with Examples
- Struts 2 : There is no Action mapped for namespace [/] and action name [form] associated with context path [/proj]
- Truncate table using Java JDBC Example
- Java: Generate random numbers within a range
- Parse XML file in Java using DOM Parser
- How to get Client IP address using Java Code Example
- JDBCTemplate Querying Examples with Spring Boot 3
- [Java Threads] Should we extend Thread Class or implement Runnable interface
- String Boot + Redis - SET and GET String Commands Examples
- Setting up Spring Boot 3 + Maven + MySQL + JDBC Example
- Spring Boot: JdbcTemplate Update Query With Parameters Example
- Java Split String by Spaces
- Unbound classpath container: JRE System Library [JavaSE-1.7]
- [Error] Microsoft Teams: We're sorry—we've run into an issue. - Microsoft
- Bash For Loop Example - Bash
- Share Multiple Images in WhatsApp using Android Intent - WhatsApp
- How to serialize-deserialize an object in java - Java
- How to remove disable google search blue triangle - Google
- Notepad++ insert a blank line above or below the current line example - NotepadPlusPlus
- Notepad++ Ghost typing Examples - NotepadPlusPlus
- How to know the Serial Number of MacBook on macOS Ventura 13.0 - MacOS