Under the covers Groovy's YamlBuilder is using Jackson's JSON to YAML converter.
Jackson's converter does support literal block style, but this needs to be enabled. The current version of YamlBuilder does not support setting options.
I copied the YamlBuilder class and the related YamlConverter class so I could modify the settings.
In the YamlBuilder class, I modified this method:
public static String convertJsonToYaml(Reader jsonReader) {
try (Reader reader = jsonReader) {
JsonNode json = new ObjectMapper().readTree(reader);
return new YAMLMapper().writeValueAsString(json);
} catch (IOException e) {
throw new YamlRuntimeException(e);
}
}
To be this:
public static String convertJsonToYaml(Reader jsonReader) {
try (Reader reader = jsonReader) {
JsonNode json = new ObjectMapper().readTree(reader);
YAMLMapper mapper = new YAMLMapper()
mapper.configure(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE, true)
return mapper.writeValueAsString(json);
} catch (IOException e) {
throw new YamlRuntimeException(e);
}
}
This allows me to do:
mapper.configure(YAMLGenerator.Feature.LITERAL_BLOCK_STYLE, true)
Which will successfully render the YAML as a literal block:
data: |-
this is
a literal
text value