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?
What is the benefit of using YAML vis-a-vis JSON? Looks like to JSON to me?
@hbagchi According to the YAML 1.2 spec, YAML is an official subset of JSON, so you’re right – it does look like JSON. I don’t yet know the benefits of YAML vs. JSON – I was looking at YAML vs. XML, since I knew that Ruby and Rails seemed to prefer YAML over XML.
@hbagchi : I guess JSON does not support recursive objects.
very useful post
BTW, I had troubles escaping chars with snake yaml
could anybody give a hand with this question: http://stackoverflow.com/questions/9448918/snakeyaml-escaping-new-line-characters-in-yaml
The Problem with YAML: One little Typo breaks your syntax for e.g. one EmptySpace, evt. one commata breaks it all. Another issue with both yaml and json: missing semantics.