Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 565
Anand Avatar asked Jan 24 '12 18:01

Anand


People also ask

Is JSON good as configuration?

However, JSON is actually a pretty terrible configuration language for a number of reasons. Don't get me wrong — I like JSON. It is a flexible format that is relatively easy for both machines and humans to read, and it's a pretty good data interchange and storage format. But as a configuration language, it falls short.

What is JSON configuration file?

config. json is the main configuration file. Data from config. json is used to configure virtual machine. After editing file make sure that your JSON syntax is valid.

Is YAML good for config?

YAML is a data serialization language that is often used for writing configuration files.

Is YAML the best?

YAML is better suitable for configuration than JSON, whereas JSON is suitable for serialization format and transferring the data for API. JSON is good for human readability and suitable for serialization. It is explicit and can transmit the data over HTTP.


2 Answers

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)
like image 177
mguymon Avatar answered Oct 23 '22 02:10

mguymon


I found this HOCON example:

my.organization {
    project {
        name = "DeathStar"
        description = ${my.organization.project.name} "is a tool to take control over whole world. By world I mean couch, computer and fridge ;)"
    }
    team {
        members = [
            "Aneta"
            "Kamil"
            "Lukasz"
            "Marcin"
        ]
    }
}
my.organization.team.avgAge = 26

to read values:

val config = ConfigFactory.load()
config.getString("my.organization.project.name")  // => DeathStar
config.getString("my.organization.project.description") // => DeathStar is a tool to take control over whole world. By world I mean couch, computer and fridge ;)
config.getInt("my.organization.team.avgAge") // => 26
config.getStringList("my.organization.team.members") // => [Aneta, Kamil, Lukasz, Marcin]

Reference: marcinkubala.wordpress.com

like image 22
tokhi Avatar answered Oct 23 '22 01:10

tokhi