YAML support for Apache Groovy providing parsing, building, and conversion capabilities
—
Bidirectional conversion utilities between YAML and JSON formats using Jackson's processing capabilities. Provides static utility methods for format transformation without requiring parser or builder instances.
Static utility class providing bidirectional conversion between YAML and JSON formats.
/**
* A converter for converting YAML to JSON, and vice versa.
* Uses Jackson libraries internally for robust format handling.
*/
public final class YamlConverter {
// Private constructor - utility class with static methods only
}Convert YAML content from a Reader to JSON string format.
/**
* Convert yaml to json.
* @param yamlReader the reader of yaml content
* @return the text of json
* @throws YamlRuntimeException if conversion fails
*/
public static String convertYamlToJson(Reader yamlReader);Usage Example:
import org.apache.groovy.yaml.util.YamlConverter;
import java.io.StringReader;
String yamlContent = """
name: example
version: 1.0.0
dependencies:
- groovy
- jackson
""";
String jsonResult = YamlConverter.convertYamlToJson(new StringReader(yamlContent));
// jsonResult will be: {"name":"example","version":"1.0.0","dependencies":["groovy","jackson"]}Multiple Document Handling:
import org.apache.groovy.yaml.util.YamlConverter;
import java.io.StringReader;
String multiDocYaml = """
---
name: doc1
---
name: doc2
---
name: doc3
""";
String jsonResult = YamlConverter.convertYamlToJson(new StringReader(multiDocYaml));
// Multiple YAML documents are converted to a JSON array
// jsonResult will be: [{"name":"doc1"},{"name":"doc2"},{"name":"doc3"}]Convert JSON content from a Reader to YAML string format.
/**
* Convert json to yaml.
* @param jsonReader the reader of json content
* @return the text of yaml
* @throws YamlRuntimeException if conversion fails
*/
public static String convertJsonToYaml(Reader jsonReader);Usage Example:
import org.apache.groovy.yaml.util.YamlConverter;
import java.io.StringReader;
String jsonContent = """
{
"name": "example",
"version": "1.0.0",
"dependencies": ["groovy", "jackson"]
}
""";
String yamlResult = YamlConverter.convertJsonToYaml(new StringReader(jsonContent));
// yamlResult will be:
// ---
// name: "example"
// version: "1.0.0"
// dependencies:
// - "groovy"
// - "jackson"Array Conversion:
import org.apache.groovy.yaml.util.YamlConverter;
import java.io.StringReader;
String jsonArray = """
[
{"name": "item1", "value": 1},
{"name": "item2", "value": 2}
]
""";
String yamlResult = YamlConverter.convertJsonToYaml(new StringReader(jsonArray));
// yamlResult will be:
// ---
// - name: "item1"
// value: 1
// - name: "item2"
// value: 2Use YamlConverter in conjunction with YamlSlurper for advanced processing:
import groovy.yaml.YamlSlurper;
import org.apache.groovy.yaml.util.YamlConverter;
import java.io.StringReader;
// Convert JSON to YAML, then parse with YamlSlurper
String jsonContent = """{"name": "example", "items": [1, 2, 3]}""";
String yamlContent = YamlConverter.convertJsonToYaml(new StringReader(jsonContent));
YamlSlurper yamlSlurper = new YamlSlurper();
Object result = yamlSlurper.parseText(yamlContent);Use YamlConverter with YamlBuilder for round-trip conversions:
import groovy.yaml.YamlBuilder
import org.apache.groovy.yaml.util.YamlConverter
import java.io.StringReader
// Build YAML, convert to JSON, then back to YAML
def yamlBuilder = new YamlBuilder()
yamlBuilder {
name "example"
items([1, 2, 3])
}
String originalYaml = yamlBuilder.toString()
String jsonVersion = YamlConverter.convertYamlToJson(new StringReader(originalYaml))
String roundTripYaml = YamlConverter.convertJsonToYaml(new StringReader(jsonVersion))Both conversion methods throw YamlRuntimeException if the input format is invalid or conversion fails:
public class YamlRuntimeException extends GroovyRuntimeException {
public YamlRuntimeException(String msg);
public YamlRuntimeException(Throwable cause);
public YamlRuntimeException(String msg, Throwable cause);
}Example Error Handling:
import org.apache.groovy.yaml.util.YamlConverter;
import groovy.yaml.YamlRuntimeException;
import java.io.StringReader;
try {
String invalidJson = "{invalid json content";
String result = YamlConverter.convertJsonToYaml(new StringReader(invalidJson));
} catch (YamlRuntimeException e) {
System.err.println("Conversion failed: " + e.getMessage());
// Handle conversion error
}com.fasterxml.jackson.dataformat.yaml.YAMLFactory) for YAML processingcom.fasterxml.jackson.databind.ObjectMapper) for JSON processingInstall with Tessl CLI
npx tessl i tessl/maven-org-apache-groovy--groovy-yaml