From 755452cd3c76597fb9fd2fa8da6f4b61c6d5f853 Mon Sep 17 00:00:00 2001 From: tastybento Date: Wed, 3 Jul 2024 15:43:17 -0700 Subject: [PATCH] Added support for serializing Pairs --- .../json/BentoboxTypeAdapterFactory.java | 4 ++ .../json/adapters/PairTypeAdapter.java | 41 +++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 src/main/java/world/bentobox/bentobox/database/json/adapters/PairTypeAdapter.java diff --git a/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java b/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java index a8faa19e1..9f90a4793 100644 --- a/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java +++ b/src/main/java/world/bentobox/bentobox/database/json/BentoboxTypeAdapterFactory.java @@ -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) new WorldTypeAdapter(); } else if (Vector.class.isAssignableFrom(rawType)) { return (TypeAdapter) new VectorTypeAdapter(); + } else if (Pair.class.isAssignableFrom(rawType)) { + return (TypeAdapter) new PairTypeAdapter<>(); } else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) { // This covers a lot of Bukkit objects return (TypeAdapter) new BukkitObjectTypeAdapter(gson.getAdapter(Map.class)); diff --git a/src/main/java/world/bentobox/bentobox/database/json/adapters/PairTypeAdapter.java b/src/main/java/world/bentobox/bentobox/database/json/adapters/PairTypeAdapter.java new file mode 100644 index 000000000..20d0fe7cb --- /dev/null +++ b/src/main/java/world/bentobox/bentobox/database/json/adapters/PairTypeAdapter.java @@ -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 +public class PairTypeAdapter extends TypeAdapter> { + + @Override + public void write(JsonWriter out, Pair 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 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); + } +} \ No newline at end of file