Added validations to json objects in Android-REST apis

This commit is contained in:
harshanL 2014-12-11 15:48:59 +05:30
parent 6d5ea7f586
commit dacd16b852

View File

@ -29,28 +29,50 @@ import java.util.*;
public class AndroidAPIUtil { public class AndroidAPIUtil {
public static Device convertToDeviceObject(String jsonString) { public static Device convertToDeviceObject(String jsonString) {
JsonObject obj = new Gson().fromJson(jsonString, JsonObject.class); JsonObject obj = new Gson().fromJson(jsonString, JsonObject.class);
Device device = new Device();
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
if (obj.get(AndroidConstants.DeviceConstants.DEVICE_MAC_KEY) != null) {
device.setDeviceIdentifier(
obj.get(AndroidConstants.DeviceConstants.DEVICE_MAC_KEY).getAsString());
}
if (obj.get(AndroidConstants.DeviceConstants.DEVICE_DESCRIPTION_KEY) != null) {
device.setDescription(
obj.get(AndroidConstants.DeviceConstants.DEVICE_DESCRIPTION_KEY).getAsString());
}
if (obj.get(AndroidConstants.DeviceConstants.DEVICE_OWNERSHIP_KEY) != null) {
device.setOwnership(
obj.get(AndroidConstants.DeviceConstants.DEVICE_OWNERSHIP_KEY).getAsString());
}
if (obj.get(AndroidConstants.DeviceConstants.DEVICE_PROPERTIES_KEY) != null) {
JsonObject properties = JsonObject properties =
new Gson().fromJson(obj.get(AndroidConstants.DeviceConstants.DEVICE_PROPERTIES_KEY) new Gson().fromJson(
obj.get(AndroidConstants.DeviceConstants.DEVICE_PROPERTIES_KEY)
, JsonObject.class); , JsonObject.class);
if (properties.get(AndroidConstants.DeviceProperties.PROPERTY_USER_KEY) != null) {
device.setOwner(properties.get(AndroidConstants.DeviceProperties.PROPERTY_USER_KEY)
.getAsString());
}
if (properties.get(AndroidConstants.DeviceProperties.PROPERTY_DEVICE_KEY) != null) {
device.setName(properties.get(AndroidConstants.DeviceProperties.PROPERTY_DEVICE_KEY)
.getAsString());
}
device.setProperties(parseProperties(properties));
}else{
device.setProperties(new ArrayList<Property>(0));
}
if (obj.get(AndroidConstants.DeviceConstants.DEVICE_FEATURES_KEY) != null) {
JsonObject features = JsonObject features =
new Gson().fromJson( new Gson().fromJson(
obj.get(AndroidConstants.DeviceConstants.DEVICE_FEATURES_KEY), obj.get(AndroidConstants.DeviceConstants.DEVICE_FEATURES_KEY),
JsonObject.class); JsonObject.class);
Device device = new Device();
device.setType(DeviceManagementConstants.MobileDeviceTypes.MOBILE_DEVICE_TYPE_ANDROID);
device.setOwner(properties.get(AndroidConstants.DeviceProperties.PROPERTY_USER_KEY).getAsString());
device.setDeviceIdentifier(
obj.get(AndroidConstants.DeviceConstants.DEVICE_MAC_KEY).getAsString());
device.setDescription(
obj.get(AndroidConstants.DeviceConstants.DEVICE_DESCRIPTION_KEY).getAsString());
device.setOwnership(
obj.get(AndroidConstants.DeviceConstants.DEVICE_OWNERSHIP_KEY).getAsString());
device.setName(properties.get(AndroidConstants.DeviceProperties.PROPERTY_DEVICE_KEY)
.getAsString());
device.setFeatures(parseFeatures(features)); device.setFeatures(parseFeatures(features));
device.setProperties(parseProperties(properties)); }else{
device.setFeatures(new ArrayList<Feature>(0));
}
return device; return device;
} }