Human editable JSON-like or YAML-like program configuration in Java

Anand picture Anand · Jan 24, 2012 · Viewed 9.3k times · Source

Is there a Java library similar to libconfig for C++, where the config file is stored in a JSON-like format that can be edited by humans, and later read from the program?

I don't want to use Spring or any of the larger frameworks. What I'm looking for is a small, fast, self-contained library. I looked at java.util.Properties, but it doesn't seem to support hierarchical/nested config data.

Answer

mguymon picture mguymon · Nov 27, 2012

I think https://github.com/typesafehub/config is exactly what you are looking for. The format is called HOCON for Human-Optimized Config Object Notation and it a super-set of JSON.

Examples of HOCON:

HOCON that is also valid JSON:

{
    "foo" : {
        "bar" : 10,
        "baz" : 12
    }
}

HOCON also supports standard properties format, so the following is valid as well:

foo.bar=10
foo.baz=12

One of the features I find very useful is inheritance, this allows you to layer configurations. For instance a library would have a reference.conf, and the application using the library would have an application.conf. The settings in the application.conf will override the defaults in reference.conf.

Standard Behavior for loading configs:

The convenience method ConfigFactory.load() loads the following (first-listed are higher priority):

  • system properties application.conf (all resources on classpath with this name)
  • application.json (all resources on classpath with this name)
  • application.properties (all resources on classpath with this name)
  • reference.conf (all resources on classpath with this name)