mirror of
https://github.com/plexusorg/toml4j.git
synced 2025-02-14 04:38:04 +00:00
Removed Toml#to(Class, Gson)
This commit is contained in:
parent
0817b40d8f
commit
4d32f77ce0
2 changed files with 28 additions and 43 deletions
|
@ -266,13 +266,6 @@ public class Toml {
|
||||||
* @return A new instance of targetClass.
|
* @return A new instance of targetClass.
|
||||||
*/
|
*/
|
||||||
public <T> T to(Class<T> 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);
|
HashMap<String, Object> valuesCopy = new HashMap<String, Object>(values);
|
||||||
|
|
||||||
if (defaults != null) {
|
if (defaults != null) {
|
||||||
|
@ -283,13 +276,13 @@ public class Toml {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
JsonElement json = gson.toJsonTree(valuesCopy);
|
JsonElement json = DEFAULT_GSON.toJsonTree(valuesCopy);
|
||||||
|
|
||||||
if (targetClass == JsonElement.class) {
|
if (targetClass == JsonElement.class) {
|
||||||
return targetClass.cast(json);
|
return targetClass.cast(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
return gson.fromJson(json, targetClass);
|
return DEFAULT_GSON.fromJson(json, targetClass);
|
||||||
}
|
}
|
||||||
|
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
package com.moandjiezana.toml;
|
package com.moandjiezana.toml;
|
||||||
|
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.io.InputStreamReader;
|
import java.io.InputStreamReader;
|
||||||
|
import java.io.Reader;
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
|
@ -12,8 +15,6 @@ import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.TimeZone;
|
import java.util.TimeZone;
|
||||||
|
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.Ignore;
|
import org.junit.Ignore;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
|
@ -244,31 +245,31 @@ public class BurntSushiValidTest {
|
||||||
run("unicode-literal");
|
run("unicode-literal");
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
|
||||||
public void after() throws IOException {
|
|
||||||
inputToml.close();
|
|
||||||
if (expectedJsonReader != null) {
|
|
||||||
expectedJsonReader.close();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void run(String testName) {
|
private void run(String testName) {
|
||||||
inputToml = getClass().getResourceAsStream("burntsushi/valid/" + testName + ".toml");
|
InputStream inputToml = getClass().getResourceAsStream("burntsushi/valid/" + testName + ".toml");
|
||||||
expectedJsonReader = new InputStreamReader(getClass().getResourceAsStream("burntsushi/valid/" + testName + ".json"));
|
Reader expectedJsonReader = new InputStreamReader(getClass().getResourceAsStream("burntsushi/valid/" + testName + ".json"));
|
||||||
JsonElement expectedJson = new Gson().fromJson(expectedJsonReader, JsonElement.class);
|
JsonElement expectedJson = GSON.fromJson(expectedJsonReader, JsonElement.class);
|
||||||
|
|
||||||
Toml toml = new Toml().parse(inputToml);
|
Toml toml = new Toml().parse(inputToml);
|
||||||
JsonElement actual = toml.to(JsonElement.class, TEST_GSON);
|
JsonElement actual = TEST_GSON.toJsonTree(toml).getAsJsonObject().get("values");
|
||||||
|
|
||||||
Assert.assertEquals(expectedJson, actual);
|
assertEquals(expectedJson, actual);
|
||||||
|
|
||||||
|
try {
|
||||||
|
inputToml.close();
|
||||||
|
} catch (IOException e) {}
|
||||||
|
|
||||||
|
try {
|
||||||
|
expectedJsonReader.close();
|
||||||
|
} catch (IOException e) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputStream inputToml;
|
private static final Gson GSON = new Gson();
|
||||||
private InputStreamReader expectedJsonReader;
|
|
||||||
|
|
||||||
private static final Gson TEST_GSON = new GsonBuilder()
|
private static final Gson TEST_GSON = new GsonBuilder()
|
||||||
.registerTypeAdapter(Boolean.class, serialize(Boolean.class))
|
.registerTypeAdapter(Boolean.class, serialize(Boolean.class))
|
||||||
.registerTypeAdapter(String.class, serialize(String.class))
|
.registerTypeAdapter(String.class, serialize(String.class))
|
||||||
|
.registerTypeAdapter(Long.class, serialize(Long.class))
|
||||||
|
.registerTypeAdapter(Double.class, serialize(Double.class))
|
||||||
.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
|
.registerTypeAdapter(Date.class, new JsonSerializer<Date>() {
|
||||||
@Override
|
@Override
|
||||||
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
|
public JsonElement serialize(Date src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
|
@ -277,15 +278,6 @@ public class BurntSushiValidTest {
|
||||||
return context.serialize(new Value("datetime", iso8601Format.format(src)));
|
return context.serialize(new Value("datetime", iso8601Format.format(src)));
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.registerTypeHierarchyAdapter(Number.class, new JsonSerializer<Number>() {
|
|
||||||
@Override
|
|
||||||
public JsonElement serialize(Number src, Type typeOfSrc, JsonSerializationContext context) {
|
|
||||||
String number = src.toString();
|
|
||||||
String type = number.contains(".") ? "float" : "integer";
|
|
||||||
|
|
||||||
return context.serialize(new Value(type, number));
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.registerTypeHierarchyAdapter(List.class, new JsonSerializer<List<?>>() {
|
.registerTypeHierarchyAdapter(List.class, new JsonSerializer<List<?>>() {
|
||||||
@Override
|
@Override
|
||||||
public JsonElement serialize(List<?> src, Type typeOfSrc, JsonSerializationContext context) {
|
public JsonElement serialize(List<?> src, Type typeOfSrc, JsonSerializationContext context) {
|
||||||
|
@ -341,11 +333,11 @@ public class BurntSushiValidTest {
|
||||||
return "string";
|
return "string";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (aClass == Float.class || aClass == Double.class) {
|
if (aClass == Double.class) {
|
||||||
return "float";
|
return "float";
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Number.class.isAssignableFrom(aClass)) {
|
if (aClass == Long.class) {
|
||||||
return "integer";
|
return "integer";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue