package io.gitlab.jfronny.respackopts.gson; import com.google.gson.*; import com.google.gson.reflect.TypeToken; import io.gitlab.jfronny.respackopts.Respackopts; import io.gitlab.jfronny.respackopts.model.FileRpo; import io.gitlab.jfronny.respackopts.model.condition.Condition; import meteordevelopment.starscript.Script; import java.lang.reflect.Type; import java.util.Map; import java.util.Set; public class FileRpoDeserializer implements JsonDeserializer { private static final Type stringScriptMapType = new TypeToken>(){}.getType(); private static final Type stringSetType = new TypeToken>(){}.getType(); @Override public FileRpo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { if (!json.isJsonObject()) throw new JsonParseException("Rpo must be a json object"); FileRpo rpo = new FileRpo(); for (Map.Entry entry : json.getAsJsonObject().entrySet()) { switch (entry.getKey()) { case "conditions", "condition" -> rpo.conditions = context.deserialize(entry.getValue(), Condition.class); case "fallbacks", "fallback" -> rpo.fallbacks = context.deserialize(entry.getValue(), stringSetType); case "expansions", "expansion" -> rpo.expansions = context.deserialize(entry.getValue(), stringScriptMapType); } } return rpo; } }