[FEATURE] defaultClass в сериализации по type

This commit is contained in:
Gravit 2020-06-22 11:20:42 +07:00
parent dd11ba8179
commit 07ba1255ce
No known key found for this signature in database
GPG key ID: 061981E1E85D3216
3 changed files with 43 additions and 18 deletions

View file

@ -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));

View file

@ -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");
}
}

View file

@ -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;
}
}