Friday, 29 April 2016

YAML for config

Played about with YAML today using it instead of a properties file in a java command line application (a data loader).

It is more flexible than a property file (it can do nesting and arrays) but is less verbose and more readable than JSON or XML so is a great fit for configuration. It copes better with more complicated config and will be more future proof for an app.

I used the snakeyaml which is very lightweight and supports binding to a java object.

1. Import dependency

compile 'org.yaml:snakeyaml:1.17' 

2. Create a simple java bean configuration class

3. Load file and map to configuration class

Config cfg = new Yaml().loadAs(is, Config.class);

Hello world!

The inaugral hello world test!