Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support for serializing Pairs #2421

Merged
merged 2 commits into from
Jul 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package world.bentobox.bentobox.database.json;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Map;

import org.bukkit.Location;
Expand All @@ -25,9 +27,11 @@
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;


/**
Expand Down Expand Up @@ -74,6 +78,13 @@ public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
return (TypeAdapter<T>) new WorldTypeAdapter();
} else if (Vector.class.isAssignableFrom(rawType)) {
return (TypeAdapter<T>) new VectorTypeAdapter();
} else if (Pair.class.isAssignableFrom(rawType)) {
// Add Pair handling here with type safety
Type pairType = type.getType();
ParameterizedType parameterizedType = (ParameterizedType) pairType;
Type xType = parameterizedType.getActualTypeArguments()[0];
Type zType = parameterizedType.getActualTypeArguments()[1];
return (TypeAdapter<T>) new PairTypeAdapter<>(xType, zType);
} else if (ConfigurationSerializable.class.isAssignableFrom(rawType)) {
// This covers a lot of Bukkit objects
return (TypeAdapter<T>) new BukkitObjectTypeAdapter(gson.getAdapter(Map.class));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package world.bentobox.bentobox.database.json.adapters;

import java.io.IOException;
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;

public class PairTypeAdapter<X, Z> extends TypeAdapter<Pair<X, Z>> {
private final Type xType;
private final Type zType;

public PairTypeAdapter(Type xType, Type zType) {
this.xType = xType;
this.zType = zType;
}

@Override
public void write(JsonWriter out, Pair<X, Z> pair) throws IOException {
out.beginObject();
out.name("x");
Gson gson = new Gson();
gson.toJson(pair.getKey(), xType, out);
out.name("z");
gson.toJson(pair.getValue(), zType, out);
out.endObject();
}

@Override
public Pair<X, Z> read(JsonReader in) throws IOException {
X x = null;
Z z = null;

in.beginObject();
while (in.hasNext()) {
String name = in.nextName();
if (name.equals("x")) {
x = new Gson().fromJson(in, xType);
} else if (name.equals("z")) {
z = new Gson().fromJson(in, zType);
}
}
in.endObject();
return new Pair<>(x, z);
}
}
Loading