Launcher/LaunchServer/src/main/java/ru/gravit/launchserver/config/TextureProviderAdapter.java

36 lines
1.2 KiB
Java
Raw Normal View History

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.texture.TextureProvider;
2019-03-13 12:13:21 +03:00
import ru.gravit.utils.helper.LogHelper;
2018-12-26 15:33:49 +03:00
2019-01-15 06:35:39 +03:00
import java.lang.reflect.Type;
public class TextureProviderAdapter implements JsonSerializer<TextureProvider>, JsonDeserializer<TextureProvider> {
private static final String PROP_NAME = "type";
2019-01-15 06:35:39 +03:00
@Override
public TextureProvider 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 TextureProvider> cls = TextureProvider.getProviderClass(typename);
2019-03-13 12:13:21 +03:00
if(cls == null)
{
LogHelper.error("TextureProvider %s not found", typename);
return null;
}
return (TextureProvider) context.deserialize(json, cls);
}
@Override
public JsonElement serialize(TextureProvider src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jo = context.serialize(src).getAsJsonObject();
String classPath = TextureProvider.getProviderName(src.getClass());
jo.add(PROP_NAME, new JsonPrimitive(classPath));
return jo;
}
}