Commit b168dca5 authored by George Adrian Stoica's avatar George Adrian Stoica

Merge branch 'issue-9-LatLong-meta-data' into issue-10-display-latlong-metadata-in-the-fx-ui

catch up with branch to get metadata serialization deserialization code
parents d549c332 03287017
......@@ -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) {
......
......@@ -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();
}
}
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");
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment