diff --git a/simpleexample2/core/src/main/java/simpleex/json/LatLongDeserializer.java b/simpleexample2/core/src/main/java/simpleex/json/LatLongDeserializer.java index 37636474cb2238d23ce69fac1df0274ee569d3d3..deaab616c7f4918c49dfb7d1e7ffbcdc3d6ca5cd 100644 --- a/simpleexample2/core/src/main/java/simpleex/json/LatLongDeserializer.java +++ b/simpleexample2/core/src/main/java/simpleex/json/LatLongDeserializer.java @@ -9,6 +9,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import java.io.IOException; import simpleex.core.LatLong; +import simpleex.core.MetaData; /** * JSON serializer for LatLong. @@ -36,7 +37,24 @@ public class LatLongDeserializer extends JsonDeserializer<LatLong> { final ObjectNode objectNode = (ObjectNode) jsonNode; final double latitude = objectNode.get(LatLongSerializer.LATITUDE_FIELD_NAME).asDouble(); final double longitude = objectNode.get(LatLongSerializer.LONGITUDE_FIELD_NAME).asDouble(); - return new LatLong(latitude, longitude); + final LatLong latLon = new LatLong(latitude, longitude); + if (objectNode.has(LatLongSerializer.META_DATA_FIELD_NAME)) { + final ObjectNode metaDataNode = (ObjectNode) objectNode.get(LatLongSerializer.META_DATA_FIELD_NAME); + final MetaData metaData = latLon.getMetaData(); + if (metaDataNode.has(LatLongSerializer.TAGS_FIELD_NAME)) { + for (final JsonNode tagNode : (ArrayNode) metaDataNode.get(LatLongSerializer.TAGS_FIELD_NAME)) { + metaData.addTags(tagNode.asText()); + } + } + if (metaDataNode.has(LatLongSerializer.PROPERTIES_FIELD_NAME)) { + for (final JsonNode propertyNode : (ArrayNode) metaDataNode.get(LatLongSerializer.PROPERTIES_FIELD_NAME)) { + final String propertyName = (propertyNode instanceof ArrayNode ? ((ArrayNode) propertyNode).get(0) : ((ObjectNode) propertyNode).get(LatLongSerializer.PROPERTIES_NAME_FIELD_NAME)).asText(); + final String propertyValue = (propertyNode instanceof ArrayNode ? ((ArrayNode) propertyNode).get(1) : ((ObjectNode) propertyNode).get(LatLongSerializer.PROPERTIES_VALUE_FIELD_NAME)).asText(); + metaData.setProperty(propertyName, propertyValue); + } + } + } + return latLon; } else if (jsonNode instanceof ArrayNode) { final ArrayNode locationArray = (ArrayNode) jsonNode; if (locationArray.size() == ARRAY_JSON_NODE_SIZE) { diff --git a/simpleexample2/core/src/main/java/simpleex/json/LatLongSerializer.java b/simpleexample2/core/src/main/java/simpleex/json/LatLongSerializer.java index 59dfeb6fb57eccbacf6f91b8c16fb0d44d7c2c9a..d56f1013c690e4fa6a0e3a6bd46f236e194bc206 100644 --- a/simpleexample2/core/src/main/java/simpleex/json/LatLongSerializer.java +++ b/simpleexample2/core/src/main/java/simpleex/json/LatLongSerializer.java @@ -4,12 +4,19 @@ import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; import java.io.IOException; +import java.util.Iterator; import simpleex.core.LatLong; +import simpleex.core.MetaData; public class LatLongSerializer extends JsonSerializer<LatLong> { public static final String LONGITUDE_FIELD_NAME = "longitude"; public static final String LATITUDE_FIELD_NAME = "latitude"; + public static final String META_DATA_FIELD_NAME = "metaData"; + public static final String TAGS_FIELD_NAME = "tags"; + public static final String PROPERTIES_FIELD_NAME = "properties"; + public static final String PROPERTIES_NAME_FIELD_NAME = "name"; + public static final String PROPERTIES_VALUE_FIELD_NAME = "value"; @Override public void serialize(final LatLong latLon, final JsonGenerator jsonGen, @@ -19,6 +26,38 @@ public class LatLongSerializer extends JsonSerializer<LatLong> { jsonGen.writeNumber(latLon.getLatitude()); jsonGen.writeFieldName(LONGITUDE_FIELD_NAME); jsonGen.writeNumber(latLon.getLongitude()); + + // serialize meta-data + if (latLon.hasMetaData()) { + jsonGen.writeFieldName(META_DATA_FIELD_NAME); + jsonGen.writeStartObject(); + final MetaData metaData = latLon.getMetaData(); + final Iterator<String> tags = metaData.tags(); + if (tags.hasNext()) { + jsonGen.writeFieldName(TAGS_FIELD_NAME); + jsonGen.writeStartArray(); + while (tags.hasNext()) { + jsonGen.writeString(tags.next()); + } + jsonGen.writeEndArray(); + } + final Iterator<String> propertyNames = metaData.propertyNames(); + if (propertyNames.hasNext()) { + jsonGen.writeFieldName(PROPERTIES_FIELD_NAME); + jsonGen.writeStartArray(); + while (propertyNames.hasNext()) { + jsonGen.writeStartObject(); + jsonGen.writeFieldName(PROPERTIES_NAME_FIELD_NAME); + final String propertyName = propertyNames.next(); + jsonGen.writeString(propertyName); + jsonGen.writeFieldName(PROPERTIES_VALUE_FIELD_NAME); + jsonGen.writeString(metaData.getProperty(propertyName)); + jsonGen.writeEndObject(); + } + jsonGen.writeEndArray(); + } + } + jsonGen.writeEndObject(); } } diff --git a/simpleexample2/core/src/test/java/simpleex/json/LatLongsJsonTest.java b/simpleexample2/core/src/test/java/simpleex/json/LatLongsJsonTest.java index 079cab29ef6164db476c972c9d3d59ec70de72df..4345ef642db10005f82539bfe80f4fce5c586fae 100644 --- a/simpleexample2/core/src/test/java/simpleex/json/LatLongsJsonTest.java +++ b/simpleexample2/core/src/test/java/simpleex/json/LatLongsJsonTest.java @@ -1,10 +1,13 @@ package simpleex.json; import com.fasterxml.jackson.databind.ObjectMapper; +import java.util.Arrays; +import java.util.Iterator; import org.junit.Assert; import org.junit.Test; import simpleex.core.LatLong; import simpleex.core.LatLongs; +import simpleex.core.MetaData; public class LatLongsJsonTest { @@ -43,4 +46,38 @@ public class LatLongsJsonTest { Assert.assertEquals(latLong1(), latLongs.getLatLong(0)); Assert.assertEquals(latLong2(), latLongs.getLatLong(1)); } + + @Test + public void testLatLongMetaDataSerialization() throws Exception { + final LatLong latLong = latLong1(); + final MetaData metaData = latLong.getMetaData(); + metaData.addTags("aTag", "bTag"); + metaData.setProperty("aProperty", "aValue"); + final String actualJson = objectMapper.writeValueAsString(latLong); + final String expectedJson = "{\"latitude\":63.1,\"longitude\":12.3," + + "\"metaData\":{\"tags\":[\"aTag\",\"bTag\"],\"properties\":[{\"name\":\"aProperty\",\"value\":\"aValue\"}]}}"; + assertEqualsIgnoreWhitespace(expectedJson, actualJson); + } + + // relies on a certain order + private void check(final Iterator<String> it, final String... ss) { + final Iterator<String> it1 = Arrays.asList(ss).iterator(); + while (it1.hasNext() && it.hasNext()) { + Assert.assertEquals(it1.next(), it.next()); + } + Assert.assertEquals(it1.hasNext(), it.hasNext()); + } + + @Test + public void testLatLongMetaDataDeserialization() throws Exception { + final String json = "{\"latitude\":63.1,\"longitude\":12.3," + + "\"metaData\":{\"tags\":[\"aTag\",\"bTag\"],\"properties\":[{\"name\":\"aProperty\",\"value\":\"aValue\"},[\"bProperty\",\"bValue\"]]}}"; + final LatLong latLong = objectMapper.readValue(json, LatLong.class); + final MetaData metaData = latLong.getMetaData(); + Assert.assertTrue(metaData.hasTags("aTag", "bTag")); + Assert.assertEquals("aValue", metaData.getProperty("aProperty")); + Assert.assertEquals("bValue", metaData.getProperty("bProperty")); + check(metaData.tags(), "aTag", "bTag"); + check(metaData.propertyNames(), "aProperty", "bProperty"); + } }