From 6dabdcd9a1e482dc3555166474e39e248a943b53 Mon Sep 17 00:00:00 2001 From: Hallvard Traetteberg <hal@ntnu.no> Date: Tue, 27 Aug 2019 22:11:54 +0200 Subject: [PATCH] Flere forenklinger. --- .../simpleex/json/JacksonConfigurator.java | 33 ----------------- .../java/simpleex/json/LatLongsModule.java | 37 +++++++++++++++++++ .../java/simpleex/ui/FxAppController.java | 22 ++++++++++- .../java/simpleex/json/AbstractJsonTest.java | 18 --------- .../java/simpleex/json/LatLongJsonTest.java | 24 ------------ .../java/simpleex/json/LatLongsJsonTest.java | 23 +++++++++--- 6 files changed, 74 insertions(+), 83 deletions(-) delete mode 100644 simpleexample/src/main/java/simpleex/json/JacksonConfigurator.java create mode 100644 simpleexample/src/main/java/simpleex/json/LatLongsModule.java delete mode 100644 simpleexample/src/test/java/simpleex/json/AbstractJsonTest.java delete mode 100644 simpleexample/src/test/java/simpleex/json/LatLongJsonTest.java diff --git a/simpleexample/src/main/java/simpleex/json/JacksonConfigurator.java b/simpleexample/src/main/java/simpleex/json/JacksonConfigurator.java deleted file mode 100644 index 8d4fe46..0000000 --- a/simpleexample/src/main/java/simpleex/json/JacksonConfigurator.java +++ /dev/null @@ -1,33 +0,0 @@ -package simpleex.json; - -import com.fasterxml.jackson.databind.Module; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.fasterxml.jackson.databind.module.SimpleModule; -import simpleex.core.LatLong; -import simpleex.core.LatLongs; - -public class JacksonConfigurator { - - public ObjectMapper createObjectMapper() { - final ObjectMapper objectMapper = new ObjectMapper(); - configureObjectMapper(objectMapper); - return objectMapper; - } - - public void configureObjectMapper(final ObjectMapper objectMapper) { - objectMapper.registerModule(createModule()); - } - - public Module createModule() { - final SimpleModule module = new SimpleModule(); - configureModule(module); - return module; - } - - public void configureModule(final SimpleModule module) { - module.addSerializer(LatLong.class, new LatLongSerializer()); - module.addSerializer(LatLongs.class, new LatLongsSerializer()); - module.addDeserializer(LatLong.class, new LatLongDeserializer()); - module.addDeserializer(LatLongs.class, new LatLongsDeserializer()); - } -} diff --git a/simpleexample/src/main/java/simpleex/json/LatLongsModule.java b/simpleexample/src/main/java/simpleex/json/LatLongsModule.java new file mode 100644 index 0000000..5359613 --- /dev/null +++ b/simpleexample/src/main/java/simpleex/json/LatLongsModule.java @@ -0,0 +1,37 @@ +package simpleex.json; + +import com.fasterxml.jackson.core.Version; +import com.fasterxml.jackson.databind.Module; +import com.fasterxml.jackson.databind.module.SimpleDeserializers; +import com.fasterxml.jackson.databind.module.SimpleSerializers; +import simpleex.core.LatLong; +import simpleex.core.LatLongs; + +public class LatLongsModule extends Module { + + @Override + public String getModuleName() { + return "LatLongsModule"; + } + + @Override + public Version version() { + return Version.unknownVersion(); + } + + private final SimpleSerializers serializers = new SimpleSerializers(); + private final SimpleDeserializers deserializers = new SimpleDeserializers(); + + public LatLongsModule() { + serializers.addSerializer(LatLong.class, new LatLongSerializer()); + serializers.addSerializer(LatLongs.class, new LatLongsSerializer()); + deserializers.addDeserializer(LatLong.class, new LatLongDeserializer()); + deserializers.addDeserializer(LatLongs.class, new LatLongsDeserializer()); + } + + @Override + public void setupModule(final SetupContext context) { + context.addSerializers(serializers); + context.addDeserializers(deserializers); + } +} diff --git a/simpleexample/src/main/java/simpleex/ui/FxAppController.java b/simpleexample/src/main/java/simpleex/ui/FxAppController.java index 366cbef..f58187b 100644 --- a/simpleexample/src/main/java/simpleex/ui/FxAppController.java +++ b/simpleexample/src/main/java/simpleex/ui/FxAppController.java @@ -26,7 +26,24 @@ import javafx.scene.control.Slider; import javafx.stage.FileChooser; import simpleex.core.LatLong; import simpleex.core.LatLongs; -import simpleex.json.JacksonConfigurator; +import simpleex.json.LatLongsModule; + +/* +@startuml +class FxAppController +class LatLongs +class BorderPane +class "ListView<LatLong>" as ListView +class "fxmapcontrol.MapBase" as MapBase + +BorderPane *--> ListView: "left" +BorderPane *--> MapBase: "center" + +FxAppController --> LatLongs: "latLongs" +FxAppController --> MapBase: "mapView" +FxAppController --> ListView: "locationListView" +@enduml + */ public class FxAppController { @@ -189,7 +206,8 @@ public class FxAppController { public ObjectMapper getObjectMapper() { if (objectMapper == null) { - objectMapper = new JacksonConfigurator().createObjectMapper(); + objectMapper = new ObjectMapper(); + objectMapper.registerModule(new LatLongsModule()); } return objectMapper; } diff --git a/simpleexample/src/test/java/simpleex/json/AbstractJsonTest.java b/simpleexample/src/test/java/simpleex/json/AbstractJsonTest.java deleted file mode 100644 index ab75ac0..0000000 --- a/simpleexample/src/test/java/simpleex/json/AbstractJsonTest.java +++ /dev/null @@ -1,18 +0,0 @@ -package simpleex.json; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.Assert; - -public abstract class AbstractJsonTest { - - private final ObjectMapper objectMapper = new JacksonConfigurator().createObjectMapper(); - - public ObjectMapper getObjectMapper() { - return objectMapper; - } - - protected void assertEqualsIgnoreWhitespace(final String expected, final String actual) - throws Exception { - Assert.assertEquals(expected, actual.replaceAll("\\s+", "")); - } -} diff --git a/simpleexample/src/test/java/simpleex/json/LatLongJsonTest.java b/simpleexample/src/test/java/simpleex/json/LatLongJsonTest.java deleted file mode 100644 index 3e87fa7..0000000 --- a/simpleexample/src/test/java/simpleex/json/LatLongJsonTest.java +++ /dev/null @@ -1,24 +0,0 @@ -package simpleex.json; - -import org.junit.Assert; -import org.junit.Test; -import simpleex.core.LatLong; - -public class LatLongJsonTest extends AbstractJsonTest { - - @Test - public void testLatLongSerialization() throws Exception { - assertEqualsIgnoreWhitespace("{\"latitude\":63.1,\"longitude\":12.3}", getObjectMapper().writeValueAsString(new LatLong(63.1, 12.3))); - } - - @Test - public void testLatLongObjectDeserialization() throws Exception { - Assert.assertEquals(new LatLong(63.1, 12.3), - getObjectMapper().readValue("{\"latitude\":63.1,\"longitude\":12.3}", LatLong.class)); - } - - @Test - public void testLatLongArrayDeserialization() throws Exception { - Assert.assertEquals(new LatLong(63.1, 12.3), getObjectMapper().readValue("[ 63.1, 12.3 ]", LatLong.class)); - } -} diff --git a/simpleexample/src/test/java/simpleex/json/LatLongsJsonTest.java b/simpleexample/src/test/java/simpleex/json/LatLongsJsonTest.java index 46bb9fb..c233c8d 100644 --- a/simpleexample/src/test/java/simpleex/json/LatLongsJsonTest.java +++ b/simpleexample/src/test/java/simpleex/json/LatLongsJsonTest.java @@ -1,23 +1,34 @@ package simpleex.json; +import com.fasterxml.jackson.databind.ObjectMapper; import org.junit.Assert; import org.junit.Test; import simpleex.core.LatLong; import simpleex.core.LatLongs; -public class LatLongsJsonTest extends AbstractJsonTest { +public class LatLongsJsonTest { + + private final ObjectMapper objectMapper = new ObjectMapper(); + { + objectMapper.registerModule(new LatLongsModule()); + } + + protected void assertEqualsIgnoreWhitespace(final String expected, final String actual) + throws Exception { + Assert.assertEquals(expected, actual.replaceAll("\\s+", "")); + } @Test public void testLatLongsSerialization() throws Exception { - assertEqualsIgnoreWhitespace( - "[{\"latitude\":63.1,\"longitude\":12.3},{\"latitude\":63.0,\"longitude\":12.4}]", - getObjectMapper().writeValueAsString(new LatLongs(new LatLong(63.1, 12.3), new LatLong(63.0, 12.4)))); + final String actualJson = objectMapper.writeValueAsString(new LatLongs(new LatLong(63.1, 12.3), new LatLong(63.0, 12.4))); + final String expectedJson = "[{\"latitude\":63.1,\"longitude\":12.3},{\"latitude\":63.0,\"longitude\":12.4}]"; + assertEqualsIgnoreWhitespace(expectedJson, actualJson); } @Test public void testLatLongsDeserialization() throws Exception { - final LatLongs latLongs = - getObjectMapper().readValue("[{\"latitude\":63.1,\"longitude\":12.3}, [63.0,12.4]]", LatLongs.class); + final String json = "[{\"latitude\":63.1,\"longitude\":12.3}, [63.0,12.4]]"; + final LatLongs latLongs = objectMapper.readValue(json, LatLongs.class); Assert.assertEquals(2, latLongs.getLatLongCount()); Assert.assertEquals(new LatLong(63.1, 12.3), latLongs.getLatLong(0)); Assert.assertEquals(new LatLong(63.0, 12.4), latLongs.getLatLong(1)); -- GitLab