Removed Toml#to(Class, Gson)

This commit is contained in:
moandji.ezana 2015-04-29 00:19:39 +02:00
parent 0817b40d8f
commit 4d32f77ce0
2 changed files with 28 additions and 43 deletions

View file

@ -266,15 +266,8 @@ public class Toml {
* @return A new instance of 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) {
for (Map.Entry<String, Object> entry : defaults.values.entrySet()) {
if (!valuesCopy.containsKey(entry.getKey())) {
@ -282,14 +275,14 @@ public class Toml {
}
}
}
JsonElement json = gson.toJsonTree(valuesCopy);
JsonElement json = DEFAULT_GSON.toJsonTree(valuesCopy);
if (targetClass == JsonElement.class) {
return targetClass.cast(json);
}
return gson.fromJson(json, targetClass);
return DEFAULT_GSON.fromJson(json, targetClass);
}
@SuppressWarnings("unchecked")

View file

@ -1,8 +1,11 @@
package com.moandjiezana.toml;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Type;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
@ -12,8 +15,6 @@ import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;
import org.junit.After;
import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test;
@ -244,31 +245,31 @@ public class BurntSushiValidTest {
run("unicode-literal");
}
@After
public void after() throws IOException {
inputToml.close();
if (expectedJsonReader != null) {
expectedJsonReader.close();
}
}
private void run(String testName) {
inputToml = getClass().getResourceAsStream("burntsushi/valid/" + testName + ".toml");
expectedJsonReader = new InputStreamReader(getClass().getResourceAsStream("burntsushi/valid/" + testName + ".json"));
JsonElement expectedJson = new Gson().fromJson(expectedJsonReader, JsonElement.class);
InputStream inputToml = getClass().getResourceAsStream("burntsushi/valid/" + testName + ".toml");
Reader expectedJsonReader = new InputStreamReader(getClass().getResourceAsStream("burntsushi/valid/" + testName + ".json"));
JsonElement expectedJson = GSON.fromJson(expectedJsonReader, JsonElement.class);
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 InputStreamReader expectedJsonReader;
private static final Gson GSON = new Gson();
private static final Gson TEST_GSON = new GsonBuilder()
.registerTypeAdapter(Boolean.class, serialize(Boolean.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>() {
@Override
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)));
}
})
.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<?>>() {
@Override
public JsonElement serialize(List<?> src, Type typeOfSrc, JsonSerializationContext context) {
@ -341,11 +333,11 @@ public class BurntSushiValidTest {
return "string";
}
if (aClass == Float.class || aClass == Double.class) {
if (aClass == Double.class) {
return "float";
}
if (Number.class.isAssignableFrom(aClass)) {
if (aClass == Long.class) {
return "integer";
}