Respackopts/src/main/java/io/gitlab/jfronny/respackopts/gson/FileRpoDeserializer.java

32 lines
1.5 KiB
Java

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<FileRpo> {
private static final Type stringScriptMapType = new TypeToken<Map<String, Script>>(){}.getType();
private static final Type stringSetType = new TypeToken<Set<String>>(){}.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<String, JsonElement> 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;
}
}