blob: 335ef34cbd6b9b291c55d12f1ba61383f7cb2f4c (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
package de.blinkt.openvpn.core.connection;
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.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import java.lang.reflect.Type;
// Adapter for Gson used to serialize and deserialize abstract Connection class, adds a property about the implemented class
public class ConnectionAdapter implements JsonSerializer<Connection>, JsonDeserializer<Connection> {
public final static String META_TYPE = ConnectionAdapter.class.getSimpleName() + ".META_TYPE";
@Override
public Connection deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObject = json.getAsJsonObject();
String className = jsonObject.get(META_TYPE).getAsString();
try {
Class<?> clz = Class.forName(className);
return context.deserialize(json, clz);
} catch (ClassNotFoundException e) {
throw new JsonParseException(e);
}
}
@Override
public JsonElement serialize(Connection src, Type typeOfSrc, JsonSerializationContext context) {
JsonElement json = context.serialize(src, src.getClass());
json.getAsJsonObject().addProperty(META_TYPE, src.getClass().getCanonicalName());
return json;
}
}
|