YAML support for Apache Groovy - provides YamlBuilder for creating YAML payloads and YamlSlurper for parsing YAML content
npx @tessl/cli install tessl/maven-org-codehaus-groovy--groovy-yaml@3.0.0YAML support for Apache Groovy - provides YamlBuilder for creating YAML payloads and YamlSlurper for parsing YAML content. The library offers a fluent API for building YAML documents and comprehensive parsing capabilities for reading YAML from various sources.
implementation 'org.codehaus.groovy:groovy-yaml:3.0.25'<dependency><groupId>org.codehaus.groovy</groupId><artifactId>groovy-yaml</artifactId><version>3.0.25</version></dependency>import groovy.yaml.YamlBuilder
import groovy.yaml.YamlSlurper
import groovy.yaml.YamlRuntimeException
import org.apache.groovy.yaml.util.YamlConverterFor Java:
import groovy.yaml.YamlBuilder;
import groovy.yaml.YamlSlurper;
import groovy.yaml.YamlRuntimeException;
import org.apache.groovy.yaml.util.YamlConverter;// Build YAML using YamlBuilder
def builder = new YamlBuilder()
builder.person {
name "John Doe"
age 30
address {
street "123 Main St"
city "Anytown"
}
}
String yamlString = builder.toString()
// Parse YAML using YamlSlurper
def slurper = new YamlSlurper()
def parsed = slurper.parseText(yamlString)
assert parsed.person.name == "John Doe"
assert parsed.person.age == 30The groovy-yaml library is built around four core components:
Internally, the library leverages Jackson's YAML dataformat libraries and converts between JSON and YAML formats, allowing seamless integration with Groovy's existing JsonBuilder and JsonSlurper components.
Construct YAML documents using various builder patterns including named arguments, closures, arrays, and collections.
class YamlBuilder extends GroovyObjectSupport implements Writable {
/** Default constructor */
YamlBuilder()
/** Get the internal content object */
Object getContent()
/** Create root YAML object from named arguments */
Object call(Map m)
/** Create root YAML array from list */
Object call(List l)
/** Create root YAML array from varargs */
Object call(Object... args)
/** Create root YAML array applying closure to collection */
Object call(Iterable coll, Closure c)
Object call(Collection coll, Closure c)
/** Create root YAML object from closure */
Object call(Closure c)
/** Dynamic method dispatch for builder DSL */
Object invokeMethod(String name, Object args)
/** Serialize internal structure to YAML string */
String toString()
/** Write YAML output to Writer */
Writer writeTo(Writer out) throws IOException
}Usage Examples:
// Named arguments approach
def builder = new YamlBuilder()
builder.person(name: "Alice", age: 25)
// Closure-based DSL
builder.person {
name "Bob"
age 30
hobbies(["reading", "swimming"])
}
// Array creation
builder([1, 2, 3, 4])
// Collection with closure
def people = [new Person(name: "Charlie"), new Person(name: "Diana")]
builder people, { person ->
name person.name
id person.id
}Parse YAML content from strings, readers, streams, files, or paths into native Groovy data structures.
class YamlSlurper {
/** Default constructor */
YamlSlurper()
/** Parse YAML string into object tree */
Object parseText(String yaml)
/** Parse YAML from Reader into object tree */
Object parse(Reader reader)
/** Parse YAML from InputStream into object tree */
Object parse(InputStream stream)
/** Parse YAML from File into object tree */
Object parse(java.io.File file) throws IOException
/** Parse YAML from Path into object tree */
Object parse(Path path) throws IOException
}Usage Examples:
def slurper = new YamlSlurper()
// Parse from string
def result1 = slurper.parseText("""
name: "Product X"
price: 29.99
categories:
- electronics
- gadgets
""")
// Parse from file
def result2 = slurper.parse(new File("config.yml"))
// Parse from path
def result3 = slurper.parse(Paths.get("data.yml"))
// Parse from input stream
def result4 = slurper.parse(new FileInputStream("input.yml"))Utility functions for converting between YAML and JSON formats.
class YamlConverter {
/** Convert YAML reader content to JSON string */
static String convertYamlToJson(Reader yamlReader)
/** Convert JSON reader content to YAML string */
static String convertJsonToYaml(Reader jsonReader)
}Usage Examples:
// Convert YAML to JSON
String yamlContent = "name: John\nage: 30"
String jsonResult = YamlConverter.convertYamlToJson(new StringReader(yamlContent))
// Convert JSON to YAML
String jsonContent = '{"name":"John","age":30}'
String yamlResult = YamlConverter.convertJsonToYaml(new StringReader(jsonContent))Custom exception type for YAML processing errors.
class YamlRuntimeException extends GroovyRuntimeException {
/** Exception with message */
YamlRuntimeException(String msg)
/** Exception with cause */
YamlRuntimeException(Throwable cause)
/** Exception with message and cause */
YamlRuntimeException(String msg, Throwable cause)
}Common Error Scenarios:
try {
def slurper = new YamlSlurper()
def result = slurper.parseText("invalid: yaml: content:")
} catch (YamlRuntimeException e) {
println "YAML parsing failed: ${e.message}"
}// Standard Java/Groovy types used throughout the API
interface Writable {
Writer writeTo(Writer out) throws IOException
}
class GroovyObjectSupport {
// Base class providing dynamic method invocation
}
class GroovyRuntimeException extends RuntimeException {
// Base exception class for Groovy runtime errors
}
// Java standard library types
class Reader // java.io.Reader
class Writer // java.io.Writer
class InputStream // java.io.InputStream
class File // java.io.File
class Path // java.nio.file.Path
interface Map<K,V> // java.util.Map
interface List<E> // java.util.List
interface Collection<E> // java.util.Collection
interface Iterable<T> // java.lang.Iterable
class Closure<V> // groovy.lang.Closure