I am using snakeYaml to convert a String in YAML format to Groovy Map.
@Grab(group='org.yaml', module='snakeyaml', version='1.17')
import org.yaml.snakeyaml.Yaml
Yaml yaml = new Yaml()
Map config = yaml.load(new File('config.yaml').text)
I want to reach now the opposite : Given a Groovy Map, i want to convert it to String in YAML format WITHOUT WRITING THE STRING in a FILE.
I found groovy.yaml.YamlBuilder
. However, the groovy compiler of the environment is old and this class is not found.
Ideally, the same lib (snakeYaml) can handle the opposite case. But no way to get it work.
yes yaml.dump()
is the answer
Map config = [kind: 'Pod', metadata:[name: app]]
Yaml yaml = new Yaml()
assert yaml.dump(config) == '''kind: Pod
metadata:
name: app
'''