Skip to main content

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:

Config.Builderserializer(WriterSerializer)parser(ReaderParser)