Top Description Methods
com.typesafe.config

public Interface Config

extends ConfigMergeable
Known Direct Implementers
com.typesafe.config.impl.SimpleConfig
Imports
java.time.Duration, .Period, java.time.temporal.TemporalAmount, java.util.List, .Map, .Set, java.util.concurrent.TimeUnit

An immutable map from config paths to config values. Paths are dot-separated expressions such as foo.bar.baz. Values are as in JSON (booleans, strings, numbers, lists, or objects), represented by ConfigValue instances. Values accessed through the Config interface are never null.

Config is an immutable object and thus safe to use from multiple threads. There's never a need for "defensive copies."

Fundamental operations on a Config include getting configuration values, resolving substitutions with Config#resolve(), and merging configs using Config#withFallback(ConfigMergeable).

All operations return a new immutable Config rather than modifying the original instance.

Examples

You can find an example app and library on GitHub. Also be sure to read the package overview which describes the big picture as shown in those examples.

Paths, keys, and Config vs. ConfigObject

Config is a view onto a tree of ConfigObject; the corresponding object tree can be found through 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 Config is in terms of path expressions, while the API for a ConfigObject is in terms of keys. Conceptually, Config is a one-level map from paths to values, while a ConfigObject is a tree of nested maps from keys to values.

Use ConfigUtil#joinPath and ConfigUtil#splitPath to convert between path expressions and individual path elements (keys).

Another difference between Config and ConfigObject is that conceptually, ConfigValues with a valueType() of NULL exist in a ConfigObject, while a Config treats null values as if they were missing. (With the exception of two methods: Config#hasPathOrNull and Config#getIsNull let you detect null values.)

Getting configuration values

The "getters" on a Config all work in the same way. They never return null, nor do they return a ConfigValue with valueType() of NULL. Instead, they throw ConfigException.Missing if the value is completely absent or set to null. If the value is set to null, a subtype of ConfigException.Missing called ConfigException.Null will be thrown. ConfigException.WrongType will be thrown anytime you ask for a 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 Config, you can get its ConfigObject with root(), and then iterate over the ConfigObject (which implements java.util.Map). Or, you can use entrySet() which recurses the object tree for you and builds up 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 Config it's necessary to call Config#resolve() to handle substitutions (though ConfigFactory#load() and similar methods will do the resolve for you already).

Merging

The full Config for your application can be constructed using the associative operation Config#withFallback(ConfigMergeable). If you use ConfigFactory#load() (recommended), it merges system properties 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 ConfigObject#render() on the root object, myConfig.root().render(). There's also a variant ConfigObject#render(ConfigRenderOptions) which allows you to control the format of the rendered string. (See 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 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 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.

Method Summary

Modifier and TypeMethod and Description
public Config

Returns:

a Config instance containing this config at the given key.
atKey
(String
key to store this config at.
key
)

Places the config inside a Config at the given key.

public Config

Returns:

a Config instance containing this config at the given path.
atPath
(String
path expression to store this config at.
path
)

Places the config inside another Config at the given path.

public void
checkValid(Config
a reference configuration
reference
,
String...
only validate values underneath these paths that your code module owns and understands
restrictToPaths
)

Validates this config against a reference config, throwing an exception if it is invalid.

public Set<Map.Entry<String, ConfigValue>>

Returns:

set of paths with non-null values, built up by recursing the entire tree of ConfigObject and creating an entry for each leaf value.
entrySet
()

Returns the set of path-value pairs, excluding any null values, found by recursing the root object.

public Object

Returns:

the unwrapped value at the requested path
getAnyRef
(String
path expression
path
)

Gets the value at the path as an unwrapped Java boxed value ( Boolean, Integer, and so on - see ConfigValue#unwrapped()).

public List<? extends Object>

Returns:

the list at the path
getAnyRefList
(String
the path to the list value.
path
)

Gets a list value with any kind of elements.

public boolean

Returns:

the boolean value at the requested path
getBoolean
(String
path expression
path
)

public List<Boolean>

Returns:

the list at the path
getBooleanList
(String
the path to the list value.
path
)

Gets a list value with boolean elements.

public Long

Returns:

the value at the requested path, in bytes
getBytes
(String
path expression
path
)

Gets a value as a size in bytes (parses special strings like "128M").

public List<Long>

Returns:

the list at the path
getBytesList
(String
the path to the list value.
path
)

Gets a list value with elements representing a size in bytes.

public Config

Returns:

the nested Config value at the requested path
getConfig
(String
path expression
path
)

public List<? extends Config>

Returns:

the list at the path
getConfigList
(String
the path to the list value.
path
)

Gets a list value with Config elements.

public double

Returns:

the floating-point value at the requested path
getDouble
(String
path expression
path
)

public List<Double>

Returns:

the list at the path
getDoubleList
(String
the path to the list value.
path
)

Gets a list value with double elements.

public long

Returns:

the duration value at the requested path, in the given TimeUnit
getDuration
(String
path expression
path
,
TimeUnit
convert the return value to this time unit
unit
)

Gets a value as a duration in a specified TimeUnit.

public Duration

Returns:

the duration value at the requested path
getDuration
(String
path expression
path
)

Gets a value as a java.time.Duration.

public List<Long>

Returns:

list of durations, in the requested units
getDurationList
(String
a path expression
path
,
TimeUnit
time units of the returned values
unit
)

Gets a list, converting each value in the list to a duration, using the same rules as getDuration(String, TimeUnit).

public List<Duration>

Returns:

list of durations
getDurationList
(String
a path expression
path
)

Gets a list, converting each value in the list to a duration, using the same rules as getDuration(String).

public <
a generic denoting a specific type of enum
T extends Enum<T>
>
T

Returns:

the Enum value at the requested path of the requested enum class
getEnum
(Class<T>
an enum class
enumClass
,
String
path expression
path
)

public <
a generic denoting a specific type of enum
T extends Enum<T>
>
List<T>

Returns:

the list at the path
getEnumList
(Class<T>
the enum class
enumClass
,
String
the path to the list value.
path
)

Gets a list value with Enum elements.

public int

Returns:

the 32-bit integer value at the requested path
getInt
(String
path expression
path
)

Gets the integer at the given path.

public List<Integer>

Returns:

the list at the path
getIntList
(String
the path to the list value.
path
)

Gets a list value with int elements.

public boolean

Returns:

true if the value exists and is null, false if it exists and is not null
getIsNull
(String
the path expression
path
)

Checks whether a value is set to null at the given path, but throws an exception if the value is entirely unset.

public ConfigList

Returns:

the ConfigList at the path
getList
(String
the path to the list value.
path
)

Gets a list value (with any element type) as a ConfigList, which implements java.util.List<ConfigValue>.

public long

Returns:

the 64-bit long value at the requested path
getLong
(String
path expression
path
)

Gets the long integer at the given path.

public List<Long>

Returns:

the list at the path
getLongList
(String
the path to the list value.
path
)

Gets a list value with long elements.

public ConfigMemorySize

Returns:

the value at the requested path, in bytes
getMemorySize
(String
path expression
path
)

Gets a value as an amount of memory (parses special strings like "128M").

public List<ConfigMemorySize>

Returns:

list of memory sizes
getMemorySizeList
(String
a path expression
path
)

Gets a list, converting each value in the list to a memory size, using the same rules as getMemorySize(String).

public Long

Returns:

the duration value at the requested path, in milliseconds
getMilliseconds
(String
path expression
path
)
Deprecated As of release 1.1, replaced by getDuration(String, TimeUnit)

Get value as a duration in milliseconds.

public List<Long>

Returns:

list of millisecond values
getMillisecondsList
(String
the path
path
)
Deprecated As of release 1.1, replaced by getDurationList(String, TimeUnit)

public Long

Returns:

the duration value at the requested path, in nanoseconds
getNanoseconds
(String
path expression
path
)
Deprecated As of release 1.1, replaced by getDuration(String, TimeUnit)

Get value as a duration in nanoseconds.

public List<Long>

Returns:

list of nanosecond values
getNanosecondsList
(String
the path
path
)
Deprecated As of release 1.1, replaced by getDurationList(String, TimeUnit)

public Number

Returns:

the numeric value at the requested path
getNumber
(String
path expression
path
)

public List<Number>

Returns:

the list at the path
getNumberList
(String
the path to the list value.
path
)

Gets a list value with number elements.

public ConfigObject

Returns:

the ConfigObject value at the requested path
getObject
(String
path expression
path
)

public List<? extends ConfigObject>

Returns:

the list at the path
getObjectList
(String
the path to the list value.
path
)

Gets a list value with object elements.

public Period

Returns:

the period value at the requested path
getPeriod
(String
path expression
path
)

Gets a value as a java.time.Period.

public String

Returns:

the string value at the requested path
getString
(String
path expression
path
)

public List<String>

Returns:

the list at the path
getStringList
(String
the path to the list value.
path
)

Gets a list value with string elements.

public TemporalAmount

Returns:

the temporal value at the requested path
getTemporal
(String
path expression
path
)

Gets a value as a java.time.temporal.TemporalAmount.

public ConfigValue

Returns:

the value at the requested path
getValue
(String
path expression
path
)

Gets the value at the given path, unless the value is a null value or missing, in which case it throws just like the other getters.

public boolean

Returns:

true if a non-null value is present at the path
hasPath
(String
the path expression
path
)

Checks whether a value is present and non-null at the given path.

public boolean

Returns:

true if a value is present at the path, even if the value is null
hasPathOrNull
(String
the path expression
path
)

Checks whether a value is present at the given path, even if the value is null.

public boolean

Returns:

true if the configuration is empty
isEmpty
()

Returns true if the Config's root object contains no key-value pairs.

public boolean

Returns:

true if there are no unresolved substitutions remaining in this configuration.
isResolved
()

Checks whether the config is completely resolved.

public ConfigOrigin

Returns:

the origin of the Config for use in error messages
origin
()

Gets the origin of the Config, which may be a file, or a file with a line number, or just a descriptive phrase.

public Config

Returns:

an immutable object with substitutions resolved
resolve
()

Returns a replacement config with all substitutions (the ${foo.bar} syntax, see the spec) resolved.

public Config

Returns:

the resolved Config (may be only partially resolved if options are set to allow unresolved)
resolve
(ConfigResolveOptions
resolve options
options
)

Like Config#resolve() but allows you to specify non-default options.

public Config

Returns:

an immutable object with substitutions resolved
resolveWith
(Config
configuration to pull values from
source
)

Like Config#resolve() except that substitution values are looked up in the given source, rather than in this instance.

public Config

Returns:

the resolved Config (may be only partially resolved if options are set to allow unresolved)
resolveWith
(Config
source configuration to pull values from
source
,
ConfigResolveOptions
resolve options
options
)

Like Config#resolveWith(Config) but allows you to specify non-default options.

public ConfigObject

Returns:

the root object in the configuration
root
()

Gets the Config as a tree of ConfigObject.

public Config
withFallback(ConfigMergeable
an object whose keys should be used as fallbacks, if the keys are not present in this one
other
)

Redeclares com.typesafe.config.ConfigMergeable.withFallback.

Returns a new value computed by merging this value with another, with keys in this value "winning" over the other one.

public Config

Returns:

a copy of the config minus all paths except the one specified
withOnlyPath
(String
path to keep
path
)

Clone the config with only the given path (and its children) retained; all sibling paths are removed.

public Config

Returns:

a copy of the config minus the specified path
withoutPath
(String
path expression to remove
path
)

Clone the config with the given path removed.

public Config

Returns:

the new instance with the new map entry
withValue
(String
path expression for the value's new location
path
,
ConfigValue
value at the new path
value
)

Returns a Config based on this one, but with the given path set to the given value.