YAML support for Apache Groovy providing parsing, building, and conversion capabilities
—
Programmatic YAML construction using Groovy's builder pattern with support for closures, maps, lists, and dynamic method calls. Provides a fluent API for creating YAML documents from Groovy data structures.
Creates a YAML builder instance that supports Groovy's builder pattern with various construction methods.
/**
* A builder for creating YAML payloads using Groovy's builder pattern.
* Extends GroovyObjectSupport for dynamic method support and implements Writable for output.
*/
public class YamlBuilder extends GroovyObjectSupport implements Writable {
/**
* Creates a new YAML builder instance.
* Initializes internal JsonBuilder for processing.
*/
public YamlBuilder();
/**
* Get the internal content object built by the builder.
* @return the content object
*/
public Object getContent();
}Create a root YAML object from a Map using named arguments.
/**
* Named arguments can be passed to the YAML builder instance to create a root YAML object.
* @param m a map of key / value pairs
* @return a map of key / value pairs
*/
public Object call(Map m);Usage Example:
import groovy.yaml.YamlBuilder;
import java.util.Map;
YamlBuilder yaml = new YamlBuilder();
yaml.call(Map.of("name", "Guillaume", "age", 33));
assert yaml.toString().equals("""---
name: "Guillaume"
age: 33
""");Create a root YAML array from a List.
/**
* A list of elements as arguments to the YAML builder creates a root YAML array.
* @param l a list of values
* @return a list of values
*/
public Object call(List l);Usage Example:
import groovy.yaml.YamlBuilder;
import java.util.List;
YamlBuilder yaml = new YamlBuilder();
Object result = yaml.call(List.of(1, 2, 3));
assert result instanceof List;
assert yaml.toString().equals("""---
- 1
- 2
- 3
""");Create a root YAML array from variable arguments.
/**
* Varargs elements as arguments to the YAML builder create a root YAML array.
* @param args an array of values
* @return a list of values
*/
public Object call(Object... args);Usage Example:
import groovy.yaml.YamlBuilder;
YamlBuilder yaml = new YamlBuilder();
Object result = yaml.call(1, 2, 3);
assert result instanceof List;
assert yaml.toString().equals("""---
- 1
- 2
- 3
""");Create a root YAML array from a collection, applying a closure to each object.
/**
* A collection and closure passed to a YAML builder will create a root YAML array applying
* the closure to each object in the collection.
* @param coll a collection
* @param c a closure used to convert the objects of coll
* @return a list of values
*/
public Object call(Iterable coll, Closure c);
/**
* Delegates to the Iterable version.
* @param coll a collection
* @param c a closure used to convert the objects of coll
* @return a list of values
*/
public Object call(Collection coll, Closure c);Usage Example:
import groovy.yaml.YamlBuilder
class Author {
String name
}
def authors = [new Author(name: "Guillaume"), new Author(name: "Jochen"), new Author(name: "Paul")]
def yaml = new YamlBuilder()
yaml.call(authors) { Author author ->
name author.name
}
assert yaml.toString() == '''---
- name: "Guillaume"
- name: "Jochen"
- name: "Paul"
'''Create a root YAML object from a closure using builder pattern.
/**
* A closure passed to a YAML builder will create a root YAML object.
* @param c a closure whose method call statements represent key / values of a YAML object
* @return a map of key / value pairs
*/
public Object call(Closure c);Usage Example:
import groovy.yaml.YamlBuilder
def yaml = new YamlBuilder()
def result = yaml {
name "Guillaume"
age 33
}
assert result instanceof Map
assert yaml.toString() == '''---
name: "Guillaume"
age: 33
'''Create YAML structure using dynamic method calls following the builder pattern.
/**
* A method call on the YAML builder instance will create a root object with only one key
* whose name is the name of the method being called.
* This method takes various argument types including closures, maps, or no arguments.
* @param name the single key
* @param args the value associated with the key
* @return a map with a single key
*/
@Override
public Object invokeMethod(String name, Object args);Usage Examples:
import groovy.yaml.YamlBuilder
// Builder-style with closure
def yaml = new YamlBuilder()
def result = yaml.person {
name "Guillaume"
age 33
}
assert yaml.toString() == '''---
person:
name: "Guillaume"
age: 33
'''
// Named arguments
yaml = new YamlBuilder()
yaml.person(name: "Guillaume", age: 33)
assert yaml.toString() == '''---
person:
name: "Guillaume"
age: 33
'''
// Mixed named arguments and closure
yaml = new YamlBuilder()
yaml.person(name: "Guillaume", age: 33) { town "Paris" }
assert yaml.toString() == '''---
person:
name: "Guillaume"
age: 33
town: "Paris"
'''
// Empty args create empty object
yaml = new YamlBuilder()
yaml.person()
assert yaml.toString() == '''---
person: {}
'''Convert the built structure to YAML string format.
/**
* Serializes the internal data structure built with the builder to a conformant YAML payload string.
* @return a YAML output string
*/
@Override
public String toString();Usage Example:
import groovy.yaml.YamlBuilder
def yaml = new YamlBuilder()
yaml { temperature 37 }
assert yaml.toString() == '''---
temperature: 37
'''Write the YAML output to a Writer instance (Writable interface).
/**
* The YAML builder implements the Writable interface,
* so that you can have the builder serialize itself the YAML payload to a writer.
* @param out a writer on which to serialize the YAML payload
* @return the writer
* @throws IOException if writing fails
*/
@Override
public Writer writeTo(Writer out) throws IOException;Usage Example:
import groovy.yaml.YamlBuilder
import java.io.StringWriter
def yaml = new YamlBuilder()
yaml { temperature 37 }
def out = new StringWriter()
out << yaml
assert out.toString() == '''---
temperature: 37
'''YAML building methods may throw YamlRuntimeException if the structure cannot be converted to valid YAML:
public class YamlRuntimeException extends GroovyRuntimeException {
public YamlRuntimeException(String msg);
public YamlRuntimeException(Throwable cause);
public YamlRuntimeException(String msg, Throwable cause);
}Install with Tessl CLI
npx tessl i tessl/maven-org-apache-groovy--groovy-yaml