Fix (de)serialization of markers

This commit is contained in:
Lukas Rieger (Blue) 2022-07-30 13:23:31 +02:00
parent b359ec964c
commit 29e84ed198
No known key found for this signature in database
GPG Key ID: 2D09EC5ED2687FF2
2 changed files with 24 additions and 11 deletions

View File

@ -44,21 +44,25 @@
public final class MarkerGson {
public static final Gson INSTANCE = new GsonBuilder()
public static final Gson INSTANCE = addAdapters(new GsonBuilder())
.setLenient()
.create();
/* This class can not be instantiated. */
private MarkerGson() {}
public static GsonBuilder addAdapters(GsonBuilder builder) {
return builder
.registerTypeAdapter(Marker.class, new MarkerDeserializer())
.registerTypeAdapter(Marker.class, new MarkerSerializer())
.registerTypeAdapter(Line.class, new LineAdapter())
.registerTypeAdapter(Shape.class, new ShapeAdapter())
.registerTypeAdapter(Color.class, new ColorAdapter())
.registerTypeAdapter(Vector2d.class, new Vector2dAdapter())
.registerTypeAdapter(Vector3d.class, new Vector3dAdapter())
.registerTypeAdapter(Vector2i.class, new Vector2iAdapter())
.registerTypeAdapter(Vector3i.class, new Vector3iAdapter())
.setLenient()
.disableHtmlEscaping()
.create();
/* This class can not be instantiated. */
private MarkerGson() {}
.registerTypeAdapter(Vector3i.class, new Vector3iAdapter());
}
static class MarkerDeserializer implements JsonDeserializer<Marker> {
@ -71,11 +75,20 @@ static class MarkerDeserializer implements JsonDeserializer<Marker> {
);
@Override
public Marker deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
public Marker deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
String markerType = jsonElement.getAsJsonObject().get("type").getAsString();
Class<? extends Marker> markerClass = MARKER_TYPES.get(markerType);
if (markerClass == null) throw new JsonParseException("Unknown marker type: " + markerType);
return jsonDeserializationContext.deserialize(jsonElement, markerClass);
return context.deserialize(jsonElement, markerClass);
}
}
static class MarkerSerializer implements JsonSerializer<Marker> {
@Override
public JsonElement serialize(Marker src, Type typeOfSrc, JsonSerializationContext context) {
return context.serialize(src, src.getClass()); // serialize the actual marker-subclass
}
}

View File

@ -37,7 +37,7 @@ public class MarkerSet {
private String label;
private boolean toggleable, defaultHidden;
private final Map<String, Marker> markers;
private final ConcurrentHashMap<String, Marker> markers;
/**
* Empty constructor for deserialization.