Reading Entries
Configuration files can contain entries for anything from primitive types up to complex hierarchies of POJOs consisting
of Maps, Collections, and/or beans.
Entries are accessed via the Config.get(String) method which returns the following bean:
The most common case for configuration values are primitives.
# A string
key1 = foo
# A boolean
key2 = true
# An integer
key3 = 123
# A long
key4 = 10000000000
# Doubles
key5 = 6.67e−11
key6 = Infinity
On integers and longs, "K", "M", and "G" can be used to identify kilo, mega, and giga.
key1 = 100K # Same as 1024000
key2 = 100M # Same as 104857600
Numbers can also use hexadecimal and octal notation:
hex1 = 0x12FE
hex2 = 0X12FE
octal1 = 01234
Strings with newlines are treated as multi-line values that get broken into separate lines:
key1 = This is a particularly long sentence that we want to split
onto separate lines.
Typically, multi-line values are started on the next line for clarity like so:
key1 =
This is a particularly long sentence that we want to split
onto separate lines.
POJOs
Entries can also be read as POJOs.
In theory, any parsable POJO type can be represented as a config value. However in practice, we're typically talking about the following:
- Objects convertible from Strings.
- Beans.
An example of an object convertible from a String was already shown in an example above. In that case, it was a URL which has a public constructor that takes in a String:
# A POJO
key4 = http://bar
// Read values from section #1
URL key4 = config.get("Section1/key4").as(URL.class).orElse(null);
Beans are represented as JSON 5 by default:
// Contact information
[ContactInfo]
address =
{
street: '123 Main Street',
city: 'Anywhere',
state: 'NY',
zip: 12345
}
// Example bean
public class Address {
public String street, city;
public StateEnum state;
public int zip;
}
// Example usage
Config config = Config.create("MyConfig.cfg").build();
Address myAddress = config.get("ContactInfo/address").as(Address.class).orElse(null);
The default serializer and parser is registered on the Config through the following methods:
Arrays
The asStringArray() method allows you to retrieve comma-delimited lists of values:
key1 = foo, bar, baz
String[] key1 = config.get("key1").asStringArray().orElse(null);
String arrays can also be represented in JSON when the registered parser is a JSON parser:
key1 = ['foo','bar','baz']
String[] key1 = config.get("key1").asStringArray().orElse(null);
Primitive arrays can also be retrieved using the as() methods:
key1 = [1,2,3]
int[] key1 = config.get("key1").as(int[].class).orElse(null);
Arrays of POJOs can also be retrieved using the methods as well:
addresses =
[
{
street: '123 Main Street',
city: 'Anywhere',
state: 'NY',
zip: 12345
},
{
street: '456 Main Street',
city: 'Anywhere',
state: 'NY',
zip: 12345
}
]
Address[] addresses = config.get("addresses").as(Address[].class).orElse(null);
Binary data
Entries can also be accessed as binary data.
Binary data can be represented in 3 formats:
| Format | Example |
|---|---|
| BASE-64 (default) | Zm9vYmFycw== |
| Hexadecimal | 666F6F62617273 |
| Spaced hexadecimal | 66 6F 6F 62 61 72 73 |
The binary data format is controlled via the following setting:
key = Zm9vYmFycw==
byte[] bytes = config.get("key").asBytes().orElse(null);
Binary lines can be split up into separate lines for readability:
key =
Zm9vYm
Fycw==
Binary data line wrapping can be controlled via the following setting:
In This Section
📄️ 2.5.2.1. Java Collection Framework Objects
Entries can also be read as Java Collection Framework objects.