Skip to content

Commit 29e84ed

Browse files
committed
Fix (de)serialization of markers
1 parent b359ec9 commit 29e84ed

File tree

2 files changed

+24
-11
lines changed

2 files changed

+24
-11
lines changed

src/main/java/de/bluecolored/bluemap/api/markers/MarkerGson.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,21 +44,25 @@
4444

4545
public final class MarkerGson {
4646

47-
public static final Gson INSTANCE = new GsonBuilder()
47+
public static final Gson INSTANCE = addAdapters(new GsonBuilder())
48+
.setLenient()
49+
.create();
50+
51+
/* This class can not be instantiated. */
52+
private MarkerGson() {}
53+
54+
public static GsonBuilder addAdapters(GsonBuilder builder) {
55+
return builder
4856
.registerTypeAdapter(Marker.class, new MarkerDeserializer())
57+
.registerTypeAdapter(Marker.class, new MarkerSerializer())
4958
.registerTypeAdapter(Line.class, new LineAdapter())
5059
.registerTypeAdapter(Shape.class, new ShapeAdapter())
5160
.registerTypeAdapter(Color.class, new ColorAdapter())
5261
.registerTypeAdapter(Vector2d.class, new Vector2dAdapter())
5362
.registerTypeAdapter(Vector3d.class, new Vector3dAdapter())
5463
.registerTypeAdapter(Vector2i.class, new Vector2iAdapter())
55-
.registerTypeAdapter(Vector3i.class, new Vector3iAdapter())
56-
.setLenient()
57-
.disableHtmlEscaping()
58-
.create();
59-
60-
/* This class can not be instantiated. */
61-
private MarkerGson() {}
64+
.registerTypeAdapter(Vector3i.class, new Vector3iAdapter());
65+
}
6266

6367
static class MarkerDeserializer implements JsonDeserializer<Marker> {
6468

@@ -71,11 +75,20 @@ static class MarkerDeserializer implements JsonDeserializer<Marker> {
7175
);
7276

7377
@Override
74-
public Marker deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
78+
public Marker deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext context) throws JsonParseException {
7579
String markerType = jsonElement.getAsJsonObject().get("type").getAsString();
7680
Class<? extends Marker> markerClass = MARKER_TYPES.get(markerType);
7781
if (markerClass == null) throw new JsonParseException("Unknown marker type: " + markerType);
78-
return jsonDeserializationContext.deserialize(jsonElement, markerClass);
82+
return context.deserialize(jsonElement, markerClass);
83+
}
84+
85+
}
86+
87+
static class MarkerSerializer implements JsonSerializer<Marker> {
88+
89+
@Override
90+
public JsonElement serialize(Marker src, Type typeOfSrc, JsonSerializationContext context) {
91+
return context.serialize(src, src.getClass()); // serialize the actual marker-subclass
7992
}
8093

8194
}

src/main/java/de/bluecolored/bluemap/api/markers/MarkerSet.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class MarkerSet {
3737

3838
private String label;
3939
private boolean toggleable, defaultHidden;
40-
private final Map<String, Marker> markers;
40+
private final ConcurrentHashMap<String, Marker> markers;
4141

4242
/**
4343
* Empty constructor for deserialization.

0 commit comments

Comments
 (0)