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#"
Facing issues? Have Questions? Post them here! I am happy to answer!
More Posts related to Java,
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
More Posts:
- How to Screenshot on Mac Ventura 13 - Step by Step Guide - MacOS
- Path of homebrew (brew) installation in macOS Big Sur - MacOS
- How to update Cargo (Rust Lang) - Rust
- What is exit(0), exit(1) ... exit(8) codes in Python Programming - Python
- Android Lint app_name is not translated in af (Afrikaans) am (Amharic) ar (Arabic) bg (Bulgarian) - Android
- Comprehensive Tutorial: Ways to Create Objects in JavaScript - JavaScript
- Fix: ZSH: cd: too many arguments (macOS) - zsh
- Add HTML to Android Activity or TextView - Android