|
@@ -0,0 +1,45 @@
|
|
|
+package TypeAdapter;
|
|
|
+
|
|
|
+import java.lang.reflect.Type;
|
|
|
+
|
|
|
+import com.google.gson.JsonDeserializationContext;
|
|
|
+import com.google.gson.JsonDeserializer;
|
|
|
+import com.google.gson.JsonElement;
|
|
|
+import com.google.gson.JsonObject;
|
|
|
+import com.google.gson.JsonParseException;
|
|
|
+import com.google.gson.JsonPrimitive;
|
|
|
+import com.google.gson.JsonSerializationContext;
|
|
|
+import com.google.gson.JsonSerializer;
|
|
|
+
|
|
|
+import classes.CpsEdge;
|
|
|
+
|
|
|
+public class CpsEdgeAdapter implements JsonSerializer<CpsEdge>, JsonDeserializer<CpsEdge> {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public JsonElement serialize(CpsEdge arg0, Type arg1, JsonSerializationContext arg2) {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ JsonObject object = new JsonObject();
|
|
|
+ object.add("type", new JsonPrimitive(arg0.getClass().getSimpleName()));
|
|
|
+ object.add("properties", arg2.serialize(arg0, arg0.getClass()));
|
|
|
+ object.add("a", new JsonPrimitive(arg0.getA().getID()));
|
|
|
+ object.add("b", new JsonPrimitive(arg0.getB().getID()));
|
|
|
+
|
|
|
+ return object;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public CpsEdge deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) throws JsonParseException {
|
|
|
+ // TODO Auto-generated method stub
|
|
|
+ JsonObject object = arg0.getAsJsonObject();
|
|
|
+ String type = object.get("type").getAsString();
|
|
|
+ JsonElement element = object.get("properties");
|
|
|
+ try {
|
|
|
+ String packageName = "classes.";
|
|
|
+ return arg2.deserialize(element, Class.forName(packageName + type));
|
|
|
+ } catch (ClassNotFoundException cnfe) {
|
|
|
+ // TODO: handle exception
|
|
|
+ throw new JsonParseException("Unknown element type: " + type, cnfe);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+}
|