From f4b3924dc345832060d67640466ec760d506a044 Mon Sep 17 00:00:00 2001 From: Olof Larsson Date: Tue, 10 Sep 2013 02:40:57 +0200 Subject: [PATCH] We wanna serialize JsonElements as JsonElements, duh --- src/com/massivecraft/mcore/MCore.java | 9 +++++ .../mcore/adapter/JsonElementAdapter.java | 37 +++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 src/com/massivecraft/mcore/adapter/JsonElementAdapter.java diff --git a/src/com/massivecraft/mcore/MCore.java b/src/com/massivecraft/mcore/MCore.java index a5d1d0a2..5d4899a4 100644 --- a/src/com/massivecraft/mcore/MCore.java +++ b/src/com/massivecraft/mcore/MCore.java @@ -14,6 +14,7 @@ import org.bukkit.inventory.PlayerInventory; import com.massivecraft.mcore.adapter.InventoryAdapter; import com.massivecraft.mcore.adapter.ItemStackAdapter; +import com.massivecraft.mcore.adapter.JsonElementAdapter; import com.massivecraft.mcore.adapter.ObjectIdAdapter; import com.massivecraft.mcore.adapter.PlayerInventoryAdapter; import com.massivecraft.mcore.adapter.UUIDAdapter; @@ -35,6 +36,10 @@ import com.massivecraft.mcore.util.TimeUnit; import com.massivecraft.mcore.xlib.bson.types.ObjectId; import com.massivecraft.mcore.xlib.gson.Gson; import com.massivecraft.mcore.xlib.gson.GsonBuilder; +import com.massivecraft.mcore.xlib.gson.JsonArray; +import com.massivecraft.mcore.xlib.gson.JsonNull; +import com.massivecraft.mcore.xlib.gson.JsonObject; +import com.massivecraft.mcore.xlib.gson.JsonPrimitive; public class MCore extends MPlugin { @@ -66,6 +71,10 @@ public class MCore extends MPlugin .setPrettyPrinting() .disableHtmlEscaping() .excludeFieldsWithModifiers(Modifier.TRANSIENT) + .registerTypeAdapter(JsonNull.class, JsonElementAdapter.get()) + .registerTypeAdapter(JsonPrimitive.class, JsonElementAdapter.get()) + .registerTypeAdapter(JsonArray.class, JsonElementAdapter.get()) + .registerTypeAdapter(JsonObject.class, JsonElementAdapter.get()) .registerTypeAdapter(ObjectId.class, ObjectIdAdapter.get()) .registerTypeAdapter(UUID.class, UUIDAdapter.get()) .registerTypeAdapter(ItemStack.class, ItemStackAdapter.get()) diff --git a/src/com/massivecraft/mcore/adapter/JsonElementAdapter.java b/src/com/massivecraft/mcore/adapter/JsonElementAdapter.java new file mode 100644 index 00000000..ef636ee9 --- /dev/null +++ b/src/com/massivecraft/mcore/adapter/JsonElementAdapter.java @@ -0,0 +1,37 @@ +package com.massivecraft.mcore.adapter; + +import java.lang.reflect.Type; + +import com.massivecraft.mcore.xlib.gson.JsonDeserializationContext; +import com.massivecraft.mcore.xlib.gson.JsonDeserializer; +import com.massivecraft.mcore.xlib.gson.JsonElement; +import com.massivecraft.mcore.xlib.gson.JsonParseException; +import com.massivecraft.mcore.xlib.gson.JsonSerializationContext; +import com.massivecraft.mcore.xlib.gson.JsonSerializer; + +public class JsonElementAdapter implements JsonDeserializer, JsonSerializer +{ + // -------------------------------------------- // + // INSTANCE & CONSTRUCT + // -------------------------------------------- // + + private static JsonElementAdapter i = new JsonElementAdapter(); + public static JsonElementAdapter get() { return i; } + + // -------------------------------------------- // + // OVERRIDE + // -------------------------------------------- // + + @Override + public JsonElement serialize(JsonElement src, Type typeOfSrc, JsonSerializationContext context) + { + return src; + } + + @Override + public JsonElement deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException + { + return json; + } + +}