2018-12-23 20:19:43 +03:00
|
|
|
package ru.gravit.launchserver.config;
|
|
|
|
|
2019-01-15 06:35:39 +03:00
|
|
|
import com.google.gson.*;
|
2018-12-26 15:33:49 +03:00
|
|
|
import ru.gravit.launchserver.auth.handler.AuthHandler;
|
|
|
|
|
2019-01-15 06:35:39 +03:00
|
|
|
import java.lang.reflect.Type;
|
|
|
|
|
2018-12-23 20:19:43 +03:00
|
|
|
public class AuthHandlerAdapter implements JsonSerializer<AuthHandler>, JsonDeserializer<AuthHandler> {
|
|
|
|
private static final String PROP_NAME = "type";
|
2019-01-15 06:35:39 +03:00
|
|
|
|
2018-12-23 20:19:43 +03:00
|
|
|
@Override
|
|
|
|
public AuthHandler deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
|
|
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
|
2018-12-26 15:33:49 +03:00
|
|
|
Class<? extends AuthHandler> cls = AuthHandler.getHandlerClass(typename);
|
2018-12-23 20:19:43 +03:00
|
|
|
|
|
|
|
|
|
|
|
return (AuthHandler) context.deserialize(json, cls);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public JsonElement serialize(AuthHandler src, Type typeOfSrc, JsonSerializationContext context) {
|
|
|
|
JsonObject jo = context.serialize(src).getAsJsonObject();
|
|
|
|
|
2018-12-26 15:33:49 +03:00
|
|
|
@SuppressWarnings("unchecked")
|
2019-01-15 06:35:39 +03:00
|
|
|
String classPath = AuthHandler.getHandlerName((Class<AuthHandler>) src.getClass());
|
2018-12-23 20:19:43 +03:00
|
|
|
jo.add(PROP_NAME, new JsonPrimitive(classPath));
|
|
|
|
|
|
|
|
return jo;
|
|
|
|
}
|
|
|
|
}
|