Class Toml
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);
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleanbooleancontainsPrimitive(String key) booleancontainsTable(String key) booleancontainsTableArray(String key) entrySet()private ObjectgetBoolean(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()
-
Field Details
-
DEFAULT_GSON
private static final com.google.gson.Gson DEFAULT_GSON -
values
-
defaults
-
-
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.
-
Toml
-
-
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
-
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
-
getList
-
getBoolean
-
getBoolean
-
getDate
-
getDate
-
getDouble
-
getDouble
-
getTable
-
getTables
-
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
-
get
-