2021-10-03 16:45:29 +02:00
|
|
|
package io.gitlab.jfronny.respackopts.gson.entry;
|
|
|
|
|
2022-06-04 19:43:49 +02:00
|
|
|
import io.gitlab.jfronny.gson.*;
|
2021-11-14 15:37:01 +01:00
|
|
|
import io.gitlab.jfronny.respackopts.model.tree.ConfigNumericEntry;
|
2021-10-03 16:45:29 +02:00
|
|
|
|
|
|
|
import java.lang.reflect.Type;
|
|
|
|
|
|
|
|
public class NumericEntrySerializer implements JsonSerializer<ConfigNumericEntry>, JsonDeserializer<ConfigNumericEntry> {
|
|
|
|
@Override
|
|
|
|
public JsonElement serialize(ConfigNumericEntry src, Type typeOfSrc, JsonSerializationContext context) {
|
|
|
|
return new JsonPrimitive(src.getValue());
|
|
|
|
}
|
|
|
|
@Override
|
|
|
|
public ConfigNumericEntry deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
|
|
|
|
if (json.isJsonPrimitive() && json.getAsJsonPrimitive().isNumber()) {
|
2022-08-28 20:06:59 +02:00
|
|
|
ConfigNumericEntry result = new ConfigNumericEntry();
|
2021-10-03 16:45:29 +02:00
|
|
|
result.setValue(json.getAsDouble());
|
|
|
|
result.setDefault(json.getAsDouble());
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
else if (json.isJsonObject()) {
|
|
|
|
JsonObject o = json.getAsJsonObject();
|
2022-08-28 20:06:59 +02:00
|
|
|
ConfigNumericEntry result = new ConfigNumericEntry();
|
2021-10-03 16:45:29 +02:00
|
|
|
JsonElement min = o.get("min");
|
|
|
|
JsonElement max = o.get("max");
|
|
|
|
if (min != null) {
|
|
|
|
if (min.isJsonPrimitive() && min.getAsJsonPrimitive().isNumber()) {
|
|
|
|
result.min = min.getAsNumber().doubleValue();
|
|
|
|
}
|
|
|
|
else throw new JsonSyntaxException("Expected number as min of numeric entry");
|
|
|
|
}
|
|
|
|
if (max != null) {
|
|
|
|
if (max.isJsonPrimitive() && max.getAsJsonPrimitive().isNumber()) {
|
|
|
|
result.max = max.getAsNumber().doubleValue();
|
|
|
|
}
|
|
|
|
else throw new JsonSyntaxException("Expected number as max of numeric entry");
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
throw new JsonSyntaxException("Could not deserialize numeric entry");
|
|
|
|
}
|
|
|
|
}
|