Working with YAML in Java: SnakeYAML and Jackson Libraries
YAML (YAML Ain't Markup Language) is a human-readable data serialization format, often used for configuration files, API descriptions, and data exchange. In the Java ecosystem, there are several libraries for working with YAML, but the most popular are SnakeYAML (a low-level parser) and Jackson YAML (a high-level solution with annotation support). In this article, we will take a detailed look at both libraries, their capabilities, and usage examples.
1. SnakeYAML — The Classic YAML Parser
SnakeYAML is a simple and lightweight library that provides full control over the parsing process. It supports all YAML 1.1 specifications and allows working with both simple structures (Map, List) and custom objects.
Installing SnakeYAML
If you use Maven, add the dependency to pom.xml:
<dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>2.2</version></dependency>For Gradle:
implementation 'org.yaml:snakeyaml:2.2'Example: Parsing a YAML String into a Map
import org.yaml.snakeyaml.Yaml;import java.util.Map;
public class SnakeYamlExample { public static void main(String[] args) { String yamlString = """ server: host: localhost port: 8080 database: url: jdbc:mysql://localhost:3306/mydb user: admin password: secret """;
Yaml yaml = new Yaml(); Map<String, Object> data = yaml.load(yamlString);
System.out.println(data); // Output: {server={host=localhost, port=8080}, database={url=jdbc:mysql://localhost:3306/mydb, user=admin, password=secret}} }}Example: Loading YAML from a File
import org.yaml.snakeyaml.Yaml;import java.io.FileInputStream;import java.io.InputStream;import java.util.Map;
public class SnakeYamlFileExample { public static void main(String[] args) { try (InputStream input = new FileInputStream("config.yaml")) { Yaml yaml = new Yaml(); Map<String, Object> data = yaml.load(input); System.out.println("Port: " + ((Map<String, Object>) data.get("server")).get("port")); } catch (Exception e) { e.printStackTrace(); } }}Example: Serializing a Java Object to YAML
import org.yaml.snakeyaml.Yaml;import java.util.Map;
public class SnakeYamlDumpExample { public static void main(String[] args) { Map<String, Object> data = Map.of( "name", "PythonLib", "version", 2.0, "features", List.of("YAML", "JSON", "XML") );
Yaml yaml = new Yaml(); String output = yaml.dump(data); System.out.println(output); // Output: // features: // - YAML // - JSON // - XML // name: PythonLib // version: 2.0 }}Pros of SnakeYAML:
- Simple API with no extra dependencies
- Support for custom types via constructors
- Flexible parsing configuration (e.g., ignoring unknown fields)
2. Jackson YAML — A Modern Solution with Annotations
Jackson YAML is a module for the popular Jackson library that allows you to work with YAML just like with JSON. It supports annotations (@JsonProperty, @JsonIgnore), automatic binding to POJOs, and streaming processing.
Installing Jackson YAML
Maven:
Also in library
Working with YAML in C# (.NET): YamlDotNet Library
gRPC for Java: Libraries, Setup, and Usage Examples
Data Validation in Swift (iOS/macOS): Libraries and Approaches
Working with Images in Go: Resize and Crop with the imaging Library
Authorization and Authentication in Swift: A Complete Guide for iOS/macOS
Recommendations