Java

Java YAML Shootout – SnakeYaml vs YamlBeans

After spending a couple months developing LiveOn using the Play! Framework, I’ve grown increasingly intolerant of other Java frameworks. While I’d used YAML before in Rails & Python, Java frameworks usually ignore YAML in favor of XML for configuration. The creators of Play! realized that XML sucked, and implemented their dependency, database, and routing configurations as YAML.

So, in the spirit of helping spread YAML around the Java world, I’ve taken a look at a few YAML libraries. Lets see which one stacks up as the best bet for adding YAML support to your Java project.


The contenders

Note: I’ve ommited JYaml. While it is an early implementation, and still a useful library, it is no longer maintained by its creator, and therefore not a realistic candidate.

The full code sample can be found here: https://github.com/adamnengland/yaml-shootout

Basic Usage – For simple usage, its pretty much a wash. Examples in both libraries look almost identical

SnakeYaml

1 2 3 4 5 6 7 
<br /> @Test<br /> public void testSimpleConfigurationLoad() throws Exception {<br /> InputStream input = new FileInputStream(new File("src/test/resources/simpleConfig.yml"));<br /> Map data = (Map) Yaml.load(input);<br /> assertEquals("yaml-shootout", data.get("simple"));<br /> }<br /> 

YamlBeans

<br /> @Test</p> <p>public void testSimpleConfigurationLoad() throws Exception {<br /> YamlReader reader = new YamlReader(new FileReader("src/test/resources/simpleConfig.yml"));<br /> Map data = (Map) reader.read();<br /> assertEquals("yaml-shootout", data.get("simple"));<br /> }<br />

Parsing to a Bean – Once again, not much difference here.

SnakeYaml

<br /> @Test<br /> @Test<br /> public void testAdvancedConfigurationLoad() throws Exception {<br /> InputStream input = new FileInputStream(new File("src/test/resources/advancedConfig.yml"));<br /> YamlConfig data = (YamlConfig) Yaml.load(input);<br /> assertEquals("yaml-shootout", data.simple);<br /> assertEquals("gmail", data.advanced.type);<br /> }<br />

YamlBeans

<br /> @Test<br /> public void testAdvancedConfigurationLoad() throws Exception {<br /> YamlReader reader = new YamlReader(new FileReader("src/test/resources/advancedConfig.yml"));<br /> YamlConfig data = (YamlConfig) reader.read();<br /> assertEquals("yaml-shootout", data.simple);<br /> assertEquals("gmail", data.advanced.type);<br /> }<br />

Extra Features

  • Both Libraries support serialization (with a similar, simple syntax)
  • Both Libraries support deserialization into maps, lists, and Strings
  • Both Libraries support Tags & Shortcuts
  • SnakeYaml supports YAML’s merge specification.

Community

Here, SnakeYaml starts to get the edge. Both SnakeYaml and YamlBeans are hosted on google code.

SnakeYaml YamlBeans
Starred By 118 users 24 users
Commiters 3 1
Google Group Messages 630 57

Finally

Well, I have to say, I’m a little disappointed in the outcome. I’d hoped for something a little more controversial when I started this experiment, but it appears that SnakeYaml & YamlBeans are both excellent yaml frameworks for java, with a lot of the same syntax. However, at the end of the day, SnakeYaml gets the edge for a larger feature set, and a much more active community


Leave a Reply

Your email address will not be published. Required fields are marked *