mirror of
https://github.com/GravitLauncher/Launcher
synced 2024-12-22 16:41:46 +03:00
[FEATURE] defaultClass в сериализации по type
This commit is contained in:
parent
dd11ba8179
commit
07ba1255ce
3 changed files with 43 additions and 18 deletions
|
@ -15,6 +15,7 @@
|
|||
import pro.gravit.launchserver.dao.provider.DaoProvider;
|
||||
import pro.gravit.launchserver.modules.impl.LaunchServerModulesManager;
|
||||
import pro.gravit.launchserver.socket.WebSocketService;
|
||||
import pro.gravit.launchserver.socket.response.UnknownResponse;
|
||||
import pro.gravit.launchserver.socket.response.WebSocketServerResponse;
|
||||
import pro.gravit.utils.UniversalJsonAdapter;
|
||||
|
||||
|
@ -34,7 +35,7 @@ public void registerAdapters(GsonBuilder builder) {
|
|||
builder.registerTypeAdapter(Component.class, new UniversalJsonAdapter<>(Component.providers));
|
||||
builder.registerTypeAdapter(ProtectHandler.class, new UniversalJsonAdapter<>(ProtectHandler.providers));
|
||||
builder.registerTypeAdapter(DaoProvider.class, new UniversalJsonAdapter<>(DaoProvider.providers));
|
||||
builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers));
|
||||
builder.registerTypeAdapter(WebSocketServerResponse.class, new UniversalJsonAdapter<>(WebSocketService.providers, UnknownResponse.class));
|
||||
builder.registerTypeAdapter(WebSocketEvent.class, new JsonResultSerializeAdapter());
|
||||
builder.registerTypeAdapter(AuthRequest.AuthPasswordInterface.class, new UniversalJsonAdapter<>(AuthRequest.providers));
|
||||
builder.registerTypeAdapter(HWIDProvider.class, new UniversalJsonAdapter<>(HWIDProvider.providers));
|
||||
|
|
|
@ -0,0 +1,16 @@
|
|||
package pro.gravit.launchserver.socket.response;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import pro.gravit.launchserver.socket.Client;
|
||||
|
||||
public class UnknownResponse extends SimpleResponse {
|
||||
@Override
|
||||
public String getType() {
|
||||
return "unknown";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute(ChannelHandlerContext ctx, Client client) {
|
||||
sendError("This type of request is not supported");
|
||||
}
|
||||
}
|
|
@ -15,41 +15,50 @@ public class UniversalJsonAdapter<R> implements JsonSerializer<R>, JsonDeseriali
|
|||
public final ProviderMap<R> providerMap;
|
||||
public final String name;
|
||||
public final String PROP_NAME;
|
||||
public final boolean printErrorIfUnknownType;
|
||||
public Class<? extends R> defaultClass;
|
||||
|
||||
public UniversalJsonAdapter(ProviderMap<R> providerMap) {
|
||||
this.providerMap = providerMap;
|
||||
this.name = providerMap.getName();
|
||||
this.PROP_NAME = "type";
|
||||
printErrorIfUnknownType = true;
|
||||
}
|
||||
|
||||
public UniversalJsonAdapter(ProviderMap<R> providerMap, String PROP_NAME) {
|
||||
this.providerMap = providerMap;
|
||||
this.name = providerMap.getName();
|
||||
this.PROP_NAME = PROP_NAME;
|
||||
printErrorIfUnknownType = true;
|
||||
}
|
||||
|
||||
public UniversalJsonAdapter(ProviderMap<R> providerMap, String name, String PROP_NAME, boolean printErrorIfUnknownType) {
|
||||
public UniversalJsonAdapter(ProviderMap<R> providerMap, String name, Class<? extends R> defaultClass) {
|
||||
this.providerMap = providerMap;
|
||||
this.name = name;
|
||||
this.defaultClass = defaultClass;
|
||||
this.PROP_NAME = "type";
|
||||
}
|
||||
|
||||
public UniversalJsonAdapter(ProviderMap<R> providerMap, Class<? extends R> defaultClass) {
|
||||
this.providerMap = providerMap;
|
||||
this.defaultClass = defaultClass;
|
||||
this.name = providerMap.getName();
|
||||
this.PROP_NAME = "type";
|
||||
}
|
||||
|
||||
public UniversalJsonAdapter(ProviderMap<R> providerMap, String name, String PROP_NAME, Class<? extends R> defaultClass) {
|
||||
this.providerMap = providerMap;
|
||||
this.name = name;
|
||||
this.PROP_NAME = PROP_NAME;
|
||||
this.printErrorIfUnknownType = printErrorIfUnknownType;
|
||||
}
|
||||
|
||||
public UniversalJsonAdapter(ProviderMap<R> providerMap, String name, boolean printErrorIfUnknownType) {
|
||||
this.providerMap = providerMap;
|
||||
this.name = name;
|
||||
this.PROP_NAME = "type";
|
||||
this.printErrorIfUnknownType = printErrorIfUnknownType;
|
||||
this.defaultClass = defaultClass;
|
||||
}
|
||||
|
||||
public R deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
||||
String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString();
|
||||
Class<? extends R> cls = providerMap.getClass(typename);
|
||||
if (cls == null) {
|
||||
if (printErrorIfUnknownType) LogHelper.error("%s %s not found", name, typename);
|
||||
//if (printErrorIfUnknownType) LogHelper.error("%s %s not found", name, typename);
|
||||
if(defaultClass != null)
|
||||
{
|
||||
return context.deserialize(json, defaultClass);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
return context.deserialize(json, cls);
|
||||
|
@ -63,10 +72,9 @@ public JsonElement serialize(R src, Type typeOfSrc, JsonSerializationContext con
|
|||
if (classPath == null && src instanceof TypeSerializeInterface) {
|
||||
classPath = ((TypeSerializeInterface) src).getType();
|
||||
}
|
||||
if (classPath == null) {
|
||||
if (printErrorIfUnknownType) LogHelper.warning("Class %s type null", src.getClass());
|
||||
} else jo.add(PROP_NAME, new JsonPrimitive(classPath));
|
||||
|
||||
if (classPath != null) {
|
||||
jo.add(PROP_NAME, new JsonPrimitive(classPath));
|
||||
}
|
||||
return jo;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue