Improved Json record support (Gson 2.10 natively supports it) + Added ItemStack Json support
- Extract RecordTypeAdapter to its own file + only use it if Gson library does not support it (it does since 2.10, but we are unsure of which version is actually used in paper/bungee/other) - new ItemStackAdapter to support Json (de)serializing of Bukkit ItemStack.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
package fr.pandacube.lib.paper;
|
||||
|
||||
import fr.pandacube.lib.paper.json.PaperJson;
|
||||
import fr.pandacube.lib.paper.modules.PerformanceAnalysisManager;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
@@ -9,6 +10,7 @@ public class PandaLibPaper {
|
||||
|
||||
public static void onLoad(Plugin plugin) {
|
||||
PandaLibPaper.plugin = plugin;
|
||||
PaperJson.init();
|
||||
}
|
||||
|
||||
public static void onEnable() {
|
||||
|
@@ -0,0 +1,34 @@
|
||||
package fr.pandacube.lib.paper.json;
|
||||
|
||||
import com.google.gson.JsonDeserializationContext;
|
||||
import com.google.gson.JsonDeserializer;
|
||||
import com.google.gson.JsonElement;
|
||||
import com.google.gson.JsonParseException;
|
||||
import com.google.gson.JsonSerializationContext;
|
||||
import com.google.gson.JsonSerializer;
|
||||
import com.google.gson.TypeAdapterFactory;
|
||||
import com.google.gson.internal.bind.TreeTypeAdapter;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.Map;
|
||||
|
||||
/* package */ class ItemStackAdapter implements JsonSerializer<ItemStack>, JsonDeserializer<ItemStack> {
|
||||
|
||||
private static final TypeToken<ItemStack> ITEMSTACK_TYPE = TypeToken.get(ItemStack.class);
|
||||
public static final TypeAdapterFactory FACTORY = TreeTypeAdapter.newFactoryWithMatchRawType(ITEMSTACK_TYPE, new ItemStackAdapter());
|
||||
|
||||
private static final TypeToken<Map<String, Object>> MAP_STR_OBJ_TYPE = new TypeToken<>() { };
|
||||
|
||||
|
||||
@Override
|
||||
public ItemStack deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
return ItemStack.deserialize(context.deserialize(json, MAP_STR_OBJ_TYPE.getType()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public JsonElement serialize(ItemStack src, Type typeOfSrc, JsonSerializationContext context) {
|
||||
return context.serialize(src.serialize(), MAP_STR_OBJ_TYPE.getType());
|
||||
}
|
||||
}
|
@@ -0,0 +1,16 @@
|
||||
package fr.pandacube.lib.paper.json;
|
||||
|
||||
import fr.pandacube.lib.core.json.Json;
|
||||
|
||||
/**
|
||||
* Utility class to register Json adapters related to paper API classes.
|
||||
*/
|
||||
public class PaperJson {
|
||||
|
||||
/**
|
||||
* Registers Json adapters related to paper API classes.
|
||||
*/
|
||||
public static void init() {
|
||||
Json.registerTypeAdapterFactory(ItemStackAdapter.FACTORY);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user