Yaml with JYaml

Posted by Warner Onstine Tue, 19 Dec 2006 04:25:41 GMT

So, I decided to use Yaml for all of my configuration needs, in order to use this I needed a library to interface with it, I found JYaml which seems to fit the bill nicely. Here’s a short example of what I’ve used it for in Chama.

--- !org.chama.db.DBConfiguration
dev: !org.chama.db.DBConnection
  dbName: chama_dev
  driver: org.hsqldb.jdbcDriver
  url: "jdbc:hsqldb:mem:"
  username: sa
production: !org.chama.db.DBConnection
  dbName: chama_production
  driver: org.hsqldb.jdbcDriver
  url: "jdbc:hsqldb:mem:"
  username: sa
test: !org.chama.db.DBConnection
  dbName: chama_test
  driver: org.hsqldb.jdbcDriver
  url: "jdbc:hsqldb:mem:"
  username: sa

DBConfigurationTest.groovy
class DBConfigurationTest extends GroovyTestCase {

    void testReadConfigFile() {
        DBConfiguration config = Yaml.load(new File("src/test/conf/db.yml"))
            assert config.test.dbName == "chama_test"
            assert config.dev.dbName == "chama_dev"
            assert config.production.dbName = "chama_production"
    }

}

I also found another package when I was having some difficulty with JYaml called JvYaml, but when I tried to save my configuration class it gave me a nested object exception telling me that I couldn’t save nested objects into my Yaml file. That seemed kinda stupid to me so perhaps I was doing something wrong, but I didn’t have time to try and figure out what it was and went back to JYaml.

Trackbacks

Use the following link to trackback from your own site:
http://www.warneronstine.com/blog/articles/trackback/224

Comments

Leave a response

Comments