The examples in typesafe config require you to know a path to specify literally, e.g.:
config1.getString("complex-app.something")
I need to be able to parse a config file with free-form labels in places. How can I handle that.
Say that I have HOCON like:
root : {
a : { x: 1, y: 2 },
b : { x: 2, z: 3 }
}
Without knowing what keys are in the file, how can I find out that there are keys 'a' and 'b' and that the value of the object with key 'a' has keys 'x' and 'y' and for 'b' there are keys 'x' and 'z'?
Here is one way to not do it:
Config config = ConfigFactory.load("configula");
Config root = config.getObject("root").toConfig();
for (Entry<String, ConfigValue> e: root.entrySet()) {
// The key is a.x, a.y etc. ??!?
// The value is not a map-like thing
}
With the keys of the entry set being a path, I have to parse the path to find what the keys were, which is pretty close to making it easier to simply parse a text file with StringTokenizer or the like.
What is the the config way of parsing nested objects? For example, with groovy and ConfigSlurper a similar structure:
root {
a {
x = 1
y = 2
}
b {
x = 2
z = 3
}
}
would be parsed like this:
cfg = new ConfigSlurper().parse(new File('configula.config'))
cfg.root.each { k, r ->
r.each { kk, v ->
println "${k}, ${kk} -> ${v}"
}
}
every combination of things I've tried ends up with the value being a ConfigValue. Is there a way to get a tree of keys that map to objects that themselves contain keys that map to objects?
To answer my own question: ConfigObject
extends java.util.Map
so, a way to navigate when a key can be any arbitrary value, is to use ConfigObject.keySet()
.