This blog is now obsolete. Go to scott.arbeitman.id.au for all new content.

ColdFusion and YAML

| Tuesday, April 24, 2007
One interseting thing in Ruby on Rails that's sorely missing in ColdFusion is YAML. For those who don't know, YAML is similar to XML or JSON, and is a way to represent complex data or objects. YAML is extrememly lightweight and less verbose than XML.

After a little server configuration, it's easy to turn YAML text in a file into a ColdFusion structure.

I'm using JYaml as my YAML parsing library, but other options are available. Unfortunately, JYaml requires Java 5, which is not the standard ColdFusion operating environment. To remedy this, download a Java 5 JRE and follow Ben Forta's concise directions. Don't forget to put the JYaml jar file in your classpath and restart your ColdFusion server.

Assuming your .cfm file and .yaml file are in the same directory, creating a ColdFusion structure out of it is a easy as:


<cfscript>
Yaml = CreateObject("java", "org.ho.yaml.Yaml");

configFile = CreateObject("java", "java.io.File").init(ExpandPath('.')&"/settings.yaml");

settings = Yaml.load(configFile);
</cfscript>


Dumping the "settings" variable reveals exactly the structure you were expecting.

One caveat: you may experience unexpected behavior when trying to access your struct's properties using dot notation. Instead, use the "associative array" with square brackets option.

So do this:

value = settings['credentials']['username']

not this:

value = settings.credentials.username

1 comments:

Anonymous said...

BLOGGER

LAMBL