Skip to content
Snippets Groups Projects
Commit 6dabdcd9 authored by Hallvard Trætteberg's avatar Hallvard Trætteberg
Browse files

Flere forenklinger.

parent 5cd1c12f
No related branches found
No related tags found
No related merge requests found
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());
}
}
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);
}
}
......@@ -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;
}
......
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+", ""));
}
}
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));
}
}
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));
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment