ソースを参照

minor changes

Teh-Hai Julian Zheng 7 年 前
コミット
9decd5cfb9

+ 0 - 25
src/Serializer/CpsObjectDeserializer.java

@@ -1,25 +0,0 @@
-package Serializer;
-
-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 classes.AbstractCpsObject;
-
-public class CpsObjectDeserializer implements JsonDeserializer<AbstractCpsObject>{
-
-	@Override
-	public AbstractCpsObject deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2){
-		// TODO Auto-generated method stub
-		try {
-			
-		} catch (Exception e) {
-			// TODO: handle exception
-		}
-		return null;
-	}
-
-}

+ 0 - 20
src/Serializer/CpsObjectSerializer.java

@@ -1,20 +0,0 @@
-package Serializer;
-
-import java.lang.reflect.Type;
-
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-import com.google.gson.JsonSerializationContext;
-import com.google.gson.JsonSerializer;
-
-import classes.AbstractCpsObject;
-
-public class CpsObjectSerializer implements JsonSerializer<AbstractCpsObject>{
-
-	@Override
-	public JsonElement serialize(AbstractCpsObject arg0, Type arg1, JsonSerializationContext arg2) {
-		// TODO Auto-generated method stub
-		return new JsonPrimitive(arg0.getID());
-	}
-
-}

+ 0 - 20
src/Serializer/PositionSerializer.java

@@ -1,20 +0,0 @@
-package Serializer;
-
-import java.lang.reflect.Type;
-
-import com.google.gson.JsonElement;
-import com.google.gson.JsonPrimitive;
-import com.google.gson.JsonSerializationContext;
-import com.google.gson.JsonSerializer;
-
-import classes.Position;
-
-public class PositionSerializer implements JsonSerializer<Position> {
-
-	@Override
-	public JsonElement serialize(Position arg0, Type arg1, JsonSerializationContext arg2) {
-		// TODO Auto-generated method stub
-		return new JsonPrimitive(arg0.x + ":" + arg0.y);
-	}
-
-}

+ 49 - 0
src/TypeAdapter/AbstractCpsObjectAdapter.java

@@ -0,0 +1,49 @@
+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.AbstractCpsObject;
+
+public class AbstractCpsObjectAdapter implements JsonSerializer<AbstractCpsObject>, JsonDeserializer<AbstractCpsObject>{
+
+	
+	@Override
+	public JsonElement serialize(AbstractCpsObject 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()));
+
+		return object;
+	}
+	
+	
+	@Override
+	public AbstractCpsObject 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);
+		}
+
+	}
+
+
+
+}

+ 15 - 5
src/Serializer/PositionDeserializer.java → src/TypeAdapter/PositionAdapter.java

@@ -1,29 +1,39 @@
-package Serializer;
+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.JsonParseException;
+import com.google.gson.JsonPrimitive;
+import com.google.gson.JsonSerializationContext;
+import com.google.gson.JsonSerializer;
+
 import classes.Position;
 
-public class PositionDeserializer implements JsonDeserializer<Position> {
+public class PositionAdapter implements JsonSerializer<Position>, JsonDeserializer<Position> {
 
 	@Override
-	public Position deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) {
+	public JsonElement serialize(Position arg0, Type arg1, JsonSerializationContext arg2) {
 		// TODO Auto-generated method stub
+		return new JsonPrimitive(arg0.x + ":" + arg0.y);
+	}
 
+	@Override
+	public Position deserialize(JsonElement arg0, Type arg1, JsonDeserializationContext arg2) {
+		// TODO Auto-generated method stub
 		try {
 			String arg = arg0.getAsString();
 			int mid = arg.indexOf(':');
 			int x = Integer.parseInt(arg.substring(0, mid));
 			int y = Integer.parseInt(arg.substring(mid + 1, arg.length() - 1));
 			return new Position(x, y);
-			
+
 		} catch (Exception e) {
 			// TODO: handle exception
 		}
-		return new Position(-1,-1);
+		return new Position(-1, -1);
 	}
 
 }

+ 3 - 0
src/classes/Category.java

@@ -3,6 +3,8 @@ package classes;
 import java.util.ArrayList;
 import java.util.HashMap;
 
+import com.google.gson.annotations.Expose;
+
 /**
  * Class "Category" performs the functionality of listing elements into groups.
  * Each Category contains an ArrayList of CpsObjects, a name and a HashMap of
@@ -16,6 +18,7 @@ public class Category {
 	// objects: is a ArrayList of all Objects that belongs to the Category
 	private ArrayList<AbstractCpsObject> objects;
 	// name: is a String chosen by the User
+	@Expose
 	private String name; 
 	// ObjIdx: Index of each Category that corresponds to the order in the tree
 	private HashMap<String, Integer> objIdx;

+ 45 - 37
src/ui/controller/StoreController.java

@@ -10,8 +10,10 @@ import org.json.simple.JSONObject;
 
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
+import com.google.gson.JsonElement;
+import com.google.gson.JsonObject;
 
-import Serializer.PositionSerializer;
+import TypeAdapter.AbstractCpsObjectAdapter;
 import classes.Category;
 import classes.CpsEdge;
 import classes.AbstractCpsObject;
@@ -55,26 +57,28 @@ public class StoreController {
 	 */
 	public void writeSaveFile(String path) throws IOException {
 
-		Gson gson = new GsonBuilder().serializeNulls().excludeFieldsWithoutExposeAnnotation().registerTypeAdapter(Position.class, new PositionSerializer()).create();
-		
+		GsonBuilder builder = new GsonBuilder();
+		builder.registerTypeAdapter(AbstractCpsObject.class, new AbstractCpsObjectAdapter()).serializeNulls()
+				.setPrettyPrinting();
+		Gson gson = builder.create();
+
 		JSONObject json = new JSONObject();
-		int i = 1;
 
-//		json.put("MODE", "ALL");
-//		json.put("ID", IdCounter.getCounter());
-//		json.put("SIZEX", model.getCanvasX());
-//		json.put("SIZEY", model.getCanvasY());
-//		writeCategory(json);
-//		writeCategoryObjects(json);
-//		writeCanvasObjects(json);
-//		writeCategoryElements(json);
-//		writeCanvasElements(json);
-//		writeEdges(json);
-//		writeElementGraph(json);
-		
+		// json.put("MODE", "ALL");
+		// json.put("ID", IdCounter.getCounter());
+		// json.put("SIZEX", model.getCanvasX());
+		// json.put("SIZEY", model.getCanvasY());
+		// writeCategory(gson, json);
+		// writeCategoryObjects(gson, json);
+		// writeCanvasObjects(json);
+		// writeCategoryElements(json);
+		// writeCanvasElements(json);
+		// writeEdges(json);
+		// writeElementGraph(json);
+		int i = 1;
 		for (AbstractCpsObject cps : model.getObjectsOnCanvas()) {
-			gson.toJson(cps);
-			json.put("CVS" + i++, gson.toJson(cps));
+			String k = gson.toJson(cps);
+			System.out.println(k);
 		}
 
 		FileWriter writer = new FileWriter(path);
@@ -126,8 +130,8 @@ public class StoreController {
 
 		json.put("MODE", "CATEGORY");
 		// eventuell muss man ID auch Speichern
-		writeCategory(json);
-		writeCategoryObjects(json);
+		// writeCategory(gson, json);
+		// writeCategoryObjects(gson, json);
 		writeCategoryElements(json);
 
 		FileWriter writer = new FileWriter(path);
@@ -141,39 +145,39 @@ public class StoreController {
 	/**
 	 * writes all Categories into a JSONObject.
 	 * 
+	 * @param gson
+	 * 
 	 * @param json
 	 *            JSON Object
 	 * @throws IOException
 	 *             exception
 	 */
-	public void writeCategory(JSONObject json) {
-		JSONArray arr = new JSONArray();
+	public void writeCategory(Gson gson, JSONObject json) {
+		int i = 1;
 
 		for (Category cat : model.getCategories())
-			arr.add(cat.getName());
-
-		json.put("CG", arr);
+			json.put("CG" + i++, gson.toJson(cat));
 	}
 
 	/**
 	 * writes all Objects in Category into a JSONObject.
 	 * 
+	 * @param gson
+	 * 
 	 * @param json
 	 *            JSON Object
 	 */
-	public void writeCategoryObjects(JSONObject json) {
+	public void writeCategoryObjects(Gson gson, JSONObject json) {
 
-		JSONArray arr = new JSONArray();
 		int i = 1;
 
 		for (Category cats : model.getCategories())
 			for (AbstractCpsObject cps : cats.getObjects()) {
-				arr.add(getObjectType(cps));
-				arr.add(cps.getSav());
-				arr.add(cps.getObjName());
-				arr.add(cps.getImage());
-				json.put("CGO" + i++, arr);
-				arr = new JSONArray();
+				// arr.add(cps.getSav());
+				// arr.add(cps.getObjName());
+				// arr.add(cps.getImage());
+				json.put("CGO" + i++, gson.toJson(cps));
+
 			}
 	}
 
@@ -229,7 +233,8 @@ public class StoreController {
 	/**
 	 * Write Canvas Elements.
 	 * 
-	 * @param json JSON Objects
+	 * @param json
+	 *            JSON Objects
 	 */
 	public void writeCanvasElements(JSONObject json) {
 
@@ -253,7 +258,8 @@ public class StoreController {
 	/**
 	 * write all Edges into a JSONObject.
 	 * 
-	 * @param json JSON Object
+	 * @param json
+	 *            JSON Object
 	 */
 	public void writeEdges(JSONObject json) {
 
@@ -273,7 +279,8 @@ public class StoreController {
 	/**
 	 * writes the Graph from all Elements into a JSONObject.
 	 * 
-	 * @param json JSON Object
+	 * @param json
+	 *            JSON Object
 	 */
 	public void writeElementGraph(JSONObject json) {
 
@@ -325,7 +332,8 @@ public class StoreController {
 	/**
 	 * Return the Object Type.
 	 * 
-	 * @param cps AbstractCpsObject
+	 * @param cps
+	 *            AbstractCpsObject
 	 * @return The Object Type
 	 */
 	public String getObjectType(AbstractCpsObject cps) {