Provides access to the keys and tables in a TOML data source.
All getters can fall back to default values if they have been provided as a constructor argument.
Getters for simple values (String, Date, etc.) will return null if no matching key exists.
getList(String), getTable(String) and getTables(String) return empty values if there is no matching key.
All read methods throw an IllegalStateException if the TOML is incorrect.
Example usage:
Toml toml = new Toml().read(getTomlFile());
String name = toml.getString("name");
Long port = toml.getLong("server.ip"); // compound key. Is equivalent to:
Long port2 = toml.getTable("server").getLong("ip");
MyConfig config = toml.to(MyConfig.class);
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleancontainsPrimitive(String key) booleancontainsTable(String key) booleancontainsTableArray(String key) entrySet()getBoolean(String key) getBoolean(String key, Boolean defaultValue) <T> List<T><T> List<T>booleanisEmpty()Populates the current Toml instance with values from otherToml.Populates the current Toml instance with values from file.read(InputStream inputStream) Populates the current Toml instance with values from inputStream.Populates the current Toml instance with values from reader.Populates the current Toml instance with values from tomlString.<T> TPopulates an instance of targetClass with the values of this Toml instance.toMap()
-
Constructor Details
-
Toml
public Toml()Creates Toml instance with no defaults. -
Toml
- Parameters:
defaults- fallback values used when the requested key or table is not present in the TOML source that has been read.
-
-
Method Details
-
read
Populates the current Toml instance with values from file.- Parameters:
file- The File to be read. Expected to be encoded as UTF-8.- Returns:
- this instance
- Throws:
IllegalStateException- If file contains invalid TOML
-
read
Populates the current Toml instance with values from inputStream.- Parameters:
inputStream- Closed after it has been read.- Returns:
- this instance
- Throws:
IllegalStateException- If file contains invalid TOML
-
read
Populates the current Toml instance with values from reader.- Parameters:
reader- Closed after it has been read.- Returns:
- this instance
- Throws:
IllegalStateException- If file contains invalid TOML
-
read
Populates the current Toml instance with values from otherToml.- Parameters:
otherToml-- Returns:
- this instance
-
read
Populates the current Toml instance with values from tomlString.- Parameters:
tomlString- String to be read.- Returns:
- this instance
- Throws:
IllegalStateException- If tomlString is not valid TOML
-
getString
-
getString
-
getLong
-
getLong
-
getList
- Type Parameters:
T- type of list items- Parameters:
key- a TOML key- Returns:
nullif the key is not found
-
getList
- Type Parameters:
T- type of list items- Parameters:
key- a TOML keydefaultValue- a list of default values- Returns:
nullis the key is not found
-
getBoolean
-
getBoolean
-
getDate
-
getDate
-
getDouble
-
getDouble
-
getTable
- Parameters:
key- A table name, not including square brackets.- Returns:
- A new Toml instance or
nullif no value is found for key.
-
getTables
- Parameters:
key- Name of array of tables, not including square brackets.- Returns:
- A
Listof Toml instances ornullif no value is found for key.
-
contains
- Parameters:
key- a key name, can be compound (eg. a.b.c)- Returns:
- true if key is present
-
containsPrimitive
- Parameters:
key- a key name, can be compound (eg. a.b.c)- Returns:
- true if key is present and is a primitive
-
containsTable
- Parameters:
key- a key name, can be compound (eg. a.b.c)- Returns:
- true if key is present and is a table
-
containsTableArray
- Parameters:
key- a key name, can be compound (eg. a.b.c)- Returns:
- true if key is present and is a table array
-
isEmpty
public boolean isEmpty() -
to
Populates an instance of targetClass with the values of this Toml instance. The target's field names must match keys or tables. Keys not present in targetClass will be ignored.
Tables are recursively converted to custom classes or to
Map<String, Object>.In addition to straight-forward conversion of TOML primitives, the following are also available:
- Integer -> int, long (or wrapper),
BigInteger - Float -> float, double (or wrapper),
BigDecimal - One-letter String -> char,
Character - String ->
String, enum,URI,URL - Multiline and Literal Strings ->
String - Array ->
List,Set, array. The generic type can be anything that can be converted. - Table -> Custom class,
Map<String, Object>
- Type Parameters:
T- type of targetClass.- Parameters:
targetClass- Class to deserialize TOML to.- Returns:
- A new instance of targetClass.
- Integer -> int, long (or wrapper),
-
toMap
-
entrySet
- Returns:
- a
Setof Map.Entry instances. Modifications to theSetare not reflected in this Toml instance. Entries are immutable, soMap.Entry.setValue(Object)throws an UnsupportedOperationException.
-