mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-22 08:01:24 +01:00
Addd GSON enum type adapter for compatibility with Mohist
https://github.com/BentoBoxWorld/BentoBox/issues/1766
This commit is contained in:
parent
e472e07c34
commit
bedeb7c168
@ -17,6 +17,7 @@ import com.google.gson.reflect.TypeToken;
|
||||
import world.bentobox.bentobox.BentoBox;
|
||||
import world.bentobox.bentobox.api.flags.Flag;
|
||||
import world.bentobox.bentobox.database.json.adapters.BukkitObjectTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.EnumTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.FlagTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.ItemStackTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.LocationTypeAdapter;
|
||||
@ -44,13 +45,15 @@ public class BentoboxTypeAdapterFactory implements TypeAdapterFactory {
|
||||
/* (non-Javadoc)
|
||||
* @see com.google.gson.TypeAdapterFactory#create(com.google.gson.Gson, com.google.gson.reflect.TypeToken)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
@Override
|
||||
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
|
||||
Class<?> rawType = type.getRawType();
|
||||
if (Location.class.isAssignableFrom(rawType)) {
|
||||
// Use our current location adapter for backward compatibility
|
||||
return (TypeAdapter<T>) new LocationTypeAdapter();
|
||||
} else if (Enum.class.isAssignableFrom(rawType)) {
|
||||
return new EnumTypeAdapter(rawType);
|
||||
} else if (ItemStack.class.isAssignableFrom(rawType)) {
|
||||
// Use our current location adapter for backward compatibility
|
||||
return (TypeAdapter<T>) new ItemStackTypeAdapter();
|
||||
|
@ -0,0 +1,61 @@
|
||||
package world.bentobox.bentobox.database.json.adapters;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
|
||||
import com.google.common.collect.BiMap;
|
||||
import com.google.common.collect.HashBiMap;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.annotations.SerializedName;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
|
||||
/**
|
||||
* @author tastybento
|
||||
*
|
||||
* @param <T> enum class to be serialized
|
||||
*/
|
||||
public final class EnumTypeAdapter<T extends Enum<T>> extends TypeAdapter<T> {
|
||||
|
||||
|
||||
/**
|
||||
* Bimap to store name <-> enum references
|
||||
*/
|
||||
private final BiMap<String, T> enumMap = HashBiMap.create();
|
||||
|
||||
|
||||
public EnumTypeAdapter(Class<T> enumClass) {
|
||||
for (T value : enumClass.getEnumConstants()) {
|
||||
|
||||
String name = value.name();
|
||||
try {
|
||||
SerializedName annotation = enumClass.getField(name).getAnnotation(SerializedName.class);
|
||||
|
||||
if (annotation != null) {
|
||||
Arrays.stream(annotation.alternate()).forEach(s -> enumMap.put(s, value));
|
||||
// Reset name
|
||||
name = annotation.value();
|
||||
}
|
||||
|
||||
} catch (NoSuchFieldException e) {
|
||||
// Ignore
|
||||
}
|
||||
|
||||
enumMap.put(name, value);
|
||||
}
|
||||
}
|
||||
|
||||
@Override public T read(JsonReader input) throws IOException {
|
||||
if (JsonToken.NULL.equals(input.peek())) {
|
||||
input.nextNull();
|
||||
return null;
|
||||
}
|
||||
return enumMap.get(input.nextString());
|
||||
}
|
||||
|
||||
@Override public void write(JsonWriter output, T enumValue) throws IOException {
|
||||
output.value(enumValue != null ? enumMap.inverse().get(enumValue) : null);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user