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

@Test
public void testSimpleConfigurationLoad() throws Exception {
  InputStream input = new FileInputStream(new File("src/test/resources/simpleConfig.yml"));
  Map data = (Map) Yaml.load(input);
  assertEquals("yaml-shootout", data.get("simple"));
}

YamlBeans

@Test

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

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

SnakeYaml

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

YamlBeans

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

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

Advertisements
Tagged ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: