package ru.gravit.utils; import com.google.gson.*; import ru.gravit.utils.helper.LogHelper; import java.lang.reflect.Type; public class UniversalJsonAdapter implements JsonSerializer, JsonDeserializer { public final ProviderMap providerMap; public final String PROP_NAME; public UniversalJsonAdapter(ProviderMap providerMap) { this.providerMap = providerMap; this.PROP_NAME = "type"; } public UniversalJsonAdapter(ProviderMap providerMap, String PROP_NAME) { this.providerMap = providerMap; this.PROP_NAME = PROP_NAME; } public R deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String typename = json.getAsJsonObject().getAsJsonPrimitive(PROP_NAME).getAsString(); Class cls = providerMap.getProviderClass(typename); if (cls == null) { LogHelper.error("Provider %s not found", typename); return null; } return context.deserialize(json, cls); } @Override public JsonElement serialize(R src, Type typeOfSrc, JsonSerializationContext context) { JsonObject jo = context.serialize(src).getAsJsonObject(); @SuppressWarnings("unchecked") String classPath = providerMap.getProviderName((Class) src.getClass()); jo.add(PROP_NAME, new JsonPrimitive(classPath)); return jo; } }