From 9ed7fdb1cfd3dbcb6fd32e33f56ab948400ba42d Mon Sep 17 00:00:00 2001 From: Takase <20792268+takase1121@users.noreply.github.com> Date: Sat, 3 Feb 2024 19:08:46 +0800 Subject: [PATCH] Reduce marker coordinates precision (#5) --- .../bluemap/api/gson/MarkerGson.java | 24 +++++++++++++++---- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/bluecolored/bluemap/api/gson/MarkerGson.java b/src/main/java/de/bluecolored/bluemap/api/gson/MarkerGson.java index 8a138eb..de973a9 100644 --- a/src/main/java/de/bluecolored/bluemap/api/gson/MarkerGson.java +++ b/src/main/java/de/bluecolored/bluemap/api/gson/MarkerGson.java @@ -235,8 +235,8 @@ public void write(JsonWriter out, Vector2d value) throws IOException { } out.beginObject(); - out.name("x"); out.value(value.getX()); - out.name(useZ ? "z" : "y"); out.value(value.getY()); + out.name("x"); writeRounded(out, value.getX()); + out.name(useZ ? "z" : "y"); writeRounded(out, value.getY()); out.endObject(); } @@ -262,6 +262,13 @@ public Vector2d read(JsonReader in) throws IOException { return new Vector2d(x, y); } + private void writeRounded(JsonWriter json, double value) throws IOException { + // rounding and remove ".0" to save string space + double d = Math.round(value * 10000d) / 10000d; + if (d == (long) d) json.value((long) d); + else json.value(d); + } + } static class Vector3dAdapter extends TypeAdapter { @@ -274,9 +281,9 @@ public void write(JsonWriter out, Vector3d value) throws IOException { } out.beginObject(); - out.name("x"); out.value(value.getX()); - out.name("y"); out.value(value.getY()); - out.name("z"); out.value(value.getZ()); + out.name("x"); writeRounded(out, value.getX()); + out.name("y"); writeRounded(out, value.getY()); + out.name("z"); writeRounded(out, value.getZ()); out.endObject(); } @@ -302,6 +309,13 @@ public Vector3d read(JsonReader in) throws IOException { return new Vector3d(x, y, z); } + private void writeRounded(JsonWriter json, double value) throws IOException { + // rounding and remove ".0" to save string space + double d = Math.round(value * 10000d) / 10000d; + if (d == (long) d) json.value((long) d); + else json.value(d); + } + } static class Vector2iAdapter extends TypeAdapter {