From 29e84ed198a024e2e5a6a7dd4a5c62999cac4fb4 Mon Sep 17 00:00:00 2001 From: "Lukas Rieger (Blue)" Date: Sat, 30 Jul 2022 13:23:31 +0200 Subject: [PATCH] Fix (de)serialization of markers --- .../bluemap/api/markers/MarkerGson.java | 33 +++++++++++++------ .../bluemap/api/markers/MarkerSet.java | 2 +- 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/main/java/de/bluecolored/bluemap/api/markers/MarkerGson.java b/src/main/java/de/bluecolored/bluemap/api/markers/MarkerGson.java index 724262d..0e4e041 100644 --- a/src/main/java/de/bluecolored/bluemap/api/markers/MarkerGson.java +++ b/src/main/java/de/bluecolored/bluemap/api/markers/MarkerGson.java @@ -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 { @@ -71,11 +75,20 @@ static class MarkerDeserializer implements JsonDeserializer { ); @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 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 { + + @Override + public JsonElement serialize(Marker src, Type typeOfSrc, JsonSerializationContext context) { + return context.serialize(src, src.getClass()); // serialize the actual marker-subclass } } diff --git a/src/main/java/de/bluecolored/bluemap/api/markers/MarkerSet.java b/src/main/java/de/bluecolored/bluemap/api/markers/MarkerSet.java index 66d35d0..f72a18d 100644 --- a/src/main/java/de/bluecolored/bluemap/api/markers/MarkerSet.java +++ b/src/main/java/de/bluecolored/bluemap/api/markers/MarkerSet.java @@ -37,7 +37,7 @@ public class MarkerSet { private String label; private boolean toggleable, defaultHidden; - private final Map markers; + private final ConcurrentHashMap markers; /** * Empty constructor for deserialization.