Configuration library for JVM languages using HOCON files
—
Configuration values provide the type-safe foundation for the Typesafe Config library. The ConfigValue hierarchy includes interfaces for different value types (objects, lists, primitives) with support for unwrapping, rendering, and origin tracking.
Base interface for all configuration values.
public interface ConfigValue extends ConfigMergeable {
ConfigValueType valueType();
Object unwrapped();
String render();
String render(ConfigRenderOptions options);
ConfigOrigin origin();
ConfigValue withOrigin(ConfigOrigin origin);
Config atPath(String path);
Config atKey(String key);
}Usage Examples:
ConfigValue value = config.getValue("app.settings");
// Check value type
ConfigValueType type = value.valueType();
if (type == ConfigValueType.OBJECT) {
ConfigObject obj = (ConfigObject) value;
}
// Unwrap to plain Java object
Object unwrapped = value.unwrapped();
// Render as string
String rendered = value.render();
String json = value.render(ConfigRenderOptions.concise().setJson(true));
// Get origin information
ConfigOrigin origin = value.origin();
String filename = origin.filename();
int lineNumber = origin.lineNumber();Represents object/map values in configuration.
public interface ConfigObject extends ConfigValue, Map<String, ConfigValue> {
Config toConfig();
ConfigObject withOnlyKey(String key);
ConfigObject withoutKey(String key);
ConfigObject withValue(String key, ConfigValue value);
ConfigObject withOnlyPath(String path);
ConfigObject withoutPath(String path);
ConfigObject withValue(String path, ConfigValue value);
Map<String, Object> unwrapped();
}Usage Examples:
ConfigObject serverObj = config.getObject("server");
// Convert to Config for path-based access
Config serverConfig = serverObj.toConfig();
int port = serverConfig.getInt("port");
// Map-based access (key-based, not path-based)
ConfigValue portValue = serverObj.get("port");
// Manipulation (returns new instances)
ConfigObject withHttps = serverObj.withValue("https.enabled", ConfigValueFactory.fromAnyRef(true));
ConfigObject httpOnly = serverObj.withOnlyKey("http");
ConfigObject withoutSsl = serverObj.withoutKey("ssl");
// Unwrap to plain Map
Map<String, Object> serverMap = serverObj.unwrapped();Represents list/array values in configuration.
public interface ConfigList extends List<ConfigValue>, ConfigValue {
List<Object> unwrapped();
}Usage Examples:
ConfigList serverList = config.getList("load-balancer.servers");
// List operations (immutable)
int size = serverList.size();
ConfigValue firstServer = serverList.get(0);
// Iterate over values
for (ConfigValue server : serverList) {
if (server.valueType() == ConfigValueType.OBJECT) {
ConfigObject serverObj = (ConfigObject) server;
Config serverConfig = serverObj.toConfig();
String host = serverConfig.getString("host");
}
}
// Unwrap to plain List
List<Object> unwrappedList = serverList.unwrapped();Enumeration of all possible configuration value types.
public enum ConfigValueType {
OBJECT, // ConfigObject - maps/objects
LIST, // ConfigList - arrays/lists
NUMBER, // Numbers (int, long, double)
BOOLEAN, // Boolean values
NULL, // Null values
STRING // String values
}Usage Examples:
ConfigValue value = config.getValue("some.path");
switch (value.valueType()) {
case OBJECT:
ConfigObject obj = (ConfigObject) value;
break;
case LIST:
ConfigList list = (ConfigList) value;
break;
case NUMBER:
Number num = (Number) value.unwrapped();
break;
case BOOLEAN:
Boolean bool = (Boolean) value.unwrapped();
break;
case STRING:
String str = (String) value.unwrapped();
break;
case NULL:
// Handle null value
break;
}Factory for creating ConfigValue instances from Java objects.
public final class ConfigValueFactory {
public static ConfigValue fromAnyRef(Object object);
public static ConfigValue fromAnyRef(Object object, String originDescription);
public static ConfigObject fromMap(Map<String, ?> values);
public static ConfigObject fromMap(Map<String, ?> values, String originDescription);
public static ConfigList fromIterable(Iterable<?> values);
public static ConfigList fromIterable(Iterable<?> values, String originDescription);
}Usage Examples:
// Create values from Java objects
ConfigValue stringValue = ConfigValueFactory.fromAnyRef("hello world");
ConfigValue numberValue = ConfigValueFactory.fromAnyRef(42);
ConfigValue boolValue = ConfigValueFactory.fromAnyRef(true);
// Create from collections
Map<String, Object> serverMap = Map.of(
"host", "localhost",
"port", 8080,
"ssl", true
);
ConfigObject serverObj = ConfigValueFactory.fromMap(serverMap);
List<String> hosts = List.of("web1", "web2", "web3");
ConfigList hostList = ConfigValueFactory.fromIterable(hosts);
// Create with origin description
ConfigValue documented = ConfigValueFactory.fromAnyRef(
"production-value",
"created programmatically"
);Options for controlling how configuration values are rendered to strings.
public final class ConfigRenderOptions {
public static ConfigRenderOptions defaults();
public static ConfigRenderOptions concise();
public ConfigRenderOptions setComments(boolean value);
public ConfigRenderOptions setOriginComments(boolean value);
public ConfigRenderOptions setFormatted(boolean value);
public ConfigRenderOptions setJson(boolean value);
public ConfigRenderOptions setShowEnvVariableValues(boolean value);
}Usage Examples:
ConfigValue value = config.getValue("database");
// Default rendering (HOCON with comments)
String hocon = value.render();
// Concise JSON output
String json = value.render(ConfigRenderOptions.concise().setJson(true));
// Formatted HOCON without comments
String clean = value.render(
ConfigRenderOptions.defaults()
.setComments(false)
.setFormatted(true)
);
// Include origin information in comments
String withOrigins = value.render(
ConfigRenderOptions.defaults()
.setOriginComments(true)
);
// Hide environment variable values (show ${VAR} instead of actual value)
String secure = value.render(
ConfigRenderOptions.defaults()
.setShowEnvVariableValues(false)
);Tracks where configuration values came from for error reporting and debugging.
public interface ConfigOrigin {
String description();
String filename();
URL url();
String resource();
int lineNumber();
List<String> comments();
ConfigOrigin withComments(List<String> comments);
ConfigOrigin withLineNumber(int lineNumber);
}Usage Examples:
ConfigValue value = config.getValue("server.port");
ConfigOrigin origin = value.origin();
// Get origin information
String desc = origin.description(); // Human-readable description
String file = origin.filename(); // Filename if from file
URL url = origin.url(); // URL if from URL
String resource = origin.resource(); // Resource name if from classpath
int line = origin.lineNumber(); // Line number (-1 if not available)
List<String> comments = origin.comments(); // Associated comments
// Create modified origin
ConfigOrigin withComments = origin.withComments(
List.of("Modified programmatically", "Added validation")
);Factory for creating ConfigOrigin instances.
public final class ConfigOriginFactory {
public static ConfigOrigin newSimple(String description);
public static ConfigOrigin newFile(String filename);
public static ConfigOrigin newURL(URL url);
}Usage Examples:
// Create simple origin
ConfigOrigin simple = ConfigOriginFactory.newSimple("programmatic config");
// Create file origin
ConfigOrigin fileOrigin = ConfigOriginFactory.newFile("/etc/myapp/config.conf");
// Create URL origin
ConfigOrigin urlOrigin = ConfigOriginFactory.newURL(
new URL("https://config.example.com/app.conf")
);
// Use with value creation
ConfigValue valueWithOrigin = ConfigValueFactory
.fromAnyRef("test-value")
.withOrigin(simple);Represents memory/byte size values with unit parsing and conversion.
public final class ConfigMemorySize {
public static ConfigMemorySize ofBytes(long bytes);
public static ConfigMemorySize ofBytes(BigInteger bytes);
public long toBytes();
public BigInteger toBytesBigInteger();
public String toString();
public boolean equals(Object other);
public int hashCode();
}Usage Examples:
// Get memory size from config
ConfigMemorySize maxHeap = config.getMemorySize("jvm.max-heap");
// Convert to bytes
long bytes = maxHeap.toBytes();
BigInteger bigBytes = maxHeap.toBytesBigInteger();
// Create memory sizes
ConfigMemorySize size1 = ConfigMemorySize.ofBytes(1024 * 1024 * 128); // 128MB
ConfigMemorySize size2 = ConfigMemorySize.ofBytes(new BigInteger("4294967296")); // 4GB
// String representation
String readable = maxHeap.toString(); // e.g., "512 MB"ConfigValue.unwrapped() converts configuration values to plain Java objects:
Map<String, Object>List<Object>StringNumber (Integer, Long, Double, or BigDecimal)BooleannullUsage Examples:
// Object unwrapping
ConfigObject obj = config.getObject("database");
Map<String, Object> dbMap = obj.unwrapped();
String host = (String) dbMap.get("host");
Integer port = (Integer) dbMap.get("port");
// List unwrapping
ConfigList list = config.getList("servers");
List<Object> serverList = list.unwrapped();
for (Object server : serverList) {
if (server instanceof Map) {
Map<String, Object> serverMap = (Map<String, Object>) server;
}
}
// Deep unwrapping of nested structures
Object fullyUnwrapped = config.root().unwrapped();
Map<String, Object> configMap = (Map<String, Object>) fullyUnwrapped;config.getString() over config.getValue().unwrapped()valueType() before casting ConfigValue instancesInstall with Tessl CLI
npx tessl i tessl/maven-com-typesafe--config