From 4d0ddf591b3e4eab818315228453a518c7326365 Mon Sep 17 00:00:00 2001 From: NorbiPeti Date: Wed, 10 Aug 2016 11:16:35 +0200 Subject: [PATCH] Fixed generalized serializers --- src/io/github/norbipeti/chat/server/Main.java | 9 ++------ .../chat/server/data/DataManager.java | 1 + .../data/LoaderCollectionSerializer.java | 2 +- .../norbipeti/chat/server/data/LoaderRef.java | 2 -- .../chat/server/data/LoaderRefSerializer.java | 21 ++++++++++++------- 5 files changed, 18 insertions(+), 17 deletions(-) diff --git a/src/io/github/norbipeti/chat/server/Main.java b/src/io/github/norbipeti/chat/server/Main.java index 311e090..1907b06 100644 --- a/src/io/github/norbipeti/chat/server/Main.java +++ b/src/io/github/norbipeti/chat/server/Main.java @@ -40,14 +40,9 @@ public class Main { if (Modifier.isAbstract(data.getModifiers())) continue; gsonBuilder.registerTypeAdapter(new DataType(LoaderCollection.class, data), - new LoaderCollectionSerializer()); // TODO: Test + new LoaderCollectionSerializer()); + gsonBuilder.registerTypeAdapter(new DataType(LoaderRef.class, data), new LoaderRefSerializer()); } - gsonBuilder.registerTypeAdapter(new TypeToken>() { - }.getType(), new LoaderRefSerializer()); - gsonBuilder.registerTypeAdapter(new TypeToken>() { - }.getType(), new LoaderRefSerializer()); - gsonBuilder.registerTypeAdapter(new TypeToken>() { - }.getType(), new LoaderRefSerializer()); gson = gsonBuilder.create(); /* * User user = new User(); user.setName("asd"); user.setEmail("test@test.com"); User user2 = new User(); user2.setName("Teszt"); user2.setEmail("test2@test.com"); // user = diff --git a/src/io/github/norbipeti/chat/server/data/DataManager.java b/src/io/github/norbipeti/chat/server/data/DataManager.java index 225f69f..627a040 100644 --- a/src/io/github/norbipeti/chat/server/data/DataManager.java +++ b/src/io/github/norbipeti/chat/server/data/DataManager.java @@ -56,6 +56,7 @@ public final class DataManager { private static Map cache = new HashMap<>(); // TODO: Remove objects from the cache over time // TODO: Save the object when it gets removed from cache and when the app stops + // TODO: Handle unloading of used objects (prevent detached objects) @SuppressWarnings("unchecked") private static T loadFromFile(File file, Class cl) { diff --git a/src/io/github/norbipeti/chat/server/data/LoaderCollectionSerializer.java b/src/io/github/norbipeti/chat/server/data/LoaderCollectionSerializer.java index a3254e3..4a4490c 100644 --- a/src/io/github/norbipeti/chat/server/data/LoaderCollectionSerializer.java +++ b/src/io/github/norbipeti/chat/server/data/LoaderCollectionSerializer.java @@ -19,7 +19,7 @@ import io.github.norbipeti.chat.server.db.domain.SavedData; public class LoaderCollectionSerializer extends TypeAdapter> { // private static final Type returnType = getReturnType(); - // TODO: http://stackoverflow.com/a/17300227 + // http://stackoverflow.com/a/17300227 @Override public void write(JsonWriter out, LoaderCollection value) throws IOException { if (value == null) { diff --git a/src/io/github/norbipeti/chat/server/data/LoaderRef.java b/src/io/github/norbipeti/chat/server/data/LoaderRef.java index 82432fe..02948db 100644 --- a/src/io/github/norbipeti/chat/server/data/LoaderRef.java +++ b/src/io/github/norbipeti/chat/server/data/LoaderRef.java @@ -1,7 +1,5 @@ package io.github.norbipeti.chat.server.data; -import java.io.Serializable; - import io.github.norbipeti.chat.server.db.domain.SavedData; /** diff --git a/src/io/github/norbipeti/chat/server/data/LoaderRefSerializer.java b/src/io/github/norbipeti/chat/server/data/LoaderRefSerializer.java index 3160da3..60c3fac 100644 --- a/src/io/github/norbipeti/chat/server/data/LoaderRefSerializer.java +++ b/src/io/github/norbipeti/chat/server/data/LoaderRefSerializer.java @@ -9,10 +9,10 @@ import com.google.gson.stream.JsonWriter; import io.github.norbipeti.chat.server.db.domain.SavedData; // @SuppressWarnings("rawtypes") -public class LoaderRefSerializer extends TypeAdapter> { +public class LoaderRefSerializer extends TypeAdapter> { @Override - public void write(JsonWriter out, LoaderRef value) throws IOException { + public void write(JsonWriter out, LoaderRef value) throws IOException { if (value == null) { out.nullValue(); return; @@ -27,7 +27,7 @@ public class LoaderRefSerializer extends TypeAdapter read(JsonReader in) throws IOException { + public LoaderRef read(JsonReader in) throws IOException { if (in.peek().equals(JsonToken.NULL)) { in.nextNull(); return null; @@ -39,16 +39,23 @@ public class LoaderRefSerializer extends TypeAdapter cl; + Class cl; try { - cl = (Class) Class.forName(DataManager.getPackageName() + "." + in.nextString()); + cl = (Class) Class.forName(DataManager.getPackageName() + "." + in.nextString()); } catch (ClassNotFoundException e) { e.printStackTrace(); return null; } - LoaderRef col = new LoaderRef(cl, id); + LoaderRef ref; + try { + ref = LoaderRef.class.getDeclaredConstructor(Class.class).newInstance(cl); + } catch (Exception e) { + e.printStackTrace(); + return null; + } + ref.id = id; in.endObject(); - return col; + return ref; } }