mirror of
https://github.com/plexusorg/toml4j.git
synced 2024-12-28 19:24:15 +00:00
Added Toml#to(Class, Gson) to allow for test output as specified by
BurntSushi/toml-test
This commit is contained in:
parent
e36b8dbe8e
commit
d9e1ccc98a
1 changed files with 17 additions and 3 deletions
|
@ -26,6 +26,7 @@ import org.parboiled.parserunners.RecoveringParseRunner;
|
|||
import org.parboiled.support.ParsingResult;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.JsonElement;
|
||||
|
||||
/**
|
||||
* <p>Provides access to the keys and tables in a TOML data source.</p>
|
||||
|
@ -46,10 +47,11 @@ import com.google.gson.Gson;
|
|||
*/
|
||||
public class Toml {
|
||||
|
||||
private static Pattern ARRAY_INDEX_PATTERN = Pattern.compile("(.*)\\[(\\d+)\\]");
|
||||
private static final Gson DEFAULT_GSON = new Gson();
|
||||
private static final Pattern ARRAY_INDEX_PATTERN = Pattern.compile("(.*)\\[(\\d+)\\]");
|
||||
|
||||
private Map<String, Object> values = new HashMap<String, Object>();
|
||||
private final Toml defaults;
|
||||
private final Gson gson = new Gson();
|
||||
|
||||
/**
|
||||
* Creates Toml instance with no defaults.
|
||||
|
@ -223,6 +225,13 @@ public class Toml {
|
|||
* @param targetClass
|
||||
*/
|
||||
public <T> T to(Class<T> targetClass) {
|
||||
return to(targetClass, DEFAULT_GSON);
|
||||
}
|
||||
|
||||
/*
|
||||
* Should not be used directly, except for testing purposes
|
||||
*/
|
||||
<T> T to(Class<T> targetClass, Gson gson) {
|
||||
HashMap<String, Object> valuesCopy = new HashMap<String, Object>(values);
|
||||
|
||||
if (defaults != null) {
|
||||
|
@ -233,7 +242,12 @@ public class Toml {
|
|||
}
|
||||
}
|
||||
|
||||
String json = gson.toJson(valuesCopy);
|
||||
JsonElement json = gson.toJsonTree(valuesCopy);
|
||||
|
||||
if (targetClass == JsonElement.class) {
|
||||
return targetClass.cast(json);
|
||||
}
|
||||
|
||||
return gson.fromJson(json, targetClass);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue