mirror of
https://gitlab.com/SamB440/rpgregions-2.git
synced 2025-12-29 03:39:08 +00:00
28 lines
906 B
Java
28 lines
906 B
Java
package net.islandearth.rpgregions.gson;
|
|
|
|
import com.google.gson.*;
|
|
import com.google.gson.reflect.TypeToken;
|
|
import org.bukkit.inventory.ItemStack;
|
|
|
|
import java.lang.reflect.Type;
|
|
import java.util.Map;
|
|
|
|
public class ItemStackAdapter implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
|
|
|
|
private final Gson gson;
|
|
|
|
public ItemStackAdapter() {
|
|
this.gson = new GsonBuilder().create();
|
|
}
|
|
|
|
@Override
|
|
public ItemStack deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
|
|
Map<String, Object> map = gson.fromJson(jsonElement, new TypeToken<Map<String, Object>>(){}.getType());
|
|
return ItemStack.deserialize(map);
|
|
}
|
|
|
|
@Override
|
|
public JsonElement serialize(ItemStack itemStack, Type type, JsonSerializationContext context) {
|
|
return gson.toJsonTree(itemStack.serialize());
|
|
}
|
|
} |