YAML support for Apache Groovy providing parsing, building, and conversion capabilities
—
Comprehensive YAML parsing functionality that converts YAML documents from various sources into Groovy data structures, providing the same ease of use as JsonSlurper for JSON processing.
Creates a YAML parser instance that can parse YAML from multiple input sources.
/**
* Represents a YAML parser that converts YAML documents into Groovy data structures.
* Similar to JsonSlurper but for YAML format.
*/
public class YamlSlurper {
/**
* Creates a new YAML parser instance.
* Initializes internal JsonSlurper for processing converted data.
*/
public YamlSlurper();
}Parse YAML content directly from a string.
/**
* Parse the content of the specified yaml string into a tree of Nodes.
* @param yaml the content of yaml as a string
* @return the root node of the parsed tree of Nodes
*/
public Object parseText(String yaml);Usage Example:
import groovy.yaml.YamlSlurper;
YamlSlurper yamlSlurper = new YamlSlurper();
Object result = yamlSlurper.parseText("""
language: groovy
sudo: required
dist: trusty
matrix:
include:
- jdk: openjdk10
- jdk: oraclejdk9
- jdk: oraclejdk8
before_script:
- unset _JAVA_OPTIONS
""");
// Access parsed data using Groovy's dynamic syntax
assert "groovy".equals(result.language);
assert "required".equals(result.sudo);
assert "trusty".equals(result.dist);Parse YAML content from a Reader instance.
/**
* Parse the content of the specified reader into a tree of Nodes.
* @param reader the reader of yaml content
* @return the root node of the parsed tree of Nodes
*/
public Object parse(Reader reader);Usage Example:
import groovy.yaml.YamlSlurper;
import java.io.StringReader;
YamlSlurper yamlSlurper = new YamlSlurper();
String yamlContent = "name: example\nversion: 1.0.0";
Object result = yamlSlurper.parse(new StringReader(yamlContent));Parse YAML content from an InputStream.
/**
* Parse the content of the specified input stream into a tree of Nodes.
* @param stream the input stream of yaml content
* @return the root node of the parsed tree of Nodes
*/
public Object parse(InputStream stream);Usage Example:
import groovy.yaml.YamlSlurper;
import java.io.ByteArrayInputStream;
import java.nio.charset.StandardCharsets;
YamlSlurper yamlSlurper = new YamlSlurper();
String yamlContent = "name: example\nversion: 1.0.0";
InputStream stream = new ByteArrayInputStream(yamlContent.getBytes(StandardCharsets.UTF_8));
Object result = yamlSlurper.parse(stream);Parse YAML content from a File object.
/**
* Parse the content of the specified file into a tree of Nodes.
* @param file the file containing yaml content
* @return the root node of the parsed tree of Nodes
* @throws IOException if file cannot be read
*/
public Object parse(java.io.File file) throws IOException;Usage Example:
import groovy.yaml.YamlSlurper;
import java.io.File;
import java.io.IOException;
YamlSlurper yamlSlurper = new YamlSlurper();
try {
Object result = yamlSlurper.parse(new File("config.yaml"));
// Process parsed YAML data
} catch (IOException e) {
// Handle file reading errors
}Parse YAML content from a Path object (Java NIO).
/**
* Parse the content of the specified path into a tree of Nodes.
* @param path the path to the file containing yaml content
* @return the root node of the parsed tree of Nodes
* @throws IOException if file cannot be read
*/
public Object parse(Path path) throws IOException;Usage Example:
import groovy.yaml.YamlSlurper;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.IOException;
YamlSlurper yamlSlurper = new YamlSlurper();
try {
Path configPath = Paths.get("config.yaml");
Object result = yamlSlurper.parse(configPath);
// Process parsed YAML data
} catch (IOException e) {
// Handle file reading errors
}All parsing methods may throw YamlRuntimeException if the YAML content is malformed or cannot be processed:
public class YamlRuntimeException extends GroovyRuntimeException {
public YamlRuntimeException(String msg);
public YamlRuntimeException(Throwable cause);
public YamlRuntimeException(String msg, Throwable cause);
}Example Error Handling:
import groovy.yaml.YamlSlurper;
import groovy.yaml.YamlRuntimeException;
YamlSlurper yamlSlurper = new YamlSlurper();
try {
Object result = yamlSlurper.parseText("invalid: yaml: content: [");
} catch (YamlRuntimeException e) {
System.err.println("YAML parsing failed: " + e.getMessage());
}Install with Tessl CLI
npx tessl i tessl/maven-org-apache-groovy--groovy-yaml