Skip to content

Commit

Permalink
Merge pull request #263 from OpenSRP/262-location-properties-desializer
Browse files Browse the repository at this point in the history
Location properties deserializer
  • Loading branch information
githengi authored Jul 15, 2019
2 parents ec2d8fd + 6da927c commit 53740b2
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 4 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
VERSION_NAME=1.6.45-SNAPSHOT
VERSION_NAME=1.6.46-SNAPSHOT
VERSION_CODE=1
GROUP=org.smartregister
POM_SETTING_DESCRIPTION=OpenSRP Client Core Application
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import org.joda.time.DateTime;

import java.util.HashMap;
import java.util.Map;

/**
Expand All @@ -17,7 +18,7 @@ public enum PropertyStatus {
@SerializedName("Inactive")
INACTIVE,
@SerializedName("Pending Review")
PENDING_REVIEW;
PENDING_REVIEW

}

Expand All @@ -41,7 +42,7 @@ public enum PropertyStatus {

private int version;

private transient Map<String, String> customProperties;
private transient Map<String, String> customProperties = new HashMap<>();

public String getUid() {
return uid;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,27 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;

import org.joda.time.DateTime;
import org.smartregister.domain.LocationProperty;

import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Created by samuelgithengi on 11/30/18.
*/
public class PropertiesConverter implements JsonSerializer<LocationProperty> {
public class PropertiesConverter implements JsonSerializer<LocationProperty>, JsonDeserializer<LocationProperty> {

public static Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ")
.registerTypeAdapter(DateTime.class, new DateTimeTypeConverter()).create();
Expand All @@ -31,4 +37,23 @@ public JsonElement serialize(LocationProperty src, Type typeOfSrc, JsonSerializa
}
return object;
}

@Override
public LocationProperty deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {

LocationProperty properties = gson.fromJson(json, LocationProperty.class);

Set<Map.Entry<String, JsonElement>> entrySet = json.getAsJsonObject().entrySet();
Set<String> mappedKeys = new HashSet<>();
for (Field field : LocationProperty.class.getDeclaredFields()) {
mappedKeys.add(field.getName());
}
for (Map.Entry<String, JsonElement> entry : entrySet) {
if (!mappedKeys.contains(entry.getKey())) {
properties.getCustomProperties().put(entry.getKey(), entry.getValue().getAsString());
}
}

return properties;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,22 @@ public void testSerializeCustomProperties() {
assertEquals(expected, stripTimezone(gson.toJson(structure)));


}


@Test
public void testDeserializeCustomProperties() {

Location structure = gson.fromJson("{\"type\":\"Feature\",\"id\":\"90397\",\"geometry\":{\"type\":\"Polygon\",\"coordinates\":[[[32.5978597,-14.1699446],[32.5978956,-14.1699609],[32.5978794,-14.1699947],[32.5978434,-14.1699784],[32.5978597,-14.1699446]]]},\"properties\":{\"uid\":\"41587456-b7c8-4c4e-b433-23a786f742fc\",\"code\":\"21384443\",\"type\":\"Residential Structure\",\"status\":\"Active\",\"parentId\":\"3734\",\"geographicLevel\":5,\"effectiveStartDate\":\"2017-01-10T00:00:00.000\",\"version\":0,\"taskBusinessStatus\":\"Not Visited\",\"taskIdentifier\":\"0d15fcac-df64-4f53-b01a-b650d1e45252\",\"taskStatus\":\"In Progress\"}}"
, Location.class);

Map<String, String> customProperties = structure.getProperties().getCustomProperties();
assertEquals(3, customProperties.size());
assertEquals("0d15fcac-df64-4f53-b01a-b650d1e45252", customProperties.get("taskIdentifier"));
assertEquals("Not Visited", customProperties.get("taskBusinessStatus"));
assertEquals("In Progress", customProperties.get("taskStatus"));


}

public static String stripTimezone(String locationJson) {
Expand Down

0 comments on commit 53740b2

Please sign in to comment.