mirror of
https://gitlab.com/SamB440/rpgregions-2.git
synced 2025-12-27 18:59:10 +00:00
Make AbstractAdapter work with other packages
This commit is contained in:
10
l10n.toml
10
l10n.toml
@@ -1,10 +0,0 @@
|
||||
basepath = "."
|
||||
|
||||
locales = [
|
||||
"nb",
|
||||
"sv"
|
||||
]
|
||||
|
||||
[[paths]]
|
||||
reference = "src/main/resources/lang/en-GB/*.json"
|
||||
l10n = "src/main/resources/lang/{locale}/*.json"
|
||||
@@ -130,7 +130,7 @@ public final class RPGRegions extends JavaPlugin implements RPGRegionsAPI, Langu
|
||||
|
||||
public Gson getGson() {
|
||||
return new GsonBuilder()
|
||||
.registerTypeAdapter(DiscoveryReward.class, new AbstractAdapter<DiscoveryReward>("net.islandearth.rpgregions.rewards."))
|
||||
.registerTypeAdapter(DiscoveryReward.class, new AbstractAdapter<DiscoveryReward>(null))
|
||||
.setPrettyPrinting()
|
||||
.serializeNulls().create();
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package net.islandearth.rpgregions.gson;
|
||||
|
||||
import com.google.gson.*;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
@@ -8,14 +9,22 @@ public class AbstractAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T
|
||||
|
||||
private final String thePackage;
|
||||
|
||||
public AbstractAdapter(String thePackage) {
|
||||
/**
|
||||
* Constructs a new adapter.
|
||||
* If null is passed, the package will also be saved to the json file.
|
||||
* @param thePackage package class is located in
|
||||
*/
|
||||
public AbstractAdapter(@Nullable String thePackage) {
|
||||
this.thePackage = thePackage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(T src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
JsonObject result = new JsonObject();
|
||||
result.add("type", new JsonPrimitive(src.getClass().getSimpleName()));
|
||||
if (thePackage != null)
|
||||
result.add("type", new JsonPrimitive(src.getClass().getSimpleName()));
|
||||
else
|
||||
result.add("type", new JsonPrimitive(src.getClass().getPackage().getName() + "." + src.getClass().getSimpleName()));
|
||||
result.add("properties", context.serialize(src, src.getClass()));
|
||||
return result;
|
||||
}
|
||||
@@ -27,7 +36,10 @@ public class AbstractAdapter<T> implements JsonSerializer<T>, JsonDeserializer<T
|
||||
JsonElement element = jsonObject.get("properties");
|
||||
|
||||
try {
|
||||
return context.deserialize(element, Class.forName(thePackage + type));
|
||||
if (thePackage != null)
|
||||
return context.deserialize(element, Class.forName(thePackage + type));
|
||||
else
|
||||
return context.deserialize(element, Class.forName(type));
|
||||
} catch (ClassNotFoundException cnfe) {
|
||||
throw new JsonParseException("Unknown element type: " + type, cnfe);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user