33 lines
1.3 KiB
Java
33 lines
1.3 KiB
Java
package io.gitlab.jfronny.respackopts.gson;
|
|
|
|
import com.google.gson.*;
|
|
import io.gitlab.jfronny.respackopts.Respackopts;
|
|
import io.gitlab.jfronny.respackopts.model.DirRpo;
|
|
import io.gitlab.jfronny.respackopts.model.condition.Condition;
|
|
|
|
import java.lang.reflect.Type;
|
|
import java.util.Map;
|
|
|
|
public class DirRpoDeserializer implements JsonDeserializer<DirRpo> {
|
|
@Override
|
|
public DirRpo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
if (!json.isJsonObject())
|
|
throw new JsonParseException("Rpo must be a json object");
|
|
DirRpo rpo = new DirRpo();
|
|
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" -> {
|
|
if (entry.getValue().isJsonPrimitive() && entry.getValue().getAsJsonPrimitive().isString()) {
|
|
rpo.fallback = entry.getValue().getAsString();
|
|
}
|
|
else {
|
|
throw new JsonParseException("Directory .rpos only support a single fallback");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return rpo;
|
|
}
|
|
}
|