4444
4545public 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 }
0 commit comments