mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2025-01-08 01:17:41 +01:00
Added support for serializing Pairs
This commit is contained in:
parent
5e0d80e514
commit
755452cd3c
@ -25,9 +25,11 @@ import world.bentobox.bentobox.database.json.adapters.FlagTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.ItemStackTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.LocationTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.MaterialTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.PairTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.PotionEffectTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.VectorTypeAdapter;
|
||||
import world.bentobox.bentobox.database.json.adapters.WorldTypeAdapter;
|
||||
import world.bentobox.bentobox.util.Pair;
|
||||
|
||||
|
||||
/**
|
||||
@ -74,6 +76,8 @@ public class BentoboxTypeAdapterFactory implements TypeAdapterFactory {
|
||||
return (TypeAdapter<T>) new WorldTypeAdapter();
|
||||
} else if (Vector.class.isAssignableFrom(rawType)) {
|
||||
return (TypeAdapter<T>) new VectorTypeAdapter();
|
||||
} else if (Pair.class.isAssignableFrom(rawType)) {
|
||||
return (TypeAdapter<T>) new PairTypeAdapter<>();
|
||||
} else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) {
|
||||
// This covers a lot of Bukkit objects
|
||||
return (TypeAdapter<T>) new BukkitObjectTypeAdapter(gson.getAdapter(Map.class));
|
||||
|
@ -0,0 +1,41 @@
|
||||
package world.bentobox.bentobox.database.json.adapters;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
|
||||
import world.bentobox.bentobox.util.Pair;
|
||||
|
||||
// Custom TypeAdapter for Pair<X, Z>
|
||||
public class PairTypeAdapter<X, Z> extends TypeAdapter<Pair<X, Z>> {
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter out, Pair<X, Z> pair) throws IOException {
|
||||
if (pair == null || pair.getKey() == null || pair.getValue() == null) {
|
||||
return;
|
||||
}
|
||||
out.beginArray();
|
||||
out.value(new Gson().toJson(pair.getKey()));
|
||||
out.value(new Gson().toJson(pair.getValue()));
|
||||
out.endArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Pair<X, Z> read(JsonReader in) throws IOException {
|
||||
in.beginArray();
|
||||
Type typeX = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[0];
|
||||
X x = new Gson().fromJson(in.nextString(), typeX);
|
||||
Type typeZ = ((ParameterizedType) this.getClass().getGenericSuperclass()).getActualTypeArguments()[1];
|
||||
Z z = new Gson().fromJson(in.nextString(), typeZ);
|
||||
in.endArray();
|
||||
if (x == null || z == null) {
|
||||
return null;
|
||||
}
|
||||
return new Pair<>(x, z);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user