package editor.serialization;

import java.lang.reflect.Type;

import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import editor.database.card.Card;
import editor.gui.MainFrame;

/**
 * A JSON serializer and deserializer for {@link Card} using its
 * {@link Card#multiverseid()}.  Since this doesn't serialize the fields of
 * Card itself, it is intended to be used to serialize structures containing
 * them after the database has been loaded.
 * 
 * @author Alec Roelke
 */
public class CardAdapter implements JsonSerializer<Card>, JsonDeserializer<Card>
{
    @Override
    public Card deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException
    {
        if (MainFrame.inventory().contains(json.getAsLong()))
            return MainFrame.inventory().get(json.getAsLong());
        else
            throw new JsonParseException("no card with multiverseid " + json.getAsLong() + " exists");
    }

    @Override
    public JsonElement serialize(Card src, Type typeOfSrc, JsonSerializationContext context)
    {
        return new JsonPrimitive(src.multiverseid().get(0));
    }
    
}
@@ -211,30 +211,22 @@
 son.Json
-ParseException
+Object
 ;%0Aimport
@@ -247,24 +247,29 @@
 on.JsonP
-rimitive
+arseException
 ;%0Aimport
@@ -933,24 +933,140 @@
 ption%0A    %7B%0A
+        JsonObject obj = json.getAsJsonObject();%0A%0A        long multiverseid = obj.get(%22multiverseid%22).getAsLong();%0A%0A
         if (
@@ -1096,32 +1096,28 @@
 ontains(
-json.getAsLong()
+multiverseid
 ))%0A     
@@ -1156,32 +1156,28 @@
 y().get(
-json.getAsLong()
+multiverseid
 );%0A     
@@ -1260,24 +1260,20 @@
 %22 + 
-json.getAsLong()
+multiverseid
  + %22
@@ -1415,59 +1415,246 @@
     
-return new JsonPrimitive(src.multiverseid().get(0))
+JsonObject card = new JsonObject();%0A        card.addProperty(%22multiverseid%22, src.multiverseid().get(0));%0A        card.addProperty(%22name%22, src.unifiedName());%0A        card.addProperty(%22expansion%22, src.expansion().name);%0A        return card
 ;%0A  

Add more fields for IDing cards