Working with YAML in Java: Library Overview and Usage Examples
YAML (YAML Ain't Markup Language) is a data serialization format often used for configuration files due to its readability. In Java, two main libraries are used for working with YAML: SnakeYAML (lightweight, fast) and Jackson YAML (powerful, with annotation support). In this article, we will thoroughly examine both libraries, showing examples of reading, writing, and object serialization.
1. SnakeYAML Library
SnakeYAML is the most popular library for working with YAML in Java. It supports the full YAML 1.1 syntax, including tags, anchors, and custom types. Key features:
- Reading YAML into
Map,List, or custom Java objects - Writing Java objects to YAML
- Support for recursive structures
- Low memory consumption
Adding SnakeYAML (Maven)
<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>2.2</version></dependency>Reading a YAML File
Consider an example YAML file config.yaml:
server: host: localhost port: 8080database: url: jdbc:mysql://localhost:3306/mydb user: admin password: secretJava code for reading:
import org.yaml.snakeyaml.Yaml;import java.io.InputStream;import java.util.Map;
public class SnakeYamlReader { public static void main(String[] args) { Yaml yaml = new Yaml(); try (InputStream in = SnakeYamlReader.class .getClassLoader() .getResourceAsStream("config.yaml")) { Map<String, Object> data = yaml.load(in); System.out.println("Server host: " + ((Map<String, Object>) data.get("server")).get("host")); } catch (Exception e) { e.printStackTrace(); } }}Writing to YAML
import org.yaml.snakeyaml.Yaml;import java.util.Map;import java.util.LinkedHashMap;
public class SnakeYamlWriter { public static void main(String[] args) { Map<String, Object> data = new LinkedHashMap<>(); Map<String, Object> server = new LinkedHashMap<>(); server.put("host", "127.0.0.1"); server.put("port", 9090); data.put("server", server);
Yaml yaml = new Yaml(); String output = yaml.dump(data); System.out.println(output); }}2. Jackson YAML Library
Jackson YAML is a module for Jackson that adds support for the YAML format. Key advantages:
- Full integration with Jackson annotations (
@JsonProperty,@JsonIgnore) - Working with POJO objects without additional Maps
- High performance due to streaming processing
- Support for YAML 1.1 and partially 1.2
Adding Jackson YAML (Maven)
<dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-yaml</artifactId> <version>2.16.1</version></dependency>Serializing POJO to YAML
Let's create a class for configuration:
import com.fasterxml.jackson.annotation.JsonProperty;
public class AppConfig { @JsonProperty("server") private ServerConfig server; @JsonProperty("database") private DatabaseConfig database; // getters and setters public ServerConfig getServer() { return server; } public void setServer(ServerConfig server) { this.server = server; } public DatabaseConfig getDatabase() { Also in library
Database Migrations in TypeScript: A Complete Guide to Tools and Approaches
Redis client for Java: Jedis, Lettuce and Redisson — overview and examples
Sending Email in Swift: Libraries and Examples for iOS/macOS
Bubble Tea Go: Complete Guide with Examples for TUI Apps