SnakeYAML and Groovy
I had some fun playing with SnakeYAML and Groovy the other day. Below are some of my results. If you want to run this in your groovyConsole, you need to download SnakeYAML and add the snakeyaml-<version>.jar to your classpath.
From YAML to Groovy:
import org.yaml.snakeyaml.Yaml Yaml yaml = new Yaml() def obj = yaml.load(""" a: 1 b: 2 c: - aaa - bbb""") assert obj.a == 1 assert obj.b == 2 assert obj.c == ["aaa", "bbb"]
From Groovy to YAML:
import org.yaml.snakeyaml.Yaml def map = [name: "Pushkin", aliases: ['P', 'Push']] Yaml yaml = new Yaml() String output = yaml.dump(map) assert output == '''name: Pushkin aliases: [P, Push] '''
Isn’t YAML so much simpler and cleaner than XML? Why aren’t more of us using YAML with Java and Groovy?