Paths, keys, and Config vs. ConfigObject
Config
is a view onto a tree of {@link ConfigObject}; the corresponding object tree can be found through {@link Config#root()}. ConfigObject
is a map from config keys, rather than paths, to config values. Think of ConfigObject
as a JSON object and Config
as a configuration API.
The API tries to consistently use the terms "key" and "path." A key is a key in a JSON object; it's just a string that's the key in a map. A "path" is a parseable expression with a syntax and it refers to a series of keys. Path expressions are described in the spec for Human-Optimized Config Object Notation. In brief, a path is period-separated so "a.b.c" looks for key c in object b in object a in the root object. Sometimes double quotes are needed around special characters in path expressions.
The API for a {@code Config} is in terms of path expressions, while the APIfor a {@code ConfigObject} is in terms of keys. Conceptually, {@code Config}is a one-level map from paths to values, while a {@code ConfigObject} is a tree of nested maps from keys to values.
Use {@link ConfigUtil#joinPath} and {@link ConfigUtil#splitPath} to convertbetween path expressions and individual path elements (keys).
Another difference between {@code Config} and {@code ConfigObject} is thatconceptually, {@code ConfigValue}s with a {@link ConfigValue#valueType() valueType()} of {@link ConfigValueType#NULL NULL} exist in a{@code ConfigObject}, while a {@code Config} treats null values as if theywere missing.
Getting configuration values
The "getters" on a {@code Config} all work in the same way. They never returnnull, nor do they return a {@code ConfigValue} with{@link ConfigValue#valueType() valueType()} of {@link ConfigValueType#NULL NULL}. Instead, they throw {@link ConfigException.Missing} if the value iscompletely absent or set to null. If the value is set to null, a subtype of {@code ConfigException.Missing} called {@link ConfigException.Null} will bethrown. {@link ConfigException.WrongType} will be thrown anytime you ask fora type and the value has an incompatible type. Reasonable type conversions are performed for you though.
Iteration
If you want to iterate over the contents of a {@code Config}, you can get its {@code ConfigObject} with {@link #root()}, and then iterate over the {@code ConfigObject} (which implements java.util.Map
). Or, youcan use {@link #entrySet()} which recurses the object tree for you and buildsup a Set
of all path-value pairs where the value is not null.
Resolving substitutions
Substitutions are the ${foo.bar}
syntax in config files, described in the specification. Resolving substitutions replaces these references with real values.
Before using a {@code Config} it's necessary to call {@link Config#resolve()}to handle substitutions (though {@link ConfigFactory#load()} and similarmethods will do the resolve for you already).
Merging
The full Config
for your application can be constructed using the associative operation {@link Config#withFallback(ConfigMergeable)}. If you use {@link ConfigFactory#load()} (recommended), it merges systemproperties over the top of application.conf
over the top of reference.conf
, using withFallback
. You can add in additional sources of configuration in the same way (usually, custom layers should go either just above or just below application.conf
, keeping reference.conf
at the bottom and system properties at the top).
Serialization
Convert a Config
to a JSON or HOCON string by calling {@link ConfigObject#render()} on the root object,myConfig.root().render()
. There's also a variant {@link ConfigObject#render(ConfigRenderOptions)} which allows you to controlthe format of the rendered string. (See {@link ConfigRenderOptions}.) Note that Config
does not remember the formatting of the original file, so if you load, modify, and re-save a config file, it will be substantially reformatted.
As an alternative to {@link ConfigObject#render()}, the toString()
method produces a debug-output-oriented representation (which is not valid JSON).
Java serialization is supported as well for Config
and all subtypes of ConfigValue
.
This is an interface but don't implement it yourself
Do not implement {@code Config}; it should only be implemented by the config library. Arbitrary implementations will not work because the library internals assume a specific concrete implementation. Also, this interface is likely to grow new methods over time, so third-party implementations will break.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|