Skip to content

Commit

Permalink
Addd GSON enum type adapter for compatibility with Mohist
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed May 29, 2021
1 parent e472e07 commit bedeb7c
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
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;
Expand Down Expand Up @@ -44,13 +45,15 @@ public BentoboxTypeAdapterFactory(BentoBox plugin) {
/* (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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit bedeb7c

Please sign in to comment.