Fixed generalized serializers

This commit is contained in:
Norbi Peti 2016-08-10 11:16:35 +02:00
parent d6834c2ae4
commit 4d0ddf591b
5 changed files with 18 additions and 17 deletions

View file

@ -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<LoaderRef<Conversation>>() {
}.getType(), new LoaderRefSerializer<Conversation>());
gsonBuilder.registerTypeAdapter(new TypeToken<LoaderRef<MessageChunk>>() {
}.getType(), new LoaderRefSerializer<MessageChunk>());
gsonBuilder.registerTypeAdapter(new TypeToken<LoaderRef<User>>() {
}.getType(), new LoaderRefSerializer<User>());
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 =

View file

@ -56,6 +56,7 @@ public final class DataManager {
private static Map<File, Object> 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 extends SavedData> T loadFromFile(File file, Class<T> cl) {

View file

@ -19,7 +19,7 @@ import io.github.norbipeti.chat.server.db.domain.SavedData;
public class LoaderCollectionSerializer extends TypeAdapter<LoaderCollection<?>> {
// 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) {

View file

@ -1,7 +1,5 @@
package io.github.norbipeti.chat.server.data;
import java.io.Serializable;
import io.github.norbipeti.chat.server.db.domain.SavedData;
/**

View file

@ -9,10 +9,10 @@ import com.google.gson.stream.JsonWriter;
import io.github.norbipeti.chat.server.db.domain.SavedData;
// @SuppressWarnings("rawtypes")
public class LoaderRefSerializer<T extends SavedData> extends TypeAdapter<LoaderRef<T>> {
public class LoaderRefSerializer extends TypeAdapter<LoaderRef<?>> {
@Override
public void write(JsonWriter out, LoaderRef<T> 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<T extends SavedData> extends TypeAdapter<Loader
// @SuppressWarnings("unchecked")
@SuppressWarnings("unchecked")
@Override
public LoaderRef<T> 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<T extends SavedData> extends TypeAdapter<Loader
new Exception("Error: Next isn't \"class\"").printStackTrace(); // TODO: Same as at LoaderCollectionSerializer
return null;
}
Class<T> cl;
Class<? extends SavedData> cl;
try {
cl = (Class<T>) Class.forName(DataManager.getPackageName() + "." + in.nextString());
cl = (Class<? extends SavedData>) Class.forName(DataManager.getPackageName() + "." + in.nextString());
} catch (ClassNotFoundException e) {
e.printStackTrace();
return null;
}
LoaderRef<T> col = new LoaderRef<T>(cl, id);
LoaderRef<? extends SavedData> 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;
}
}