List of usage examples for com.google.gson JsonElement isJsonNull
public boolean isJsonNull()
From source file:com.skysql.manager.api.SystemInfo.java
License:Open Source License
public SystemInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { SystemInfo systemInfo = new SystemInfo(); JsonArray array = null;/* ww w . j a v a 2s . com*/ int length = 0; if (json.getAsJsonObject().has("systems")) { array = json.getAsJsonObject().get("systems").getAsJsonArray(); length = array.size(); } else if (json.getAsJsonObject().has("system")) { length = 1; } LinkedHashMap<String, SystemRecord> systemsMap = new LinkedHashMap<String, SystemRecord>(length); systemInfo.setSystemsMap(systemsMap); if (length == 0) { return systemInfo; } for (int i = 0; i < length; i++) { SystemRecord systemRecord = new SystemRecord(SystemInfo.SYSTEM_ROOT); JsonObject systemObject = (array != null) ? array.get(i).getAsJsonObject() : json.getAsJsonObject().get("system").getAsJsonObject(); JsonElement element; systemRecord .setID((element = systemObject.get("systemid")).isJsonNull() ? null : element.getAsString()); systemRecord.setSystemType( (element = systemObject.get("systemtype")).isJsonNull() ? null : element.getAsString()); systemRecord.setName((element = systemObject.get("name")).isJsonNull() ? null : element.getAsString()); systemRecord .setState((element = systemObject.get("state")).isJsonNull() ? null : element.getAsString()); systemRecord.setStartDate( (element = systemObject.get("started")).isJsonNull() ? null : element.getAsString()); systemRecord.setLastAccess( (element = systemObject.get("lastaccess")).isJsonNull() ? null : element.getAsString()); systemRecord.setLastBackup( (element = systemObject.get("lastbackup")).isJsonNull() ? null : element.getAsString()); systemRecord.setDBUsername( (element = systemObject.get("dbusername")).isJsonNull() ? null : element.getAsString()); systemRecord.setDBPassword( (element = systemObject.get("dbpassword")).isJsonNull() ? null : element.getAsString()); systemRecord.setRepUsername( (element = systemObject.get("repusername")).isJsonNull() ? null : element.getAsString()); systemRecord.setRepPassword( (element = systemObject.get("reppassword")).isJsonNull() ? null : element.getAsString()); systemRecord.setLastMonitored( ((element = systemObject.get("lastmonitored")).isJsonNull()) ? null : element.getAsString()); MonitorLatest monitorLatest = null; if ((element = systemObject.get("monitorlatest")) != null && !element.isJsonNull()) { monitorLatest = APIrestful.getGson().fromJson(element.toString(), MonitorLatest.class); } systemRecord.setMonitorLatest(monitorLatest); String[] nodes = null; if ((element = systemObject.get("nodes")) != null && !element.isJsonNull()) { JsonArray nodesJson = element.getAsJsonArray(); int nodesCount = nodesJson.size(); nodes = new String[nodesCount]; for (int nodesIndex = 0; nodesIndex < nodesCount; nodesIndex++) { nodes[nodesIndex] = nodesJson.get(nodesIndex).getAsString(); } } systemRecord.setNodes(nodes); LinkedHashMap<String, String> properties = new LinkedHashMap<String, String>(); if ((element = systemObject.get("properties")) != null && !element.isJsonNull()) { JsonObject propertiesJson = element.getAsJsonObject(); if ((element = propertiesJson.get(SystemInfo.PROPERTY_EIP)) != null) { properties.put(SystemInfo.PROPERTY_EIP, element.isJsonNull() ? null : element.getAsString()); } if ((element = propertiesJson.get(SystemInfo.PROPERTY_MONYOG)) != null) { properties.put(SystemInfo.PROPERTY_MONYOG, element.isJsonNull() ? null : element.getAsString()); } if ((element = propertiesJson.get(SystemInfo.PROPERTY_PHPMYADMIN)) != null) { properties.put(SystemInfo.PROPERTY_PHPMYADMIN, element.isJsonNull() ? null : element.getAsString()); } if ((element = propertiesJson.get(SystemInfo.PROPERTY_DEFAULTMONITORINTERVAL)) != null) { properties.put(SystemInfo.PROPERTY_DEFAULTMONITORINTERVAL, element.isJsonNull() ? null : element.getAsString()); } if ((element = propertiesJson.get(SystemInfo.PROPERTY_DEFAULTMAXBACKUPCOUNT)) != null) { properties.put(SystemInfo.PROPERTY_DEFAULTMAXBACKUPCOUNT, element.isJsonNull() ? null : element.getAsString()); } if ((element = propertiesJson.get(SystemInfo.PROPERTY_DEFAULTMAXBACKUPSIZE)) != null) { properties.put(SystemInfo.PROPERTY_DEFAULTMAXBACKUPSIZE, element.isJsonNull() ? null : element.getAsString()); } if ((element = propertiesJson.get(SystemInfo.PROPERTY_VERSION)) != null) { properties.put(SystemInfo.PROPERTY_VERSION, element.isJsonNull() ? null : element.getAsString()); } if ((element = propertiesJson.get(SystemInfo.PROPERTY_SKIPLOGIN)) != null) { properties.put(SystemInfo.PROPERTY_SKIPLOGIN, element.isJsonNull() ? null : element.getAsString()); } } systemRecord.setProperties(properties); systemsMap.put(systemRecord.getID(), systemRecord); } if (array != null) { // create a "ROOT" system record to contain the series of flat systems; in a hierarchical organization of systems, this might be provided by the API SystemRecord rootRecord = new SystemRecord(null); rootRecord.setID(SystemInfo.SYSTEM_ROOT); rootRecord.setName("Root"); String[] systems = new String[systemsMap.keySet().size()]; int i = 0; for (String systemID : systemsMap.keySet()) { systems[i++] = systemID; } rootRecord.setNodes(systems); systemsMap.put(SystemInfo.SYSTEM_ROOT, rootRecord); } return systemInfo; }
From source file:com.skysql.manager.api.SystemTypes.java
License:Open Source License
public SystemTypes deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { SystemTypes systemTypes = new SystemTypes(); JsonElement jsonElement = json.getAsJsonObject().get("systemtypes"); if (jsonElement == null || jsonElement.isJsonNull()) { systemTypes.setList(null);//from w ww . j a v a2 s . co m } else { LinkedHashMap<String, String> types = new LinkedHashMap<String, String>(); Set<Entry<String, JsonElement>> set = jsonElement.getAsJsonObject().entrySet(); Iterator<Entry<String, JsonElement>> iter = set.iterator(); while (iter.hasNext()) { Entry<String, JsonElement> entry = iter.next(); String type = entry.getKey(); String description = entry.getValue().getAsString(); types.put(type, description); } systemTypes.setList(types); } return systemTypes; }
From source file:com.skysql.manager.api.UserInfo.java
License:Open Source License
public UserInfo deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { UserInfo userInfo = new UserInfo(); JsonElement jsonElement = json.getAsJsonObject().get("users"); if (jsonElement.isJsonNull()) { userInfo.setUsersList(new LinkedHashMap<String, UserObject>()); } else {/*from w w w. j a v a 2s. c o m*/ JsonArray array = jsonElement.getAsJsonArray(); int length = array.size(); LinkedHashMap<String, UserObject> usersList = new LinkedHashMap<String, UserObject>(length); for (int i = 0; i < length; i++) { JsonObject backupJson = array.get(i).getAsJsonObject(); JsonElement element; String username = (element = backupJson.get("username")).isJsonNull() ? null : element.getAsString(); String name = (element = backupJson.get("name")).isJsonNull() ? null : element.getAsString(); UserObject userObject = new UserObject(username, name); usersList.put(username, userObject); } userInfo.setUsersList(usersList); } return userInfo; }
From source file:com.skysql.manager.api.UserObject.java
License:Open Source License
public UserObject deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { UserObject userObject = new UserObject(); JsonObject jsonObject = json.getAsJsonObject(); JsonElement element; if (jsonObject.has("user")) { jsonObject = jsonObject.get("user").getAsJsonObject(); userObject.setUserID(((element = jsonObject.get("username")) == null || element.isJsonNull()) ? null : element.getAsString()); userObject.setName(((element = jsonObject.get("name")) == null || element.isJsonNull()) ? null : element.getAsString()); JsonElement jsonElement = jsonObject.get("properties"); if (jsonElement == null || jsonElement.isJsonNull()) { userObject.setProperties(null); } else {/*from w w w. j av a 2 s . com*/ jsonObject = (JsonObject) jsonElement; LinkedHashMap<String, String> properties = new LinkedHashMap<String, String>(); Set<Entry<String, JsonElement>> set = jsonObject.entrySet(); Iterator<Entry<String, JsonElement>> iter = set.iterator(); while (iter.hasNext()) { Entry<String, JsonElement> entry = iter.next(); String property = entry.getKey(); String value = entry.getValue().getAsString(); properties.put(property, value); } userObject.setProperties(properties); } } else if (jsonObject.has("errors")) { } else { } return userObject; }
From source file:com.skysql.manager.api.Versions.java
License:Open Source License
public Versions deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException, NullPointerException { JsonElement jsonElement = json.getAsJsonObject().get("components"); if (jsonElement != null && !jsonElement.isJsonNull()) { LinkedHashMap<String, Versions> versionsList = new LinkedHashMap<String, Versions>(); Set<Entry<String, JsonElement>> set = jsonElement.getAsJsonObject().entrySet(); Iterator<Entry<String, JsonElement>> iter = set.iterator(); while (iter.hasNext()) { Entry<String, JsonElement> entry = iter.next(); String componentID = entry.getKey(); JsonObject jsonObject = entry.getValue().getAsJsonObject(); JsonElement element;// w w w. ja v a2s . c om String name = (element = jsonObject.get("name")) == null || element.isJsonNull() ? Versions.NOT_AVAILABLE : element.getAsString(); String version = (element = jsonObject.get("version")) == null || element.isJsonNull() ? Versions.NOT_AVAILABLE : element.getAsString(); String release = (element = jsonObject.get("release")) == null || element.isJsonNull() ? Versions.NOT_AVAILABLE : element.getAsString(); String date = (element = jsonObject.get("date")) == null || element.isJsonNull() ? null : element.getAsString(); versionsList.put(componentID, new Versions(name, version, release, date)); } Versions.setVersionsList(versionsList); } return null; }
From source file:com.skysql.manager.api.WriteResponse.java
License:Open Source License
public WriteResponse deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { WriteResponse writeResponse = new WriteResponse(); JsonElement jsonElement = json.getAsJsonObject().get("deletecount"); if (jsonElement != null && !jsonElement.isJsonNull()) { writeResponse.setDeleteCount(jsonElement.getAsInt()); }/*from w w w . j a v a 2s .c o m*/ jsonElement = json.getAsJsonObject().get("updatecount"); if (jsonElement != null && !jsonElement.isJsonNull()) { writeResponse.setUpdateCount(jsonElement.getAsInt()); } jsonElement = json.getAsJsonObject().get("insertkey"); if (jsonElement != null && !jsonElement.isJsonNull()) { writeResponse.setInsertKey(jsonElement.getAsString()); } return writeResponse; }
From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java
License:Open Source License
private @CheckForNull Object jsonToJavaObject(JsonElement jsonValue, Class<?> parameterType, Type gsonType) { Object value;//from ww w.ja v a 2 s .c om if (jsonValue.isJsonNull()) { return null; } // Handle string in a special way, due to possibility of having a Java char type in the Java // side if (isString(jsonValue)) { if (parameterType.equals(String.class)) { value = jsonValue.getAsString(); return value; } if (parameterType.equals(char.class) || parameterType.equals(Character.class)) { value = Character.valueOf(jsonValue.getAsString().charAt(0)); return value; } } // If Java parameter is Object, we perform 'magic': json string, number, boolean and // null are converted to Java String, Double, Boolean and null. For json objects, // we create a Map<String,Object>, and for json arrays an Object[], and then perform // internal object conversion recursively using the same technique if (parameterType.equals(Object.class) && SUPPORTS_OBJECT_TYPE_PARAMETER) { value = toSimpleJavaType(jsonValue); return value; } // If the Java parameter is an array, but we are receiving a single item, we try to convert // the item to a single item array so that the Java method can digest it boolean useCustomGsonType = gsonType != null; boolean fakeJsonArrayForManyValuedClasses = JsonDeserializationManager.isManyValuedClass(parameterType) && !jsonValue.isJsonArray(); Type typeToInstantiate = parameterType; if (useCustomGsonType) { typeToInstantiate = gsonType; } JsonElement json = jsonValue; if (fakeJsonArrayForManyValuedClasses) { JsonArray fakeJson = new JsonArray(); fakeJson.add(jsonValue); json = fakeJson; } value = getGson().fromJson(json, typeToInstantiate); return value; }
From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java
License:Open Source License
private @CheckForNull Object toSimpleJavaType(JsonElement jsonValue) { if (jsonValue == null) return null; //VR Object value = null;/*w ww .j av a 2 s . co m*/ if (!jsonValue.isJsonNull()) { if (jsonValue.isJsonPrimitive()) { JsonPrimitive primitive = jsonValue.getAsJsonPrimitive(); if (primitive.isBoolean()) { value = Boolean.valueOf(primitive.getAsBoolean()); } else if (primitive.isNumber()) { value = Double.valueOf(primitive.getAsDouble()); } else if (primitive.isString()) { value = primitive.getAsString(); } else { throw UnexpectedException.forUnexpectedCodeBranchExecuted(); } } else if (jsonValue.isJsonArray()) { //This simply does not work (?) JsonArray array = jsonValue.getAsJsonArray(); Object[] result = new Object[array.size()]; for (int i = 0; i < array.size(); i++) { result[i] = toSimpleJavaType(array.get(i)); } value = result; } else if (jsonValue.isJsonObject()) { //This simply does not work (?) //value = getGson().fromJson(jsonValue, Map.class ); JsonObject obj = jsonValue.getAsJsonObject(); Iterator<Entry<String, JsonElement>> properties = obj.entrySet().iterator(); Map<String, Object> result = new HashMap<String, Object>(); while (properties.hasNext()) { Entry<String, JsonElement> property = properties.next(); JsonElement propertyValue = property.getValue(); result.put(property.getKey(), toSimpleJavaType(propertyValue)); } value = result; } else { throw UnexpectedException.forUnexpectedCodeBranchExecuted(); } } return value; }
From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java
License:Open Source License
private static boolean isValidJsonTypeForJavaType(JsonElement jsonElement, Class<?> parameterType) { assert jsonElement != null; assert parameterType != null; // Check json nulls if (jsonElement.isJsonNull()) { return !parameterType.isPrimitive(); }//www. j ava 2 s. c om if (parameterType.isArray()) { // This is *always* ok because if the value is not a json array // we will instantiate a single item array and attempt conversion return true; } if (parameterType.equals(Boolean.class) || parameterType.equals(boolean.class)) { return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isBoolean(); } else if (parameterType.equals(char.class) || parameterType.equals(Character.class)) { if (jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isString()) { return jsonElement.getAsString().length() == 1; } return false; } else if (parameterType.equals(String.class)) { return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isString(); } else if (ClassUtils.isNumericType(parameterType)) { return jsonElement.isJsonPrimitive() && ((JsonPrimitive) jsonElement).isNumber(); } // If we arrived here, assume somebody will know how to handle the json element, maybe customizing Gson's serialization return true; }
From source file:com.softwarementors.extjs.djn.router.processor.standard.json.JsonRequestProcessor.java
License:Open Source License
@CheckForNull private static JsonArray getMethodParametersJsonData(JsonObject object) { assert object != null; JsonElement data = object.get(JsonRequestData.DATA_ELEMENT); if (data == null) { RequestException ex = RequestException.forJsonElementMissing(JsonRequestData.DATA_ELEMENT); logger.error(ex.getMessage(), ex); throw ex; }//ww w .ja v a 2s. c om if (data.isJsonNull()) { return null; } if (!data.isJsonNull() && !data.isJsonArray()) { RequestException ex = RequestException.forJsonElementMustBeAJsonArray(JsonRequestData.DATA_ELEMENT, data.toString()); logger.error(ex.getMessage(), ex); throw ex; } return (JsonArray) data; }