diff options
author | cyberta <cyberta@riseup.net> | 2021-01-25 11:49:07 +0000 |
---|---|---|
committer | cyberta <cyberta@riseup.net> | 2021-01-25 11:49:07 +0000 |
commit | d2cfd37e8380f47cf450fff59867c87b6327a735 (patch) | |
tree | 93dee5b4dcd6fa85f9bf70c17920c21f46b6402c /app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java | |
parent | 67f62a7d20dd079fb1f6392d6c953e2ee8ea29d3 (diff) | |
parent | bb6679c4c04e974ddb064b4cb5cee268b67b8ce1 (diff) |
Merge branch 'fix_always_on_reboot' into 'master'
Fix always on reboot
Closes #8994
See merge request leap/bitmask_android!119
Diffstat (limited to 'app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java')
-rw-r--r-- | app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java | 35 |
1 files changed, 35 insertions, 0 deletions
diff --git a/app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java b/app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java new file mode 100644 index 00000000..335ef34c --- /dev/null +++ b/app/src/main/java/de/blinkt/openvpn/core/connection/ConnectionAdapter.java @@ -0,0 +1,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; + } +} |